在该文中将使用实例来说明下面几个问题:
Look and Feel Setting Up Buttons and Labels Adding Components to Containers Adding Borders Around Components Handling Events
我没有仔细看就把源文件下了下来。的确没有太多的耐心看他啰嗦,呆会再仔细看吧!
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SwingApplication implements ActionListener {
private static String labelPrefix = "按钮被点击的次数:";
private int numClicks = 0;
final JLabel label = new JLabel(labelPrefix + "0 ");
final static String LOOKANDFEEL = null;
public Component createComponents() {
JButton button = new JButton("I'm a Swing button!");
button.setMnemonic(KeyEvent.VK_I);
button.addActionListener(this);
label.setLabelFor(button);
JPanel pane = new JPanel(new GridLayout(0, 1));
pane.setBorder(BorderFactory.createEmptyBorder(30, //top
30, //left
10, //bottom
30) //right
);
pane.add(button);
pane.add(label);
return pane;
}
public void actionPerformed(ActionEvent e) {
numClicks++;
label.setText(labelPrefix + numClicks);
}
private static void initLookAndFeel() {
String lookAndFeel = null;
if (LOOKANDFEEL != null) {
if (LOOKANDFEEL.equals("Metal")) {
lookAndFeel = UIManager.getCrossPlatformLookAndFeelClassName();
} else if (LOOKANDFEEL.equals("System")) {
lookAndFeel = UIManager.getSystemLookAndFeelClassName();
} else if (LOOKANDFEEL.equals("Motif")) {
lookAndFeel = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
} else if (LOOKANDFEEL.equals("GTK+")) { //new in 1.4.2
lookAndFeel = "com.sun.java.swing.plaf.gtk.GTKLookAndFeel";
} else {
System.err.println("Unexpected value of LOOKANDFEEL specified: "
+ LOOKANDFEEL);
lookAndFeel = UIManager.getCrossPlatformLookAndFeelClassName();
}
try {
UIManager.setLookAndFeel(lookAndFeel);
} catch (ClassNotFoundException e) {
System.err.println("Couldn't find class for specified look and feel:"
+ lookAndFeel);
System.err
.println("Did you include the L&F library in the class path?");
System.err.println("Using the default look and feel.");
} catch (UnsupportedLookAndFeelException e) {
System.err.println("Can't use the specified look and feel ("
+ lookAndFeel + ") on this platform.");
System.err.println("Using the default look and feel.");
} catch (Exception e) {
System.err.println("Couldn't get specified look and feel ("
+ lookAndFeel + "), for some reason.");
System.err.println("Using the default look and feel.");
e.printStackTrace();
}
}
}
private static void createAndShowGUI() {
initLookAndFeel();
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("SwingApplication");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
SwingApplication app = new SwingApplication();
Component contents = app.createComponents();
frame.getContentPane().add(contents, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
郁闷,大致浏览了源代码,除了把按钮和标记放到面板中以外,其他和第一个示例是一样的。
运行结果难看死了,尤其是那些汉字(这又使我想起SUN对中国的不支持,唉,要不是为了钱,我还真不学这东东了,天啊!我是多么的可怜。)。
button.setMnemonic('i'); 设置一个快捷键
这里出现了布局管理器:new GridLayout(0, 1)
Borders为继承自JComponent的JPanel的一个特性,Border对象不是JComponent对象。他用于一个或者更多个组件画边缘。
Some Events and Their Associated Event Listeners
Act that Results in the Event
Listener Type
User clicks a button, presses Enter while typing
in a text field, or chooses a menu item
ActionListener
User closes a frame (main window)
WindowListener
User presses a mouse button while the cursor is
over a component
MouseListener
User moves the mouse over a component
MouseMotionListener
Component becomes visible
ComponentListener
Component gets the keyboard focus
FocusListener
Table or list selection changes
ListSelectionListener
Any property in a component changes such as
the text on a label
PropertyChangeListener
看到新东西:
Note: Event-handling code executes in an single thread, the event-dispatching thread. This ensures that each event handler finishes execution before the next one executes. For instance, the actionPerformed method in the preceding example executes in the event-dispatching thread. Painting code also executes in the event-dispatching thread. Therefore, event-handling code should execute quickly so that the program’s GUI stays responsive. If an event takes too long to execute, the GUI will freeze--that is, it won’t repaint or respond to mouse clicks. Writing Event Listeners has for more information.
这个得好好想想。Java中的线程好像和win32的不大一样。
555,好难看的东西,睡觉去吧!
本文地址:http://com.8s8s.com/it/it15271.htm