Transcript
Page 1: “Look and Feel” and Layouts

“Look and Feel” and Layouts

Feb 11, 2000

Page 2: “Look and Feel” and Layouts

Pluggable Look and Feel

You can change the looking or “look and feel” of your Swing application easily into: Windows Macintosh Motif (X-windows) Java look

Big win for software portability in a sense that we can have a program which looks the same across platforms.

If you don’t care about how your program looks, Don’t bother!!!

Page 3: “Look and Feel” and Layouts

public static void main(String[] args) { try { UIManager.setLookAndFeel( // Put your favorite Look and Feel here!!! ); } catch (Exception e) { }

new SwingApplication();}

•"javax.swing.plaf.metal.MetalLookAndFeel" •"com.sun.java.swing.plaf.windows.WindowsLookAndFeel”

•"com.sun.java.swing.plaf.motif.MotifLookAndFeel" •"javax.swing.plaf.mac.MacLookAndFeel"

Setting Look and Feel

Page 4: “Look and Feel” and Layouts

Layouts

Page 5: “Look and Feel” and Layouts

import java.awt.*;import java.awt.event.*;import javax.swing.*;

class LayoutDemo{ JFrame frame; Container pane;

LayoutDemo(){ frame = new JFrame("LayoutDemo"); pane = frame.getContentPane(); }

private void setupGUI(int w,int l) { addComponent(); frame.setSize(w,l); frame.setVisible(true); frame.pack(); }

private void addComponent() { // next page } public static void main(String args[]) { int w = Integer.parseInt(args[0]); int l = Integer.parseInt(args[1]); LayoutDemo demo = new LayoutDemo(); demo.setupGUI(w,l); }}

Page 6: “Look and Feel” and Layouts

private void addComponent(){ pane.setLayout(new BorderLayout());

JCheckBox jck1 = new JCheckBox("this class is easy"); JCheckBox jck2 = new JCheckBox("this class is hard");

pane.add(jck1,"East"); pane.add(jck2,"South");

JButton b1 = new JButton("no touch"); JButton b2 = new JButton("please touch");

pane.add(b1,"North"); pane.add(b2,"West");

JLabel label = new JLabel(new ImageIcon("cab.gif")); pane.add(label,"Center");}

Page 7: “Look and Feel” and Layouts

private void addComponent(){ pane.setLayout(new GridLayout(2,3));

JCheckBox jck1 = new JCheckBox("this class is easy"); JCheckBox jck2 = new JCheckBox("this class is hard");

pane.add(jck1); pane.add(jck2);

JButton b1 = new JButton("no touch"); JButton b2 = new JButton("please touch");

pane.add(b1); pane.add(b2);

JLabel label = new JLabel(new ImageIcon("cab.gif")); pane.add(label);}

Page 8: “Look and Feel” and Layouts

private void addComponent(){ pane.setLayout(new FlowLayout());

JCheckBox jck1 = new JCheckBox("this class is easy"); JCheckBox jck2 = new JCheckBox("this class is hard");

pane.add(jck1); pane.add(jck2);

JButton b1 = new JButton("no touch"); JButton b2 = new JButton("please touch");

pane.add(b1); pane.add(b2);

JLabel label = new JLabel(new ImageIcon("cab.gif")); pane.add(label);}

Page 9: “Look and Feel” and Layouts

private void addComponent(){ pane.setLayout(new BoxLayout(pane,BoxLayout.Y_AXIS)); ...

}

private void addComponent(){ pane.setLayout(new BoxLayout(pane,BoxLayout.X_AXIS)); ...

}

Page 10: “Look and Feel” and Layouts

private void addComponent() {JPanel p1 = new JPanel();p1.setLayout(

new BoxLayout(p1,BoxLayout.Y_AXIS));JCheckBox jck1 = new JCheckBox("this class is easy");JCheckBox jck2 = new JCheckBox("this class is hard");p1.add(jck1);p1.add(jck2);

JPanel p2 = new JPanel();p2.setLayout(

new BoxLayout(p2,BoxLayout.Y_AXIS));JButton b1 = new JButton("no touch");JButton b2 = new JButton("please touch");p2.add(b1);p2.add(b2);

JLabel label = new JLabel( new ImageIcon("cab.gif"));

pane.add(label,"Center");pane.add(p1,"West");pane.add(p2,"East");

}


Top Related