我无法获得下面链接的代码来完全执行我想要的操作。

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

public class Gui {

    static JFrame frame;
    static JLabel label;

    public static void main (String[] args) {
        Gui gui = new Gui();
        gui.go();
    }

    public void go () {
        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400,300);
        frame.setVisible(true);

        MyDrawPanel panel = new MyDrawPanel();
        frame.getContentPane().add(BorderLayout.CENTER, panel);

        label = new JLabel("I'm a label");
        frame.getContentPane().add(BorderLayout.WEST, label);

        JButton colorButton = new JButton("Change Colors");
        ColorButtonListener colorButtonListener = new ColorButtonListener();
        colorButton.addActionListener(colorButtonListener);
        frame.getContentPane().add(BorderLayout.SOUTH, colorButton);

        JButton labelButton = new JButton("Change Label");
        LabelButtonListener labelButtonListener = new LabelButtonListener();
        labelButton.addActionListener(labelButtonListener);
        frame.getContentPane().add(BorderLayout.EAST, labelButton);
    }
}

class ColorButtonListener implements ActionListener {
    JFrame frame = Gui.frame;
    public void actionPerformed (ActionEvent event) {
        frame.repaint();
    }
}

class LabelButtonListener implements ActionListener {
    JLabel label = Gui.label;
    public void actionPerformed (ActionEvent event) {
        if (label.getText() == "That hurt") {
            label.setText("I'm a label");
        } else {
            label.setText("That hurt");
        }
    }
}

class MyDrawPanel extends JPanel {
    public void paintComponent (Graphics g) {
        Graphics2D g2d = (Graphics2D) g;

        int red = (int) (Math.random() * 256);
        int green = (int) (Math.random() * 256);
        int blue = (int) (Math.random() * 256);
        Color startColor = new Color(red, green, blue);

        red = (int) (Math.random() * 256);
        green = (int) (Math.random() * 256);
        blue = (int) (Math.random() * 256);
        Color endColor = new Color(red, green, blue);

        GradientPaint gradient = new GradientPaint(70, 70, startColor, 150, 150, endColor);
        g2d.setPaint(gradient);
        g2d.fillOval(0, 0, this.getWidth(), this.getHeight());

    }
}


有一个用于绘制圆的面板类,然后将面板放置在框架的中心区域。

标签位于框架的西部,两个按钮colorButton(位于南部)和labelButton(位于东部)应分别控制圆圈和标签。 2个类ColorButtonListener和LabelButtonListener实现ActionListener接口,并分别向colorButton和labelButton注册。单击颜色按钮时,应使用随机颜色绘制一个圆圈,单击按钮时,标签按钮应在文本“我是标签”和“那很疼”之间切换。

现在,我遇到的问题是标签按钮。单击它时,它不仅更改了文本(按预期),而且还重画了圆圈。此按钮不应重画圆圈。颜色按钮按预期方式工作。

最佳答案

问题是您无法控制何时可能重新绘制。而不是每次调用您都无法控制的paintComponent时都改变颜色,例如,仅当您想从paintComponent方法中进行引用时才更改颜色。

class MyDrawPanel extends JPanel {
    private Color startColor;
    private Color endColor;

    // And setters or some other way
    // to randomise the colors

    public void paintComponent (Graphics g) {
        Graphics2D g2d = (Graphics2D) g;

        GradientPaint gradient = new GradientPaint(70, 70, startColor, 150, 150, endColor);
        g2d.setPaint(gradient);
        g2d.fillOval(0, 0, this.getWidth(), this.getHeight());

    }
}


查看Painting in AWT and Swing了解更多详细信息

10-01 03:26