你的 display 变量没有定义

解决方案 »

  1.   

    你需要将 JLabel display=new JLabel("DEMO1"); 这句话
    放到 Demo1 类成员定义部分
      

  2.   

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;public class Demo1 extends JPanel
    {   JLable display;
    String teacherSting="老师";
    String studentSting="学生";
    String manageSting="管理员";
    public Demo1()
    {   //创建单选按钮的对象

    JRadioButton teacherButton=new JRadioButton("老师");
    teacherButton.setMnemonic(KeyEvent.VK_T);
    teacherButton.setActionCommand(teacherSting);

    JRadioButton studentButton=new JRadioButton("学生");
    studentButton.setMnemonic(KeyEvent.VK_S);
    studentButton.setActionCommand(studentSting);

    JRadioButton manageButton=new JRadioButton("管理员");
    manageButton.setMnemonic(KeyEvent.VK_M);
    manageButton.setActionCommand(manageSting);

    //将单选按钮组成一组,建立单选关系
    ButtonGroup group=new ButtonGroup();
    group.add(teacherButton);
    group.add(studentButton);
    group.add(manageButton);

    //为按钮添加事件监听器
    RadioListener myListener=new RadioListener();
    teacherButton.addActionListener(myListener);
    studentButton.addActionListener(myListener);
    manageButton.addActionListener(myListener);
    //建立标签对象
    JLabel display=new JLabel("DEMO1");
    display.setPreferredSize(new Dimension(180,60));

    JPanel radioPanel=new JPanel();
    radioPanel.setLayout(new GridLayout(0,1));
        radioPanel.add(teacherButton);
        radioPanel.add(studentButton);
        radioPanel.add(manageButton);
        
    setLayout(new BorderLayout());
    add(radioPanel,BorderLayout.WEST);
    add(display,BorderLayout.CENTER);
    setBorder(BorderFactory.createEmptyBorder(20,20,20,0));
    }
    class RadioListener implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {
                 display.setText("你选中了"+e.getActionCommand()+".");
    }
    }

    public static void main(String args[])
    {
    JFrame frame=new JFrame("DEMO1");
    frame.addWindowListener(new WindowAdapter()
    {
    public void windowClosing(WindowEvent e)
    {
    System.exit(0);
    }
    });
    frame.getContentPane().add(new Demo1(),BorderLayout.CENTER);
    frame.pack();
    frame.setVisible(true);
    }
    }
    --------------------------------------------------D:\chengji>javac Demo1.java
    Demo1.java:6: cannot resolve symbol
    symbol  : class JLable
    location: class Demo1
    {   JLable display;
        ^
    1 error
    这样的错误
    还有你的第三句话我们明白
    不好意思
    楼上~~~~~~~~~~~~~~~~
      

  3.   

    是 JLabel 不是 JLable
    在函数内定义的变量,当函数返后便作废
    所以 GUI 组件应该定义为类成员
      

  4.   

    谢谢啊
    不过解释之后
    当我选一个时
    dos里出现很多异常
    我复制不了啊
    急啊~~~
      

  5.   

    将你的组件全部定义为类成员, 然后用构造函数进行初试化
    例如
    public class Demo1 extends JPanel {
        private JLabel display;
        ... ...
        public Demo1() {
            ... ...
            display = new JLabel("DEMO1");
            ... ...
        }
        ... ...
    }
      

  6.   

    再帮你一把import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;public class Demo1 extends JPanel
    {   JLabel display;
    String teacherSting="老师";
    String studentSting="学生";
    String manageSting="管理员";
    public Demo1()
    {   //创建单选按钮的对象

    JRadioButton teacherButton=new JRadioButton("老师");
    teacherButton.setMnemonic(KeyEvent.VK_T);
    teacherButton.setActionCommand(teacherSting);

    JRadioButton studentButton=new JRadioButton("学生");
    studentButton.setMnemonic(KeyEvent.VK_S);
    studentButton.setActionCommand(studentSting);

    JRadioButton manageButton=new JRadioButton("管理员");
    manageButton.setMnemonic(KeyEvent.VK_M);
    manageButton.setActionCommand(manageSting);

    //将单选按钮组成一组,建立单选关系
    ButtonGroup group=new ButtonGroup();
    group.add(teacherButton);
    group.add(studentButton);
    group.add(manageButton);

    //为按钮添加事件监听器
    RadioListener myListener=new RadioListener();
    teacherButton.addActionListener(myListener);
    studentButton.addActionListener(myListener);
    manageButton.addActionListener(myListener);
    //建立标签对象
    display=new JLabel("DEMO1");
    display.setPreferredSize(new Dimension(180,60));

    JPanel radioPanel=new JPanel();
    radioPanel.setLayout(new GridLayout(0,1));
        radioPanel.add(teacherButton);
        radioPanel.add(studentButton);
        radioPanel.add(manageButton);
        
    setLayout(new BorderLayout());
    add(radioPanel,BorderLayout.WEST);
    add(display,BorderLayout.CENTER);
    setBorder(BorderFactory.createEmptyBorder(20,20,20,0));
    }
    class RadioListener implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {
                 display.setText("你选中了"+e.getActionCommand()+".");
                 
    }
    }

    public static void main(String args[])
    {
    JFrame frame=new JFrame("DEMO1");
    frame.addWindowListener(new WindowAdapter()
    {
    public void windowClosing(WindowEvent e)
    {
    System.exit(0);
    }
    });
    frame.getContentPane().add(new Demo1(),BorderLayout.CENTER);
    frame.pack();
    frame.setVisible(true);
    }
    }
      

  7.   

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;public class Demo1 extends JPanel
    {
    String teacherSting="老师";
    String studentSting="学生";
    String manageSting="管理员";

    JLabel display=new JLabel("DEMO1");
    public Demo1()
    {   //创建单选按钮的对象

    JRadioButton teacherButton=new JRadioButton("老师");
    teacherButton.setMnemonic(KeyEvent.VK_T);
    teacherButton.setActionCommand(teacherSting);

    JRadioButton studentButton=new JRadioButton("学生");
    studentButton.setMnemonic(KeyEvent.VK_S);
    studentButton.setActionCommand(studentSting);

    JRadioButton manageButton=new JRadioButton("管理员");
    manageButton.setMnemonic(KeyEvent.VK_M);
    manageButton.setActionCommand(manageSting);

    //将单选按钮组成一组,建立单选关系
    ButtonGroup group=new ButtonGroup();
    group.add(teacherButton);
    group.add(studentButton);
    group.add(manageButton);

    //为按钮添加事件监听器
    RadioListener myListener=new RadioListener();
    teacherButton.addActionListener(myListener);
    studentButton.addActionListener(myListener);
    manageButton.addActionListener(myListener);
    //建立标签对象

    display.setPreferredSize(new Dimension(180,60));

    JPanel radioPanel=new JPanel();
    radioPanel.setLayout(new GridLayout(0,1));
             radioPanel.add(teacherButton);
             radioPanel.add(studentButton);
             radioPanel.add(manageButton);
        
    setLayout(new BorderLayout());
    add(radioPanel,BorderLayout.WEST);
    add(display,BorderLayout.CENTER);
    setBorder(BorderFactory.createEmptyBorder(20,20,20,0));
    }
    class RadioListener implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {
                 display.setText("你选中了"+e.getActionCommand()+".");
    }
    }

    public static void main(String args[])
    {
    JFrame frame=new JFrame("DEMO1");
    frame.addWindowListener(new WindowAdapter()
    {
    public void windowClosing(WindowEvent e)
    {
    System.exit(0);
    }
    });
    frame.getContentPane().add(new Demo1(),BorderLayout.CENTER);
    frame.pack();
    frame.setVisible(true);
    }
    }这个我编译过,绝对没问题