请教! JButton b = e.getSource();String bName = b.getText(); 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 按钮完整版: /** * TestButtons.java * @author Fancy */import javax.swing.*;import java.awt.event.*;public class TestButtons { JFrame frame = new JFrame("Test Buttons"); JButton jButton = new JButton("JButton"); //按钮 JToggleButton toggle = new JToggleButton("Toggle Button"); //切换按钮 JCheckBox checkBox = new JCheckBox("Check Box"); //复选按钮 JRadioButton radio1 = new JRadioButton("Radio Button 1"); //单选按钮 JRadioButton radio2 = new JRadioButton("Radio Button 2"); JRadioButton radio3 = new JRadioButton("Radio Button 3"); JLabel label = new JLabel("Here is Status, look here."); //不是按钮,是静态文本 public TestButtons() { frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(new java.awt.FlowLayout()); /* 为一般按钮添加动作监听器 */ jButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { label.setText("You clicked jButton"); } }); /* 为切换按钮添加动作监听器 */ toggle.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { JToggleButton toggle = (JToggleButton) ae.getSource(); if (toggle.isSelected()) { label.setText("You selected Toggle Button"); } else { label.setText("You deselected Toggle Button"); } } }); /* 为复选按钮添加条目监听器 */ checkBox.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { JCheckBox cb = (JCheckBox) e.getSource(); label.setText("Selected Check Box is " + cb.isSelected()); } }); /* 用一个按钮组对象包容一组单选按钮 */ ButtonGroup group = new ButtonGroup(); /* 生成一个新的动作监听器对象,备用 */ ActionListener al = new ActionListener() { public void actionPerformed(ActionEvent ae) { JRadioButton radio = (JRadioButton) ae.getSource(); if (radio == radio1) { label.setText("You selected Radio Button 1"); } else if (radio == radio2) { label.setText("You selected Radio Button 2"); } else { label.setText("You selected Radio Button 3"); } } }; /* 为各单选按钮添加动作监听器 */ radio1.addActionListener(al); radio2.addActionListener(al); radio3.addActionListener(al); /* 将单选按钮添加到按钮组中 */ group.add(radio1); group.add(radio2); group.add(radio3); frame.getContentPane().add(jButton); frame.getContentPane().add(toggle); frame.getContentPane().add(checkBox); frame.getContentPane().add(radio1); frame.getContentPane().add(radio2); frame.getContentPane().add(radio3); frame.getContentPane().add(label); frame.setSize(200, 250); } public void show() { frame.show(); } public static void main(String[] args) { TestButtons tb = new TestButtons(); tb.show(); }} to kinglomei(可乐猫) : 好像不行呀!我试了好几次了? 谢谢后面的“按钮完整版”!F:\JAVA\javastudy\Simple.java:33: incompatible typesfound : java.lang.Objectrequired: javax.swing.JButton JButton b = e.getSource(); ^1 error 不好意思,需要转换一下类型。JButton b = (JButton)e.getSource(); 成功了!谢谢kinglomei(可乐猫) ! 小怪点的问题~在什么情况下 2个jdk的问题 在类里面 如何将回车去掉 我觉得在应用程序中,我们的应该尽量使class的方法和域不要成为static,因为static的方法和域在一进程当中是常驻内存,如果时间长了,会 JBUILERX打包时的问题(为什么没有把我在WEBMODULE下建的目录打包) 找工作的迷茫 [求]IBM的JRE下载地址(1.4)-200! 求救:算法问题 求JB5的激活码?????????????????????? java 如何获取指定ip地址的电脑上的盘符目录 j2ee的环境变量的配置问题,请求各位大哥帮忙。 JPanel,如何能使它有右侧的和底部的滚轴?
/**
* TestButtons.java
* @author Fancy
*/import javax.swing.*;
import java.awt.event.*;public class TestButtons { JFrame frame = new JFrame("Test Buttons");
JButton jButton = new JButton("JButton"); //按钮
JToggleButton toggle = new JToggleButton("Toggle Button"); //切换按钮
JCheckBox checkBox = new JCheckBox("Check Box"); //复选按钮
JRadioButton radio1 = new JRadioButton("Radio Button 1"); //单选按钮
JRadioButton radio2 = new JRadioButton("Radio Button 2");
JRadioButton radio3 = new JRadioButton("Radio Button 3");
JLabel label = new JLabel("Here is Status, look here."); //不是按钮,是静态文本 public TestButtons() {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new java.awt.FlowLayout()); /* 为一般按钮添加动作监听器 */
jButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
label.setText("You clicked jButton");
}
}); /* 为切换按钮添加动作监听器 */
toggle.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
JToggleButton toggle = (JToggleButton) ae.getSource();
if (toggle.isSelected()) {
label.setText("You selected Toggle Button");
} else {
label.setText("You deselected Toggle Button");
}
}
}); /* 为复选按钮添加条目监听器 */
checkBox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
JCheckBox cb = (JCheckBox) e.getSource();
label.setText("Selected Check Box is " + cb.isSelected());
}
}); /* 用一个按钮组对象包容一组单选按钮 */
ButtonGroup group = new ButtonGroup();
/* 生成一个新的动作监听器对象,备用 */
ActionListener al = new ActionListener() {
public void actionPerformed(ActionEvent ae) {
JRadioButton radio = (JRadioButton) ae.getSource();
if (radio == radio1) {
label.setText("You selected Radio Button 1");
} else if (radio == radio2) {
label.setText("You selected Radio Button 2");
} else {
label.setText("You selected Radio Button 3");
}
}
};
/* 为各单选按钮添加动作监听器 */
radio1.addActionListener(al);
radio2.addActionListener(al);
radio3.addActionListener(al);
/* 将单选按钮添加到按钮组中 */
group.add(radio1);
group.add(radio2);
group.add(radio3); frame.getContentPane().add(jButton);
frame.getContentPane().add(toggle);
frame.getContentPane().add(checkBox);
frame.getContentPane().add(radio1);
frame.getContentPane().add(radio2);
frame.getContentPane().add(radio3);
frame.getContentPane().add(label); frame.setSize(200, 250);
} public void show() {
frame.show();
} public static void main(String[] args) {
TestButtons tb = new TestButtons();
tb.show();
}}
好像不行呀!我试了好几次了?
谢谢后面的“按钮完整版”!
F:\JAVA\javastudy\Simple.java:33: incompatible types
found : java.lang.Object
required: javax.swing.JButton
JButton b = e.getSource();
^
1 error