一直就想学习java的桌面编程,可惜直到现在因为是在unix上的原因并不需要java程序有界面,不过自己还是想好好学习学习。所以从今天开始总结经验:
1 import java.awt.*;
2 import java.awt.event.*;
3 import javax.swing.*;
4
5 public class ToolbarFrame2 extends Frame {
6
7 // 使用Swing组件
8 JButton cutButton, copyButton, pasteButton;
9 JButton javaButton, macButton, motifButton, winButton;
10
11 public ToolbarFrame2( ) {
12 super("Toolbar Example (Swing)");
13 setSize(450, 250);
14
15 //退出
16 addWindowListener(new WindowAdapter( ) {
17 public void windowClosing(WindowEvent e) {
18 System.exit(0);
19 }
20 });
21
22 //匿名类来出来copy cut paste事件
23 ActionListener printListener = new ActionListener( ) {
24 public void actionPerformed(ActionEvent ae) {
25 System.out.println(ae.getActionCommand( ));
26 }
27 };
28
29 // 上层的工具面版.
30 JPanel toolbar = new JPanel( );
31 toolbar.setLayout(new FlowLayout(FlowLayout.LEFT));//流式布局
32
33 cutButton = new JButton("Cut");
34 cutButton.addActionListener(printListener);
35 toolbar.add(cutButton);
36
37 copyButton = new JButton("Copy");
38 copyButton.addActionListener(printListener);
39 toolbar.add(copyButton);
40
41 pasteButton = new JButton("Paste");
42 pasteButton.addActionListener(printListener);
43 toolbar.add(pasteButton);
44
45 add(toolbar, BorderLayout.NORTH);
46
47 // 添加感观按钮
48 JPanel lnfPanel = new JPanel( );
49 LnFListener lnfListener = new LnFListener(this);
50 macButton = new JButton("Mac");
51 macButton.addActionListener(lnfListener);
52 lnfPanel.add(macButton);
53 javaButton = new JButton("Metal");
54 javaButton.addActionListener(lnfListener);
55 lnfPanel.add(javaButton);
56 motifButton = new JButton("Motif");
57 motifButton.addActionListener(lnfListener);
58 lnfPanel.add(motifButton);
59 winButton = new JButton("Windows");
60 winButton.addActionListener(lnfListener);
61 lnfPanel.add(winButton);
62 add(lnfPanel, BorderLayout.SOUTH);
63 }
64
65 public static void main(String args[]) {
66 ToolbarFrame2 tf2 = new ToolbarFrame2( );
67 tf2.setVisible(true);
68 }
69 }
1 import java.awt.*;
2 import java.awt.event.*;
3 import javax.swing.*;
4
5
6 public class LnFListener implements ActionListener {
7 Frame frame;
8
9 public LnFListener(Frame f) {
10 frame = f;
11 }
12
13 public void actionPerformed(ActionEvent e) {
14 String lnfName = null;
15 if (e.getActionCommand( ).equals("Mac")) {
16 lnfName = "com.apple.mrj.swing.MacLookAndFeel";
17 } else if (e.getActionCommand( ).equals("Metal")) {
18 lnfName = "javax.swing.plaf.metal.MetalLookAndFeel";
19 } else if (e.getActionCommand( ).equals("Motif")) {
20 lnfName = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
21 } else if (e.getActionCommand( ).equals("Windows")) {
22 lnfName = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
23 } else {
24 System.err.println("Unrecognized L&F request action: " +
25 e.getActionCommand( ));
26 return;
27 }
28 try {
29 UIManager.setLookAndFeel(lnfName);//设置当前默认外观
30 SwingUtilities.updateComponentTreeUI(frame);//通过当前外观初始化其 UI
31 }
32 catch (UnsupportedLookAndFeelException ex1) {
33 System.err.println("Unsupported LookAndFeel: " + lnfName);
34 }
35 catch (ClassNotFoundException ex2) {
36 System.err.println("LookAndFeel class not found: " + lnfName);
37 }
38 catch (InstantiationException ex3) {
39 System.err.println("Could not load LookAndFeel: " + lnfName);
40 }
41 catch (IllegalAccessException ex4) {
42 System.err.println("Cannot use LookAndFeel: " + lnfName);
43 }
44 }
45 }
先编译LnFListener 再编译ToolbarFrame2 就可以了
效果图如下: