Swing Tutorial
Swing Tutorial來瞭解java中,如何快速地作出GUI應用程式。
- http://www.apl.jhu.edu/~hall/java/Swing-Tutorial/
- Components
- NTU Swing介紹
- zaiane AWT Swing介紹
- JFormDesigner
- JFrameBuilder
- Form Designer
- GUI Builder
import javax.swing.*; public void main(String[] args) { JFrame.setDefaultLookAndFeelDecorated(true); JFrame Frame=new JFrame("Hellow the Java Wonderland World"); JLabel Label =new JLabel("Hello Bravo World"); Frame.getContentPane.add(Label); Frame.getContentPane().setLayout(new FlowLayout()); Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Frame.pack(); Frame.setVisible(true); }Each JRootPane contains several components referred to here by variable name: glassPane (a JPanel by default), layeredPane (a JLayeredPanel), contentPane (a JPanel by default) and menuBar (a JMenuBar).
JRootPane包括GlassPane, LayeredPane, ContentPane, MenuBar。GlassPane好像是放在LayeredPane上一個透明的Pane,對於攔截Mouse events,或是重導應用程式的焦點特別有用,可用
setGlassPane(myComponent);
getGlassPane().setVisible(true);
來使用它。
JMenuBar menu = new JMenuBar(); setJMenuBar(menu); setContentPane(myComponent);The default layout for the contentPane is BorderLayout. The default layout for any other JPanel is FlowLayout. Be careful not to set the layout of a JFrame directly. This will generate an exception. Setting the layout of the rootPane is also something that should be avoided because every JRootPane uses its own custom layout manager called RootLayout.
Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); WindowListener l = new WindowAdapter() { public void windowClosing(WindowEvent e){ System.exit(0); } }; myJFrame.addWindowListener(l);
import java.awt.*; import javax.swing.*; import javax.swing.border.*; class BorderTest extends JFrame { public BorderTest() { setTitle("Border Test"); setSize(450, 450); JPanel content = (JPanel) getContentPane(); content.setLayout(new GridLayout(6,2)); JPanel p = new JPanel(); p.setBorder(new BevelBorder (BevelBorder.RAISED)); p.add(new JLabel("RAISED BevelBorder")); content.add(p); p = new JPanel(); p.setBorder(new BevelBorder (BevelBorder.LOWERED)); p.add(new JLabel("LOWERED BevelBorder")); content.add(p); p = new JPanel(); p.setBorder(new LineBorder (Color.black, 5)); p.add(new JLabel("Black LineBorder, thickness = 5")); content.add(p); p = new JPanel(); p.setBorder(new EmptyBorder (10,10,10,10)); p.add(new JLabel("EmptyBorder with thickness of 10")); content.add(p); p = new JPanel(); p.setBorder(new EtchedBorder (EtchedBorder.RAISED)); p.add(new JLabel("RAISED EtchedBorder")); content.add(p); p = new JPanel(); p.setBorder(new EtchedBorder (EtchedBorder.LOWERED)); p.add(new JLabel("LOWERED EtchedBorder")); content.add(p); p = new JPanel(); p.setBorder(new SoftBevelBorder (SoftBevelBorder.RAISED)); p.add(new JLabel("RAISED SoftBevelBorder")); content.add(p); p = new JPanel(); p.setBorder(new SoftBevelBorder (SoftBevelBorder.LOWERED)); p.add(new JLabel("LOWERED SoftBevelBorder")); content.add(p); p = new JPanel(); p.setBorder(new MatteBorder (new ImageIcon("spiral.gif"))); p.add(new JLabel("MatteBorder")); content.add(p); p = new JPanel(); p.setBorder(new TitledBorder ( new MatteBorder (new ImageIcon("spiral.gif")), "Title String")); p.add(new JLabel("TitledBorder using MatteBorder")); content.add(p); p = new JPanel(); p.setBorder(new TitledBorder ( new LineBorder (Color.black, 5), "Title String")); p.add(new JLabel("TitledBorder using LineBorder")); content.add(p); p = new JPanel(); p.setBorder(new TitledBorder ( new EmptyBorder (10,10,10,10), "Title String")); p.add(new JLabel("TitledBorder using EmptyBorder")); content.add(p); setVisible(true); } public static void main(String args[]) { new BorderTest(); } }