java单选按钮
看看这段代码哪里错?编译的时候问什么说找不到符号cb1?importjavax.swing.*;importjava.awt.*;importjava.awt.event...
看看这段代码哪里错?编译的时候问什么说找不到符号cb1?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.applet.*;
import java.sql.*;
public class Test extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
private JPanel jPanel;
private JButton jButton1,jButton2,jButton3;
private JTextArea ta1;
private JTextField tf1;
public Test(String title) {
super(title);
init();
}
private void init() {
jPanel=new JPanel();
jPanel.setLayout(new FlowLayout());
ta1=new JTextArea();
tf1=new JTextField(20);
jButton1=new JButton("查询记录");
jButton2=new JButton("添加用户");
jButton3=new JButton("添加记录");
jButton1.addActionListener(this);
jButton2.addActionListener(this);
jButton3.addActionListener(this);
CheckboxGroup g = new CheckboxGroup();
Checkbox cb1,cb2;
cb1 = new Checkbox("1", g, false);
cb2 = new Checkbox("2", g, true);
jPanel.add(ta1);
jPanel.add(jButton1);
jPanel.add(jButton2);
jPanel.add(jButton3);
jPanel.add(tf1);
jPanel.add(cb1);
jPanel.add(cb2);
this.add(jPanel);
this.setSize(300,700);
this.setResizable(false);
this.setVisible(true);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(final WindowEvent e) {
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(jButton1)){
if(cb1.isSelected()){
System.out.println("1");
}
else{
System.out.println("2");
}
}
}
}
我明白自己错在哪里啦!谢谢,不过还有一个问题 就是那个isSelected(),说找不到这个方法。
我的本意是判断两个单选按钮选择了哪一个,选了1就输出1,选的是2就输出2,麻烦帮我看看怎么弄?多给10分 展开
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.applet.*;
import java.sql.*;
public class Test extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
private JPanel jPanel;
private JButton jButton1,jButton2,jButton3;
private JTextArea ta1;
private JTextField tf1;
public Test(String title) {
super(title);
init();
}
private void init() {
jPanel=new JPanel();
jPanel.setLayout(new FlowLayout());
ta1=new JTextArea();
tf1=new JTextField(20);
jButton1=new JButton("查询记录");
jButton2=new JButton("添加用户");
jButton3=new JButton("添加记录");
jButton1.addActionListener(this);
jButton2.addActionListener(this);
jButton3.addActionListener(this);
CheckboxGroup g = new CheckboxGroup();
Checkbox cb1,cb2;
cb1 = new Checkbox("1", g, false);
cb2 = new Checkbox("2", g, true);
jPanel.add(ta1);
jPanel.add(jButton1);
jPanel.add(jButton2);
jPanel.add(jButton3);
jPanel.add(tf1);
jPanel.add(cb1);
jPanel.add(cb2);
this.add(jPanel);
this.setSize(300,700);
this.setResizable(false);
this.setVisible(true);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(final WindowEvent e) {
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(jButton1)){
if(cb1.isSelected()){
System.out.println("1");
}
else{
System.out.println("2");
}
}
}
}
我明白自己错在哪里啦!谢谢,不过还有一个问题 就是那个isSelected(),说找不到这个方法。
我的本意是判断两个单选按钮选择了哪一个,选了1就输出1,选的是2就输出2,麻烦帮我看看怎么弄?多给10分 展开
4个回答
展开全部
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.applet.*;
import java.sql.*;
public class Test extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
private JPanel jPanel;
private JButton jButton1,jButton2,jButton3;
private JTextArea ta1;
private JTextField tf1;
private Checkbox cb1,cb2;
private CheckboxGroup g;
public Test(String title) {
super(title);
init();
}
private void init() {
jPanel=new JPanel();
jPanel.setLayout(new FlowLayout());
ta1=new JTextArea();
tf1=new JTextField(20);
jButton1=new JButton("查询记录");
jButton2=new JButton("添加用户");
jButton3=new JButton("添加记录");
jButton1.addActionListener(this);
jButton2.addActionListener(this);
jButton3.addActionListener(this);
g = new CheckboxGroup();
cb1 = new Checkbox("1", g, false);
cb2 = new Checkbox("2", g, true);
jPanel.add(ta1);
jPanel.add(jButton1);
jPanel.add(jButton2);
jPanel.add(jButton3);
jPanel.add(tf1);
jPanel.add(cb1);
jPanel.add(cb2);
this.add(jPanel);
this.setSize(300,700);
this.setResizable(false);
this.setVisible(true);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(final WindowEvent e) {
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(jButton1)){
if(cb1.isSelected()){
System.out.println("1");
}
else{
System.out.println("2");
}
}
}
}
给你做了小小的修改,你再试试
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.applet.*;
import java.sql.*;
public class Test extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
private JPanel jPanel;
private JButton jButton1,jButton2,jButton3;
private JTextArea ta1;
private JTextField tf1;
private Checkbox cb1,cb2;
private CheckboxGroup g;
public Test(String title) {
super(title);
init();
}
private void init() {
jPanel=new JPanel();
jPanel.setLayout(new FlowLayout());
ta1=new JTextArea();
tf1=new JTextField(20);
jButton1=new JButton("查询记录");
jButton2=new JButton("添加用户");
jButton3=new JButton("添加记录");
jButton1.addActionListener(this);
jButton2.addActionListener(this);
jButton3.addActionListener(this);
g = new CheckboxGroup();
cb1 = new Checkbox("1", g, false);
cb2 = new Checkbox("2", g, true);
jPanel.add(ta1);
jPanel.add(jButton1);
jPanel.add(jButton2);
jPanel.add(jButton3);
jPanel.add(tf1);
jPanel.add(cb1);
jPanel.add(cb2);
this.add(jPanel);
this.setSize(300,700);
this.setResizable(false);
this.setVisible(true);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(final WindowEvent e) {
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(jButton1)){
if(cb1.isSelected()){
System.out.println("1");
}
else{
System.out.println("2");
}
}
}
}
给你做了小小的修改,你再试试
展开全部
cb1 是局部变量
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(jButton1)){
if(cb1.isSelected()){
System.out.println("1");
}
else{
System.out.println("2");
}
}
}
这里当然找不到了
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(jButton1)){
if(cb1.isSelected()){
System.out.println("1");
}
else{
System.out.println("2");
}
}
}
这里当然找不到了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
因为你在INIT方法中定义的CB1,在actionPerformed肯定拿不到得啊。
你可以把CB1作为全局变量
你可以把CB1作为全局变量
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
能看看报的错误就更好了!!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询