300 likes | 632 Views
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.
E N D
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.
The 1st Swing Program javac HelloWorldSwing.java java HelloWorldSwing
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); } }
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
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
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
The 2nd Swing Example javac SwingApplication.java java SwingApplication
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) { … } }
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); }
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); }
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")); } }
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); } }
Example 3 • Look and feel • Metal • Motif • Windows • Cross Platform Look and Feel (Metal) • Radio buttons
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[]) { } }
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); }
/** 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); } } }
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); }
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(); }