JAVA Programming 1
GUI(Graphical User Interface) 2 GUI!,! GUI! GUI, GUI GUI! GUI AWT Swing AWT - java.awt Swing - javax.swing
AWT Swing 3 AWT(Abstract Windowing Toolkit)! GUI! java.awt! AWT (Heavy weight components) AWT native(peer) GUI. Swing()! AWT AWT + AWT J J! javax.swing! Swing (Light weight components) native(peer)
4 JButton JCheckBox JRadioButton JSlider JTextField JPasswordField JSpinner JTextArea JComboBox JList
JProgressBar JToolTip JMenu JScrollPane JDialog JFrame JColorChooser 5
JTable 6 JTree JEditorPane and JTextPane
JToolBar JTabbedPane JSplitPane 7
Swing GUI 8 JButton JLabel JTextField JMenu JToolBar JFrame JMenuBar JComboBox JList JLabel JScrollPane JSplitPane
GUI Object Font FontMetrics Component Dimension Color Graphics AWT Button Label TextComponent Container List Canvas Scrollbar Panel Window Checkbox Choice Applet Frame Dialog Swing JComponent JApplet JFrame JDialog JLabel JList JComboBox JSlider JTextComponent AbstractButton JScrollBar JPanel JLayeredPane JEditorPane JTextField JTextArea JOptionPane JTabbedPane JSplitPane JSeparator JRootPane JToolBar JPasswordField JMenuItem JButton JToggleButton JMenuBar JToolTip JPopupMenu JFileChooser JMenu JCheckBox JColorChooser JTree JTable JTableHeader JCheckBoxMenuItem JRadioButton JProgressBar JSpinner JScrollPane JInternalFrame JRadioButtonMenuItem 9
Swing 10 J GUI 2! JComponent! AWT Container JApplet, JDialog, JFrame JComponent! new JComponent()! AWT Component
11! GUI! java.awt.container!! AWT : Panel, Frame, Applet, Dialog, Window Swing : JPanel JFrame, JApplet, JDialog, JWindow! JFrame, JDialog, JApplet!!! java.awt.component! javax.swing.jcomponent
12
GUI 13 1. 2. main() 3. import! import java.awt.*; //! import java.awt.event.*; // AWT! import javax.swing.*; //! import javax.swing.event.*; //
14 GUI! JFrame!! (JFrame)!!! GUI Frame Menu Bar JFrame Frame : Content Pane
9-1 : 300x300 15 300 300. import javax.swing.*; public class MyFrame extends JFrame { public MyFrame() { settitle("300x300 "); setsize(300,300); // 300x300 setvisible(true); // public static void main(string[] args) { MyFrame frame = new MyFrame();
main() 16 import javax.swing.*; public class MyFrame extends JFrame { MyFrame() { settitle(" "); setsize(300,300); setvisible(true); public static void main(string [] args) { MyFrame mf=new MyFrame(); main() import javax.swing.*; class MyFrame extends JFrame { MyFrame() { settitle(" "); setsize(300,300); setvisible(true); public class MyApp { public static void main(string [] args) { MyFrame mf = new MyFrame(); main() MyApp
17 public MyFrame() { // super(""); // Jframe settitle(""); // Container contentpane = frame.getcontentpane(); Container c = frame.getcontentpane(); JButton b = new JButton("Click"); c.add(b); JPanel p = new JPanel(); frame.setcontentpane(p);
9-2 : 3 18, OK, Cancel, Ignore. import javax.swing.*; import java.awt.*; public class ContentPaneEx extends JFrame { public ContentPaneEx() { settitle("contentpane JFrame"); setdefaultcloseoperation(jframe.exit_on_close); Container contentpane = getcontentpane(); contentpane.setbackground(color.orange); contentpane.setlayout(new FlowLayout()); contentpane.add(new JButton("OK")); contentpane.add(new JButton("Cancel")); contentpane.add(new JButton("Ignore")); setsize(300, 150); setvisible(true); public static void main(string[] args) { new ContentPaneEx();
19 System.exit(0);! (X)?!! setvisible(true) frame.setdefaultcloseoperation(jframe.exit_on_close);
main()? 20! main()! GUI! main()!
21 (Container) 1.,.. 2.. (Layout Manager) (Component)
4 22 java.awt FlowLayout BorderLayout.. GridLayout CardLayout..
23!! Container.setLayout(LayoutManager lm) lm // JPanel BorderLayout JPanel p = new JPanel(); p.setlayout(new BorderLayout()); // FlowLayout Container c = frame.getconentpane(); // c.setlayout(new FlowLayout());
FlowLayout 24!. container.setlayout(new FlowLayout()); container.add(new JButton("add")); container.add(new JButton("sub")); container.add(new JButton("mul")); container.add(new JButton("div")); container.add(new JButton("Calculate"));!
FlowLayout - 25 vgap, 40 hgap, 30 FlowLayout.LEFT
9-3 : FlowLayout 26 30, 40 LEFT FlowLayout 5. import javax.swing.*; import java.awt.*; public class FlowLayoutEx extends JFrame { public FlowLayoutEx() { settitle("flowlayout Sample"); setdefaultcloseoperation(jframe.exit_on_close); Container c = getcontentpane(); c.setlayout(new FlowLayout(FlowLayout.LEFT, 30, 40)); c.add(new JButton("add")); c.add(new JButton("sub")); c.add(new JButton("mul")); c.add(new JButton("div")); c.add(new JButton("Calculate")); setsize(300, 200); setvisible(true); hgap, 30 FlowLayout.LEFT vgap, 40 public static void main(string[] args) { new FlowLayoutEx();
BorderLayout 27! 5, East, West, South, North, Center! add(component comp, int index) comp index! BorderLayout.NORTH BorderLayout.EAST container.setlayout(new BorderLayout()); container.add(new JButton("div"), BorderLayout.WEST); container.add(new JButton("Calculate"), BorderLayout.CENTER); BorderLayout.WEST BorderLayout.SOUTH BorderLayout.CENTER
BorderLayout 28
BorderLayout 29 CENTER CENTER NORTH CENTER, NORTH, SOUTH new BorderLayout(30,20); vgap, 20 hgap, 30
9-4 : BorderLayout 30 BorderLayout 5. import javax.swing.*; import java.awt.*; public class BorderLayoutEx extends JFrame { public BorderLayoutEx() { settitle("borderlayout Sample"); setdefaultcloseoperation(jframe.exit_on_close); vgap, 20 Container c = getcontentpane(); c.setlayout(new BorderLayout(30, 20)); c.add(new JButton("Calculate"), BorderLayout.CENTER); c.add(new JButton("add"), BorderLayout.NORTH); c.add(new JButton("sub"), BorderLayout.SOUTH); c.add(new JButton("mul"), BorderLayout.EAST); c.add(new JButton("div"), BorderLayout.WEST); hgap, 30 setsize(300, 200); // 300 200 setvisible(true); // public static void main(string[] args) { new BorderLayoutEx();
GridLayout 31! (), container.setlayout(new GridLayout(4,3,5,5)); // 4 3 container.add(new JButton("1")); // container.add(new JButton("2")); //! 4x3 11 add vgap : 5 hgap : 5
GridLayout 32
9-5 : GridLayout 33 import javax.swing.*; import java.awt.*; public class GridLayoutEx extends JFrame { public GridLayoutEx() { settitle("gridlayout Sample"); setdefaultcloseoperation(jframe.exit_on_close); vgap 5 GridLayout grid = new GridLayout(4, 2); grid.setvgap(5); Container c = getcontentpane(); c.setlayout(grid); c.add(new JLabel(" ")); c.add(new JTextField("")); c.add(new JLabel(" ")); c.add(new JTextField("")); c.add(new JLabel(" ")); c.add(new JTextField("")); c.add(new JLabel(" ")); c.add(new JTextField("")); setsize(300, 200); setvisible(true); public static void main(string[] args) { new GridLayoutEx();
34?!!! /!! container.setlayout(null); // JPanel JPanel p = new JPanel(); p.setlayout(null);!! : component.setsize(int width, int height); : component.setlocation(int x, int y); : component.setbounds(int x, int y, int width, int height);
9-6 : 35 import javax.swing.*; import java.awt.*; public class NullContainerEx extends JFrame { public NullContainerEx() { settitle("null Container Sample"); setdefaultcloseoperation(jframe.exit_on_close); Container c = getcontentpane(); c.setlayout(null); JLabel la = new JLabel("Hello, Press Buttons!"); la.setlocation(130, 50); la.setsize(200, 20); c.add(la);. for(int i=1; i<=9; i++) { JButton b = new JButton(Integer.toString(i)); // b.setlocation(i*15, i*15); b.setsize(50, 20); c.add(b); // setsize(300, 200); setvisible(true); public static void main(string[] args) { new NullContainerEx();
36