rt
解决方案 »
- 菜鸟提问,java除了用记事本编辑外,还有什么好的编辑工具,谢谢!
- 请各位大哥想想办法,让上面这段程序能够运行,谢谢!
- 一个servlet错误 Can't find bundle for base name LocalStrings, locale zh_CN
- 推荐好书~~~~~~~~~~~~~~~~~~~~~~~~~~
- eclise3.0的javadoc在什么地方设置
- 基础问题,急急急,谁能帮帮我啊??????
- 24点扑克牌游戏编程算法详解
- Java问题求解答
- Applet中this.add(new Label("kk"));只能在init()事件中有效吗?
- 如何对HTML文件进行全文检索
- 反射的作用?
- JOptionPane类在不同的电脑分别变现为模式或无模式对话框,请教大家
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;public class EventTest extends javax.swing.JFrame {
private JPanel MainJPanel;
private JButton btnName;
private JButton btnAge;
private JButton btnSex;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
EventTest inst = new EventTest();
inst.setLocationRelativeTo(null);
inst.setVisible(true);
}
});
} public EventTest() {
super();
initGUI();
} private void initGUI() {
try {
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); MainJPanel = new JPanel();
getContentPane().add(MainJPanel, BorderLayout.CENTER); btnName = new JButton();
MainJPanel.add(btnName);
btnName.setText("Name"); btnSex = new JButton();
MainJPanel.add(btnSex);
btnSex.setText("Sex"); btnAge = new JButton();
MainJPanel.add(btnAge);
btnAge.setText("Age"); btnAge.addActionListener(new myActionListener(btnAge));
btnName.addActionListener(new myActionListener(btnName));
btnSex.addActionListener(new myActionListener(btnSex)); pack();
setSize(400, 300);
} catch (Exception e) {
e.printStackTrace();
}
}}class myActionListener implements ActionListener { Component component; public myActionListener() { }
public myActionListener(Component component) {
this.component = component;
} public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(null, ((JButton) component).getText()
.toString());
}}//仅供参考...
然后调用
Component.getComponentAt(int x, int y);
就可以了