1 / 23

JAVA Swing 簡介

JAVA Swing 簡介. 鄧姚文 joseph.deng@gmail.com http://www.ywdeng.idv.tw. What is Swing?. The Swing package is part of the Java Foundation Classes (JFC) JFC encompasses a group of features to help people build GUIs Swing provides all the components from buttons to split panes and tables.

natara
Download Presentation

JAVA Swing 簡介

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. JAVA Swing 簡介 鄧姚文 joseph.deng@gmail.com http://www.ywdeng.idv.tw

  2. What is Swing? • The Swing package is part of the Java Foundation Classes (JFC) • JFC encompasses a group of features to help people build GUIs • Swing provides all the components from buttons to split panes and tables.

  3. The 1st Swing Program javac HelloWorldSwing.java java HelloWorldSwing

  4. The 1st Swing Program import javax.swing.*; public class HelloWorldSwing { public static void main(String[] args) { JFrame frame = new JFrame("HelloWorldSwing"); String msg = "Hello World! This is JAVA Swing."; final JLabel label = new JLabel(msg); frame.getContentPane().add(label); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); } }

  5. The 1st Swing Program • import javax.swing.*; • Import the swing package • JFrame frame = new JFrame("HelloWorldSwing"); • Create a frame (Window) with title • final JLabel label = new JLabel(msg); • Create a constant label • frame.getContentPane().add(label); • Add the label to the frame

  6. The 1st Swing Program • frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); • Sets the operation that will happen by default when the user initiates a "close" on this frame • frame.pack(); • Causes this Window to be sized to fit the preferred size • frame.setVisible(true); • Show it

  7. The 2nd Swing Example • Event • Action • The action performed, which fires the event • Listener • The receiver of the event • Layout • The layout of a container

  8. Examples of Events and Their Associated Event Listeners

  9. The 2nd Swing Example javac SwingApplication.java java SwingApplication

  10. import javax.swing.*; import java.awt.*; import java.awt.event.*; public class SwingApplication { private static String labelPrefix = "按鍵次數: "; private int numClicks = 0; public Component createComponents() { … } public static void main(String[] args) { … } }

  11. public Component createComponents() { final JLabel label = new JLabel(labelPrefix + "0 "); JButton button = new JButton("I'm a Swing button!"); button.setMnemonic(KeyEvent.VK_I); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { numClicks++; label.setText(labelPrefix + numClicks); } }); label.setLabelFor(button); JPanel pane = new JPanel(); pane.setBorder(BorderFactory.createEmptyBorder( 30 /*top*/, 30 /*left*/, 10 /*bottom*/, 30 /*right*/)); pane.setLayout(new GridLayout(0 /*row*/, 1 /*col*/)); pane.add(button); pane.add(label); }

  12. public static void main(String[] args) { try { UIManager.setLookAndFeel( UIManager.getCrossPlatformLookAndFeelClassName()); } catch (Exception e) {} //Create the top-level container and add contents to it. JFrame frame = new JFrame("SwingApplication"); SwingApplication app = new SwingApplication(); Component contents = app.createComponents(); frame.getContentPane().add(contents, BorderLayout.CENTER); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); }

  13. Borders

  14. GridLayout import java.awt.*; import java.applet.Applet; public class ButtonGrid extends Applet { public void init() { setLayout(new GridLayout(3,2)); add(new Button("1")); add(new Button("2")); add(new Button("3")); add(new Button("4")); add(new Button("5")); add(new Button("6")); } }

  15. BorderLayout

  16. BorderLayout import java.awt.*; import java.applet.Applet; public class buttonDir extends Applet { public void init() { setLayout(new BorderLayout()); add(new Button("North"), BorderLayout.NORTH); add(new Button("South"), BorderLayout.SOUTH); add(new Button("East"), BorderLayout.EAST); add(new Button("West"), BorderLayout.WEST); add(new Button("Center"), BorderLayout.CENTER); } }

  17. Example 3 • Look and feel • Metal • Motif • Windows • Cross Platform Look and Feel (Metal) • Radio buttons

  18. Example 3

  19. import java.awt.*; import java.awt.event.*; import javax.swing.*; public class SimpleExample extends JPanel { static JFrame frame; static String metal= "Metal"; static String metalClassName = "javax.swing.plaf.metal.MetalLookAndFeel"; static String motif = "Motif"; static String motifClassName = "com.sun.java.swing.plaf.motif.MotifLookAndFeel"; static String windows = "Windows"; static String windowsClassName = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel"; JRadioButton metalButton, motifButton, windowsButton; public SimpleExample() { } class RadioListener implements ActionListener { } public void updateState() { } public static void main(String s[]) { } }

  20. public SimpleExample() { JButton button = new JButton("Hello, world"); button.setMnemonic('h'); //for looks only metalButton = new JRadioButton(metal); metalButton.setMnemonic('o'); metalButton.setActionCommand(metalClassName); motifButton = new JRadioButton(motif); motifButton.setMnemonic('m'); motifButton.setActionCommand(motifClassName); windowsButton = new JRadioButton(windows); windowsButton.setMnemonic('w'); windowsButton.setActionCommand(windowsClassName); ButtonGroup group = new ButtonGroup(); group.add(metalButton); group.add(motifButton); group.add(windowsButton); RadioListener myListener = new RadioListener(); metalButton.addActionListener(myListener); motifButton.addActionListener(myListener); windowsButton.addActionListener(myListener); add(button); add(metalButton); add(motifButton); add(windowsButton); }

  21. /** An ActionListener that listens to the radio buttons. */ class RadioListener implements ActionListener { public void actionPerformed(ActionEvent e) { String lnfName = e.getActionCommand(); try { UIManager.setLookAndFeel(lnfName); SwingUtilities.updateComponentTreeUI(frame); frame.pack(); } catch (Exception exc) { JRadioButton button = (JRadioButton)e.getSource(); button.setEnabled(false); updateState(); System.err.println("Could not load LookAndFeel: " + lnfName); } } }

  22. public void updateState() { String lnfName = UIManager.getLookAndFeel().getClass().getName(); if (lnfName.indexOf(metal) >= 0) { metalButton.setSelected(true); } else if (lnfName.indexOf(windows) >= 0) { windowsButton.setSelected(true); } else if (lnfName.indexOf(motif) >= 0) { motifButton.setSelected(true); } else { System.err.println("SimpleExample is using an unknown L&F: " + lnfName); }

  23. public static void main(String s[]) { SimpleExample panel = new SimpleExample(); frame = new JFrame("SimpleExample"); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) {System.exit(0);} }); frame.getContentPane().add("Center", panel); frame.pack(); frame.setVisible(true); panel.updateState(); }

More Related