简体   繁体   English

单击我的JButton,但在使用ActionListener单击JFrame中的另一个按钮之前,没有得到显示的操作?

[英]Clicking my JButton but not getting the action shown before i click another button in my JFrame using ActionListener?

I've just started my course Programming 2, and its quite a jump from the introducting course we had before. 我刚刚开始了编程2课程,它比我们以前的入门课程有了很大的进步。 I am to make a program that has three buttons "red", "blue", "green" and when I click "red" a red square is to appear. 我要制作一个具有三个按钮“红色”,“蓝色”,“绿色”的程序,当我单击“红色”时,将出现一个红色方块。 When I click another button, there are more squares, and the color of the square depends on the button I press. 当我单击另一个按钮时,会有更多的正方形,正方形的颜色取决于我按下的按钮。 My problem is, that when I press "red", I have to press another button before the red square appears. 我的问题是,当我按“红色”时,必须在红色方块出现之前按另一个按钮。 So in reality it will seem like the buttons I press are connected to a wrong action. 因此,实际上,我按下的按钮似乎连接了错误的动作。 It's really hard to describe, but it's as is there is a "delay of one action".. Can you provide a novice like me any assistance? 真的很难描述,但是就像“一动作的延迟”一样。您能为像我这样的新手提供任何帮助吗? I have tree classes in total: SquareIcon, CompositeIcon, and a tester with the frame. 我总共有树类:SquareIcon,CompositeIcon和带有框架的测试器。 It should only be necessary to look at the last class I pasted here. 只需要查看我在这里粘贴的最后一堂课。

import javax.swing.*;
import java.awt.*;
import java.awt.geom.Rectangle2D;

/**
 * Made by   Rasmus
 * Version   13-11-2014.
 */
public class SquareIcon implements Icon{
    private int size;
    private Color color;

public SquareIcon(int size, Color color) {
    this.size = size;
    this.color = color;
}

public void paintIcon(Component c, Graphics g, int x, int y) {
    Graphics2D g2 = (Graphics2D) g;
    Rectangle2D rec = new Rectangle2D.Double(x, y, size, size);
    g2.fill(rec);
    g2.setColor(color);
}

public int getIconWidth() {
    return size;
}

public int getIconHeight() {
    return size;
}

} }

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;

/**
 * Made by   Rasmus
 * Version   13-11-2014.
 */
public class CompositeIcon implements Icon {
    private ArrayList<Icon> icons;

public CompositeIcon() {
    icons = new ArrayList<Icon>();
}

public void paintIcon(Component c, Graphics g, int x, int y) {
    for (Icon i : icons) {
        i.paintIcon(c, g, x, y);
        x += i.getIconWidth();
    }
}

public int getIconWidth() {
    int width = 0;
    for (Icon i : icons) {
        width += i.getIconWidth();
    }
    return width;
}

public int getIconHeight() {
    int height = 0;
    for (Icon i : icons) {
        if (i.getIconHeight() > height) {
            height = i.getIconHeight();
        }
    }
    return height;
}

public void addIcon(Icon i) {
    icons.add(i);
}
}

And the last: 最后:

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

/**
 * Made by   Rasmus
 * Version   13-11-2014.
 */
public class FrameTest implements ActionListener {
    public static void main(String[] args) {
        final JFrame frame = new JFrame();
        frame.setLayout(new FlowLayout());

    JButton redButton = new JButton("Rød");
    JButton greenButton = new JButton("Grøn");
    JButton blueButton = new JButton("Blå");
    frame.add(redButton);
    frame.add(greenButton);
    frame.add(blueButton);

    final CompositeIcon ci = new CompositeIcon();
    final SquareIcon red = new SquareIcon(50, Color.RED);
    final SquareIcon green = new SquareIcon(50, Color.GREEN);
    final SquareIcon blue = new SquareIcon(50, Color.BLUE);
    JLabel squareLabel = new JLabel(ci);
    frame.add(squareLabel);

    redButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            ci.addIcon(red);
            frame.pack();
            frame.repaint();
        }
    });
    greenButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            ci.addIcon(green);
            frame.pack();
            frame.repaint();
        }
    });
    blueButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            ci.addIcon(blue);
            frame.pack();
            frame.repaint();
        }
    });
    frame.setSize(250, 75);
    frame.setVisible(true);
}


public void actionPerformed(ActionEvent e) {

}
}

Two things I would suggest: 我建议两件事:

First, I would advise that you delete the last action performed block. 首先,我建议您删除最后执行的操作块。 That block may be causing the error. 该块可能导致错误。

Second, if that isn't causing the error, try inserting breakpoints or print statements to see when it enter the action performed loop, then you can work from there. 其次,如果这不是导致错误的原因,请尝试插入断点或打印语句以查看其何时进入执行动作的循环,然后您可以从那里开始工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM