分享
 
 
 

让我勉强撑着继续往下面看吧

王朝java/jsp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

在该文中将使用实例来说明下面几个问题:

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,好难看的东西,睡觉去吧!

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有