ActionListener 缺少什么?

发布于 2025-01-05 10:01:19 字数 4018 浏览 1 评论 0原文

我需要先说明一下,我们不允许在课堂上使用 IDE。我显然错过了一些重要的事情。我正在编写一个程序,应该允许用户单击单选按钮,然后将“光”更改为该颜色。我以为我正确连接了听众,但显然我忘记了一些事情。有人可以帮我解决这个问题吗?

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

public class Lab4Frame extends JFrame {
    //public boolean red, yellow, green;
    Lab4Frame(){
        this.setLayout(new BorderLayout());
        setTitle("Lab 4 - Application #1");
        Lab4Panel p = new Lab4Panel();
        Lab4RadioButtonPanel p2 = new Lab4RadioButtonPanel();

        add(p, BorderLayout.CENTER);
        add(p2, BorderLayout.SOUTH);
    }

    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 {
        Lab4Panel canvas = new Lab4Panel();
        //Lab4RadioButtonPanel canvas2 = new Lab4RadioButtonPanel();

    public Lab4RadioButtonPanel() {
        boolean red, green, yellow;
        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.addActionListener(new RedListener(canvas));
        jrbYellow.addActionListener(new YellowListener(canvas));
        jrbGreen.addActionListener(new GreenListener(canvas));
    }
}

class Lab4Panel extends JPanel{

    public Lab4Panel(){}

    boolean red, green, yellow;
    int radius = 5;
    int x = -1;
    int y = -1;

    public void setRed(){
        red = true;
        repaint();
    }

    public void setYellow(){
        yellow = true;
        repaint();
    }

    public void setGreen(){
        green = true;
        repaint();
    }

    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.drawRect(x - 5,y - 90, 40, 120);
        g.setColor(Color.RED);
        g.drawOval(x,y - 80, 4 * radius, 4 * radius);
        g.setColor(Color.YELLOW);
        g.drawOval(x,y - 40, 4 * radius, 4 * radius);
        g.setColor(Color.GREEN);
        g.drawOval(x,y, 4 * radius, 4 * radius);


        if(red){
            g.setColor(Color.RED);
            g.fillOval(x,y - 80, 4 * radius, 4 * radius);
            repaint();
        }

        else if (yellow){
            g.setColor(Color.YELLOW);
            g.fillOval(x,y - 40, 4 * radius, 4 * radius);
            repaint();
        }

        if(green){
            g.setColor(Color.GREEN);
            g.fillOval(x,y, 4 * radius, 4 * radius);
            repaint();
        }

    }


}

class RedListener implements ActionListener{
    //private Lab4RadioButtonPanel canvas;
    private Lab4Panel canvas;

    RedListener(Lab4Panel canvas) {
     this.canvas = canvas;
    }

    public void actionPerformed(ActionEvent e){
        canvas.setRed();
    }
}

class YellowListener implements ActionListener{
    private Lab4Panel canvas;

    YellowListener(Lab4Panel canvas) {
     this.canvas = canvas;
    }

    public void actionPerformed(ActionEvent e){
        canvas.setYellow();
    }
}

class GreenListener implements ActionListener{
    //private Lab4RadioButtonPanel canvas;
    private Lab4Panel canvas;

    GreenListener(Lab4Panel canvas) {
     this.canvas = canvas;
    }

    public void actionPerformed(ActionEvent e){
        canvas.setGreen();
    }
}

I need to preface this with we are not allowed to use IDE's in class. I am obviously missing something important. I am writing a program that should allow a user to click a radio button then change the "light" to that color. I thought I connected the listeners correctly but obviously I am forgetting something. Can someone help me with this?

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

public class Lab4Frame extends JFrame {
    //public boolean red, yellow, green;
    Lab4Frame(){
        this.setLayout(new BorderLayout());
        setTitle("Lab 4 - Application #1");
        Lab4Panel p = new Lab4Panel();
        Lab4RadioButtonPanel p2 = new Lab4RadioButtonPanel();

        add(p, BorderLayout.CENTER);
        add(p2, BorderLayout.SOUTH);
    }

    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 {
        Lab4Panel canvas = new Lab4Panel();
        //Lab4RadioButtonPanel canvas2 = new Lab4RadioButtonPanel();

    public Lab4RadioButtonPanel() {
        boolean red, green, yellow;
        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.addActionListener(new RedListener(canvas));
        jrbYellow.addActionListener(new YellowListener(canvas));
        jrbGreen.addActionListener(new GreenListener(canvas));
    }
}

class Lab4Panel extends JPanel{

    public Lab4Panel(){}

    boolean red, green, yellow;
    int radius = 5;
    int x = -1;
    int y = -1;

    public void setRed(){
        red = true;
        repaint();
    }

    public void setYellow(){
        yellow = true;
        repaint();
    }

    public void setGreen(){
        green = true;
        repaint();
    }

    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.drawRect(x - 5,y - 90, 40, 120);
        g.setColor(Color.RED);
        g.drawOval(x,y - 80, 4 * radius, 4 * radius);
        g.setColor(Color.YELLOW);
        g.drawOval(x,y - 40, 4 * radius, 4 * radius);
        g.setColor(Color.GREEN);
        g.drawOval(x,y, 4 * radius, 4 * radius);


