下面的一个java程序为何在eclipse中运行有错误(总是出现main函数错误)?各位大虾帮帮忙吧!
packagebasegraph;importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publiccl...
package basegraph;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class thingdeal1 extends JFrame{
//声明单选按钮,复选框和标签
private JRadioButton radio1,radio2,radio3;
private JCheckBox check1,check2,check3;
private JLabel label;
//单选按钮组对象
private ButtonGroup radiogroup=new ButtonGroup();
public thingdeal1(){
super("请你做出选择");
this.getContentPane().setLayout(new FlowLayout());
//复选框
check1=new JCheckBox("联想");
check2=new JCheckBox("苹果");
check3=new JCheckBox("华硕");
//单选按钮
radio1=new JRadioButton("超喜欢");
radio2=new JRadioButton("喜欢");
radio3=new JRadioButton("不喜欢");
//添加单选按钮到按钮组
radiogroup.add(radio1);
radiogroup.add(radio2);
radiogroup.add(radio3);
//添加复选框到窗口
this.getContentPane().add(check1);
this.getContentPane().add(check2);
this.getContentPane().add(check3);
//添加单选按钮到窗口
this.getContentPane().add(radio1);
this.getContentPane().add(radio2);
this.getContentPane().add(radio3);
//添加标签到窗口
this.getContentPane().add(label);
//注册事件监听器
check1.addItemListener(new listener1());
check2.addItemListener(new listener1());
check3.addItemListener(new listener1());
radio1.addItemListener(new listener2());
radio2.addItemListener(new listener2());
radio3.addItemListener(new listener2());
this.setSize(300,300);
this.setVisible(true);
}
public static void main(String args[]){
new thingdeal1();
}
private class listener1 implements ItemListener{
//实现itemstatechanged方法
public void itemStateChanged(ItemEvent e1){
if(e1.getSource()==check1){//获得事件源对象
if(e1.getStateChange()==ItemEvent.SELECTED){
label.setText("你选择了联想");
}else
label.setText("你取消了选择联想");
}
else if(e1.getSource()==check2){
if(e1.getStateChange()==ItemEvent.SELECTED){
label.setText("你选择了苹果");
}else
label.setText("你取消了选择苹果");
}
else if(e1.getSource()==check3){
if(e1.getStateChange()==ItemEvent.SELECTED){
label.setText("你选择了华硕");
}else
label.setText("你取消了选择华硕");
}
}
}
public class listener2 implements ItemListener{
public void itemStateChanged(ItemEvent e2){
if(e2.getSource()==radio1){
label.setText("你选择了超喜欢");
}
else if(e2.getSource()==radio2){
label.setText("你选择了喜欢");
}
else if(e2.getSource()==radio3){
label.setText("你选择了不喜欢");
}
}
}
} 展开
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class thingdeal1 extends JFrame{
//声明单选按钮,复选框和标签
private JRadioButton radio1,radio2,radio3;
private JCheckBox check1,check2,check3;
private JLabel label;
//单选按钮组对象
private ButtonGroup radiogroup=new ButtonGroup();
public thingdeal1(){
super("请你做出选择");
this.getContentPane().setLayout(new FlowLayout());
//复选框
check1=new JCheckBox("联想");
check2=new JCheckBox("苹果");
check3=new JCheckBox("华硕");
//单选按钮
radio1=new JRadioButton("超喜欢");
radio2=new JRadioButton("喜欢");
radio3=new JRadioButton("不喜欢");
//添加单选按钮到按钮组
radiogroup.add(radio1);
radiogroup.add(radio2);
radiogroup.add(radio3);
//添加复选框到窗口
this.getContentPane().add(check1);
this.getContentPane().add(check2);
this.getContentPane().add(check3);
//添加单选按钮到窗口
this.getContentPane().add(radio1);
this.getContentPane().add(radio2);
this.getContentPane().add(radio3);
//添加标签到窗口
this.getContentPane().add(label);
//注册事件监听器
check1.addItemListener(new listener1());
check2.addItemListener(new listener1());
check3.addItemListener(new listener1());
radio1.addItemListener(new listener2());
radio2.addItemListener(new listener2());
radio3.addItemListener(new listener2());
this.setSize(300,300);
this.setVisible(true);
}
public static void main(String args[]){
new thingdeal1();
}
private class listener1 implements ItemListener{
//实现itemstatechanged方法
public void itemStateChanged(ItemEvent e1){
if(e1.getSource()==check1){//获得事件源对象
if(e1.getStateChange()==ItemEvent.SELECTED){
label.setText("你选择了联想");
}else
label.setText("你取消了选择联想");
}
else if(e1.getSource()==check2){
if(e1.getStateChange()==ItemEvent.SELECTED){
label.setText("你选择了苹果");
}else
label.setText("你取消了选择苹果");
}
else if(e1.getSource()==check3){
if(e1.getStateChange()==ItemEvent.SELECTED){
label.setText("你选择了华硕");
}else
label.setText("你取消了选择华硕");
}
}
}
public class listener2 implements ItemListener{
public void itemStateChanged(ItemEvent e2){
if(e2.getSource()==radio1){
label.setText("你选择了超喜欢");
}
else if(e2.getSource()==radio2){
label.setText("你选择了喜欢");
}
else if(e2.getSource()==radio3){
label.setText("你选择了不喜欢");
}
}
}
} 展开
2个回答
展开全部
问题出在Jlabel对像,添加一个label=new Jlabel() 就Ok了啊!
我试验了一下就成功了哦。
希望对你有帮助!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class t extends JFrame{
//声明单选按钮,复选框和标签
private JRadioButton radio1,radio2,radio3;
private JCheckBox check1,check2,check3;
private JLabel label;
//单选按钮组对象
private ButtonGroup radiogroup=new ButtonGroup();
public t(){
super("请你做出选择");
this.getContentPane().setLayout(new FlowLayout());
//复选框
check1=new JCheckBox("联想");
check2=new JCheckBox("苹果");
check3=new JCheckBox("华硕");
//单选按钮
radio1=new JRadioButton("超喜欢");
radio2=new JRadioButton("喜欢");
radio3=new JRadioButton("不喜欢");
//添加单选按钮到按钮组
radiogroup.add(radio1);
radiogroup.add(radio2);
radiogroup.add(radio3);
//添加复选框到窗口
this.getContentPane().add(check1);
this.getContentPane().add(check2);
this.getContentPane().add(check3);
//添加单选按钮到窗口
this.getContentPane().add(radio1);
this.getContentPane().add(radio2);
this.getContentPane().add(radio3);
//添加标签到窗口
label=new JLabel("");
this.getContentPane().add(label);
//注册事件监听器
check1.addItemListener(new listener1());
check2.addItemListener(new listener1());
check3.addItemListener(new listener1());
radio1.addItemListener(new listener2());
radio2.addItemListener(new listener2());
radio3.addItemListener(new listener2());
this.setSize(300,300);
this.setVisible(true);
}
public static void main(String args[]){
new t();
}
private class listener1 implements ItemListener{
//实现itemstatechanged方法
public void itemStateChanged(ItemEvent e1){
if(e1.getSource()==check1){//获得事件源对象
if(e1.getStateChange()==ItemEvent.SELECTED){
label.setText("你选择了联想");
}else
label.setText("你取消了选择联想");
}
else if(e1.getSource()==check2){
if(e1.getStateChange()==ItemEvent.SELECTED){
label.setText("你选择了苹果");
}else
label.setText("你取消了选择苹果");
}
else if(e1.getSource()==check3){
if(e1.getStateChange()==ItemEvent.SELECTED){
label.setText("你选择了华硕");
}else
label.setText("你取消了选择华硕");
}
}
}
public class listener2 implements ItemListener{
public void itemStateChanged(ItemEvent e2){
if(e2.getSource()==radio1){
label.setText("你选择了超喜欢");
}
else if(e2.getSource()==radio2){
label.setText("你选择了喜欢");
}
else if(e2.getSource()==radio3){
label.setText("你选择了不喜欢");
}
}
}
}
我试验了一下就成功了哦。
希望对你有帮助!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class t extends JFrame{
//声明单选按钮,复选框和标签
private JRadioButton radio1,radio2,radio3;
private JCheckBox check1,check2,check3;
private JLabel label;
//单选按钮组对象
private ButtonGroup radiogroup=new ButtonGroup();
public t(){
super("请你做出选择");
this.getContentPane().setLayout(new FlowLayout());
//复选框
check1=new JCheckBox("联想");
check2=new JCheckBox("苹果");
check3=new JCheckBox("华硕");
//单选按钮
radio1=new JRadioButton("超喜欢");
radio2=new JRadioButton("喜欢");
radio3=new JRadioButton("不喜欢");
//添加单选按钮到按钮组
radiogroup.add(radio1);
radiogroup.add(radio2);
radiogroup.add(radio3);
//添加复选框到窗口
this.getContentPane().add(check1);
this.getContentPane().add(check2);
this.getContentPane().add(check3);
//添加单选按钮到窗口
this.getContentPane().add(radio1);
this.getContentPane().add(radio2);
this.getContentPane().add(radio3);
//添加标签到窗口
label=new JLabel("");
this.getContentPane().add(label);
//注册事件监听器
check1.addItemListener(new listener1());
check2.addItemListener(new listener1());
check3.addItemListener(new listener1());
radio1.addItemListener(new listener2());
radio2.addItemListener(new listener2());
radio3.addItemListener(new listener2());
this.setSize(300,300);
this.setVisible(true);
}
public static void main(String args[]){
new t();
}
private class listener1 implements ItemListener{
//实现itemstatechanged方法
public void itemStateChanged(ItemEvent e1){
if(e1.getSource()==check1){//获得事件源对象
if(e1.getStateChange()==ItemEvent.SELECTED){
label.setText("你选择了联想");
}else
label.setText("你取消了选择联想");
}
else if(e1.getSource()==check2){
if(e1.getStateChange()==ItemEvent.SELECTED){
label.setText("你选择了苹果");
}else
label.setText("你取消了选择苹果");
}
else if(e1.getSource()==check3){
if(e1.getStateChange()==ItemEvent.SELECTED){
label.setText("你选择了华硕");
}else
label.setText("你取消了选择华硕");
}
}
}
public class listener2 implements ItemListener{
public void itemStateChanged(ItemEvent e2){
if(e2.getSource()==radio1){
label.setText("你选择了超喜欢");
}
else if(e2.getSource()==radio2){
label.setText("你选择了喜欢");
}
else if(e2.getSource()==radio3){
label.setText("你选择了不喜欢");
}
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询