1

我们不允许在课堂上使用 IDE,这是在文本板中完成的。我正在编写一个交通灯程序,当我单击相关的交通灯颜色时,它会填充颜色并使其看起来像该灯处于活动状态。

我目前无法显示带有单选按钮的第二个面板。我已经实例化并添加了它。

它们应该是单独的面板。

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;



public class Lab4Frame extends JFrame {

    Lab4Frame(){
        setTitle("Lab 4 - Application #1");
        Lab4Panel p = new Lab4Panel();
        Lab4RadioButtonPanel p2 = new Lab4RadioButtonPanel();
        setLayout(new GridLayout(1,2));
        add(p);
        add(p2);
    }

    public static void main(String[] args){

            Lab4Frame frame = new Lab4Frame();
            frame.setTitle("Lab4 Application # 1");
            frame.setLocationRelativeTo(null);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(600, 600);
            frame.setVisible(true);
    }

}

class Lab4RadioButtonPanel extends JPanel implements MouseListener {

    public Lab4RadioButtonPanel() {
        JRadioButton jrbRed = new JRadioButton("Red", true);
        JRadioButton jrbYellow = new JRadioButton("Yellow");
        JRadioButton jrbGreen = new JRadioButton("Green");
        ButtonGroup group = new ButtonGroup();
        group.add(jrbRed);
        group.add(jrbYellow);
        group.add(jrbGreen);

        jrbRed.setMnemonic('E');
        jrbGreen.setMnemonic('G');
        jrbYellow.setMnemonic('Y');
    }




            public void mouseClicked(MouseEvent e)
                {
                    /*if (e.getSource() == red){

                    }

                    else if (e.getSource() == yellow){

                    }

                    else if (e.getSource() == green){

                    }*/



                }

        public void mouseExited(MouseEvent e){}
        public void mouseReleased(MouseEvent e){}
        public void mousePressed(MouseEvent e){}
        public void mouseMoved(MouseEvent e){}
        public void mouseEntered(MouseEvent e){}
}

class Lab4Panel extends JPanel{


    public Lab4Panel(){
    }



    int height, width;
    int radius = 5;
    int x = -1;
    int y = -1;

    protected void paintComponent(Graphics g){
        if (x<0 || y<0) {
            x = getWidth() / 2 - radius;
            y = getHeight() / 2 - radius;
        }
        super.paintComponent(g);
        g.drawRect(x - 10,y - 90, 40, 120);
        //g.drawOval(x,y - 80, 4 * radius, 4 * radius);
        //g.drawOval(x,y - 40, 4 * radius, 4 * radius);
        //g.drawOval(x,y, 4 * radius, 4 * radius);
        g.drawRect(x - 5,y - 90, 40, 120);
        g.setColor(Color.RED);
        g.fillOval(x,y - 80, 4 * radius, 4 * radius);
        g.setColor(Color.YELLOW);
        g.fillOval(x,y - 40, 4 * radius, 4 * radius);
        g.setColor(Color.GREEN);
        g.fillOval(x,y, 4 * radius, 4 * radius);

    }


}
4

2 回答 2

3

JRadioButtons 永远不会添加到容器中。

于 2012-02-14T01:02:21.933 回答
1

来自javadoc

此类用于为一组按钮创建多重排除范围。使用相同的 ButtonGroup 对象创建一组按钮意味着“打开”其中一个按钮会关闭组中的所有其他按钮。

并且 ButtonGroup 不是从 Component 扩展而来的。ButtonGroup 正在“管理”添加按钮的状态。所以我们必须添加按钮而不是 ButtonGroup。类似于 Lab4RadioButtonPanel 类中的构造函数 Lab4RadioButtonPanel

public Lab4RadioButtonPanel()
{
   JRadioButton jrbRed = new JRadioButton("Red", true);
   JRadioButton jrbYellow = new JRadioButton("Yellow");
   JRadioButton jrbGreen = new JRadioButton("Green");
   ButtonGroup group = new ButtonGroup();
   group.add(jrbRed);
   group.add(jrbYellow);
   group.add(jrbGreen);
   add(jrbRed);
   add(jrbYellow);
   add(jrbGreen);

   jrbRed.setMnemonic('E');
   jrbGreen.setMnemonic('G');
   jrbYellow.setMnemonic('Y');
}
于 2012-02-14T01:44:54.440 回答