        if(red){
            g.setColor(Color.RED);
            g.fillOval(x,y - 80, 4 * radius, 4 * radius);
            repaint();
        }

        else if (yellow){
            g.setColor(Color.YELLOW);
            g.fillOval(x,y - 40, 4 * radius, 4 * radius);
            repaint();
        }

        if(green){
            g.setColor(Color.GREEN);
            g.fillOval(x,y, 4 * radius, 4 * radius);
            repaint();
        }

    }


}

class RedListener implements ActionListener{
    //private Lab4RadioButtonPanel canvas;
    private Lab4Panel canvas;

    RedListener(Lab4Panel canvas) {
     this.canvas = canvas;
    }

    public void actionPerformed(ActionEvent e){
        canvas.setRed();
    }
}

class YellowListener implements ActionListener{
    private Lab4Panel canvas;

    YellowListener(Lab4Panel canvas) {
     this.canvas = canvas;
    }

    public void actionPerformed(ActionEvent e){
        canvas.setYellow();
    }
}

class GreenListener implements ActionListener{
    //private Lab4RadioButtonPanel canvas;
    private Lab4Panel canvas;

    GreenListener(Lab4Panel canvas) {
     this.canvas = canvas;
    }

    public void actionPerformed(ActionEvent e){
        canvas.setGreen();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深府石板幽径 2025-01-12 10:01:19

将下面的代码更改为:

class Lab4RadioButtonPanel extends JPanel {
    Lab4Panel canvas = new Lab4Panel();
    //Lab4RadioButtonPanel canvas2 = new Lab4RadioButtonPanel();

    public Lab4RadioButtonPanel() {

并将

class Lab4RadioButtonPanel extends JPanel {
    Lab4Panel canvas;
    //Lab4RadioButtonPanel canvas2 = new Lab4RadioButtonPanel();

    public Lab4RadioButtonPanel(Lab4Panel canvas) {
        this.canvas = canvas;

下面的代码更改

Lab4Panel p = new Lab4Panel();
Lab4RadioButtonPanel p2 = new Lab4RadioButtonPanel();

为:

Lab4Panel p = new Lab4Panel();
Lab4RadioButtonPanel p2 = new Lab4RadioButtonPanel(p);

我得到如下图所示的结果,我想您会明白您的程序出了什么问题:
在此处输入图像描述

Bug 很明显:您显示的面板是在 main 函数中构造的,但是您根据单击单选按钮是在 Lab4RadioButtonPanel 类中构造的,它们不指向同一个对象,这就是问题所在。

Change the code below:

class Lab4RadioButtonPanel extends JPanel {
    Lab4Panel canvas = new Lab4Panel();
    //Lab4RadioButtonPanel canvas2 = new Lab4RadioButtonPanel();

    public Lab4RadioButtonPanel() {

to

class Lab4RadioButtonPanel extends JPanel {
    Lab4Panel canvas;
    //Lab4RadioButtonPanel canvas2 = new Lab4RadioButtonPanel();

    public Lab4RadioButtonPanel(Lab4Panel canvas) {
        this.canvas = canvas;

and change the code below:

Lab4Panel p = new Lab4Panel();
Lab4RadioButtonPanel p2 = new Lab4RadioButtonPanel();

to:

Lab4Panel p = new Lab4Panel();
Lab4RadioButtonPanel p2 = new Lab4RadioButtonPanel(p);

I get the result like figure below, I think you'll get what's wrong with your program:
enter image description here

The bug is clear: the panel you display is constructed in main function, but the panel you change according to clicking a radio button is constructed in class Lab4RadioButtonPanel, they don't point to the same object, this is the problem.

冰雪梦之恋 2025-01-12 10:01:19

repaint() 之后,不会调用 paintComponent 调用。有两个 Lab4Panel。您设置听众的那个不是您正在绘制的那个。

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

class Lab4Frame extends JFrame
{
   Lab4Frame()
   {
      this.setLayout( new BorderLayout() );
      setTitle( "Lab 4 - Application #1" );
      Lab4Panel p = new Lab4Panel();
      Lab4RadioButtonPanel p2 = new Lab4RadioButtonPanel(p);

      add( p, BorderLayout.CENTER );
      add( p2, BorderLayout.SOUTH );
   }

   ...
}

class Lab4RadioButtonPanel extends JPanel
{
    Lab4Panel canvas;

   public Lab4RadioButtonPanel(Lab4Panel p)
   {
      canvas = p;
      this.setLayout( new FlowLayout() );
      ...
   }
}

The paintComponent call is not being called after your repaint()s. There are two Lab4Panels. The one you set the listeners on is not the one you are drawing.

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

class Lab4Frame extends JFrame
{
   Lab4Frame()
   {
      this.setLayout( new BorderLayout() );
      setTitle( "Lab 4 - Application #1" );
      Lab4Panel p = new Lab4Panel();
      Lab4RadioButtonPanel p2 = new Lab4RadioButtonPanel(p);

      add( p, BorderLayout.CENTER );
      add( p2, BorderLayout.SOUTH );
   }

   ...
}

class Lab4RadioButtonPanel extends JPanel
{
    Lab4Panel canvas;

   public Lab4RadioButtonPanel(Lab4Panel p)
   {
      canvas = p;
      this.setLayout( new FlowLayout() );
      ...
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文