java - 如何将 getSource() 与 JRadioButton 一起使用?

标签 java swing jbutton actionlistener

我需要先说明一下,我的老师不允许我们在类里面使用 IDE,所以我在文本板中执行此操作。我想单击 raido 按钮并更改“交通灯”颜色。如何使用 getSource() 与单选按钮交互?

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(2,1));
        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() {

        this.setLayout(new FlowLayout());
        JRadioButton jrbRed = new JRadioButton("Red", true);
        JRadioButton jrbYellow = new JRadioButton("Yellow");
        JRadioButton jrbGreen = new JRadioButton("Green");
        this.setBorder(BorderFactory.createLineBorder(Color.black));
        ButtonGroup group = new ButtonGroup();
        group.add(jrbRed);
        group.add(jrbYellow);
        group.add(jrbGreen);

        this.add(jrbRed);
        this.add(jrbYellow);
        this.add(jrbGreen);

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




            public void mouseClicked(MouseEvent e)
                {
                    if (e.getSource() == jrbRed){

                    }

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

                    }

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

                    }



                }

        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);

    }


}

最佳答案

不要将鼠标监听器与按钮(包括单选按钮)一起使用。请改用 Action 监听器。此外,要使任何监听器正常工作,必须首先将其添加到组件中。

关于java - 如何将 getSource() 与 JRadioButton 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9270460/

相关文章:

java - Graphics2D - 在 Graphics2D 对象上旋转形状

java - 设置drawString,使(0,0)位于绘图区域内

java - Java中的简单下拉菜单

java - 当我有 split 功能时,为什么要使用 StringTokenizer?

Java-Excel读取多行数据

java - 如何让我的按钮切换而不循环?

java - JButton 以编程方式显示工具提示 : actionMap. get ("postTip") 为空

java - JLabel 并通过 JLabel 的 setText(...) 方法交换其保存的文本

java - 通过 FileZilla 在 Linux 服务器上安装 Play Framework

java - Java中如何同时使用ByteArrayOutputStream和DataOutputStream?