更改 JComponent 的属性

Change a JComponent's attribute

到目前为止,我有一个简单的 JPanel,还有一些基本元素,如要显示的字符串、一个矩形和一个 JButton,都在这个面板上。当我在这个面板上单击鼠标时,我希望使用自定义颜色的东西 'rainbowColor' 将它们的颜色更改为完全相同的颜色(字符串和矩形)。

int red = 0;
int green = 255;
int blue = 0;
Color rainbowColor = new Color(red, green, blue);
boolean whichColor = true;

if (whichColor) { red = 255; blue = 0; whichColor = false; }
else { red = 0; blue = 255; whichColor = true; }

我的 'TEXT' 似乎一直都是绿色的,无论我是否单击我的面板。至少这意味着代码以某种方式工作。我仍然不明白:代码说 'whichColor' 设置为 true,因此它应该将 'red' 设置为 255。在这种情况下只有 'paintComponent { ... }' 部分很重要(我的猜测) .我真的不知道我做错了什么,非常感谢你帮助我!

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



public class Menu extends JPanel {

       public void paintComponent(Graphics g) {

                int margin = 4;
                int red = 0;
                int green = 255;
                int blue = 0;
                Color rainbowColor = new Color(red, green, blue);
                boolean whichColor = true;

                if (whichColor) { red = 255; blue = 0; whichColor = false; }
                else { red = 0; blue = 255; whichColor = true; }

          super.paintComponent(g);
          Font customFont = new Font("Dialog", Font.BOLD, 20);
          g.setFont(customFont);
          g.setColor( new Color(0,0,0));
          g.drawString( "TEXT", 20, 30 );
//        randomiseColor(randomColor);
          g.setColor(rainbowColor);
          g.drawString( "TEXT", 22, 32 );
          g.drawRect(margin, margin, getWidth() - margin*2 - 1, getHeight() - margin*2 - 1);
       }

    }

public static class RandomColorOnClick implements MouseListener {

       public void mousePressed(MouseEvent evt) {
          Component source = (Component)evt.getSource();
          source.repaint(); 
       }

       public void mouseClicked(MouseEvent evt) { }
       public void mouseReleased(MouseEvent evt) { }
       public void mouseEntered(MouseEvent evt) { }
       public void mouseExited(MouseEvent evt) { }

    }

public static void main(String[] args){

    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

    double width = screenSize.getWidth();
    double height = screenSize.getHeight();

    RandomColorOnClick colorListener = new RandomColorOnClick();    
    ButtonHandler listener = new ButtonHandler();

    Menu main_text = new Menu();
    JPanel main_content = new JPanel();
    JFrame main_window = new JFrame("Some random text");
    JButton main_exit_button = new JButton("Exit");

    main_content.addMouseListener(colorListener);

    main_window.setContentPane(main_content);
    main_window.setSize(800, 600);
    main_window.setLocation(((int)width / 2) - 400, ((int)height / 2) - 300);
    main_window.setVisible(true);
    main_window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    main_content.setLayout(new BorderLayout());
    main_content.add(main_text, BorderLayout.CENTER);

    main_exit_button.addActionListener(listener);




}

有两个问题

1) 您已经在 paintComponent() 方法() 中创建了 boolean whichColor=true;;所以每次绘制图形时,它都会创建一个 whichcolor 变量,并且它总是 true.create 在 paintcomponent 方法之外作为实例变量。

2) 在更改之前创建颜色变量 colors.you 正在创建颜色变量,但是在 that.so 颜色变量保持不变之后更改颜色。 Color rainbowColor = new Color(red, green, blue); .这就是为什么它总是绿色的。你并在 if-else 条件之后移动颜色创建行。但是你可以在外部声明它并更改颜色而不是一次又一次地创建。

这里有一个例子...

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

public class Menu extends JPanel {

    private boolean whichColor = true;
    private Color rainbowColor;

    public void paintComponent(Graphics g) {

        int margin = 4;
        int red = 0;
        int green = 255;
        int blue = 0;

        if (whichColor) {
            red = 255;
            blue = 0;
            whichColor = false;
        } else {
            red = 0;
            blue = 255;
            whichColor = true;
        }
        rainbowColor = new Color(red, green, blue);
        super.paintComponent(g);
        Font customFont = new Font("Dialog", Font.BOLD, 20);
        g.setFont(customFont);
        g.setColor(new Color(0, 0, 0));
        g.drawString("TEXT", 20, 30);
//        randomiseColor(randomColor);
        g.setColor(rainbowColor);
        System.out.println(rainbowColor);
        g.drawString("TEXT", 22, 32);
        g.drawRect(margin, margin, getWidth() - margin * 2 - 1, getHeight() - margin * 2 - 1);

    }

    public static void main(String[] args) {

        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

        double width = screenSize.getWidth();
        double height = screenSize.getHeight();

        RandomColorOnClick colorListener = new RandomColorOnClick();
        ButtonHandler listener = new ButtonHandler();

        Menu main_text = new Menu();
        JPanel main_content = new JPanel();
        JFrame main_window = new JFrame("Some random text");
        JButton main_exit_button = new JButton("Exit");

        main_content.addMouseListener(colorListener);

        main_window.setContentPane(main_content);
        main_window.setSize(800, 600);
        main_window.setLocation(((int) width / 2) - 400, ((int) height / 2) - 300);
        main_window.setVisible(true);
        main_window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        main_content.setLayout(new BorderLayout());
        main_content.add(main_text, BorderLayout.CENTER);

        main_exit_button.addActionListener(listener);
    }

}

class RandomColorOnClick implements MouseListener {

    public void mousePressed(MouseEvent evt) {
        Component source = (Component) evt.getSource();
        source.repaint();
    }

    public void mouseClicked(MouseEvent evt) {
    }

    public void mouseReleased(MouseEvent evt) {
    }

    public void mouseEntered(MouseEvent evt) {
    }

    public void mouseExited(MouseEvent evt) {
    }

}

更改红色、蓝色、绿色变量的值后,您尚未初始化颜色对象。 如果您正在更改红蓝绿变量的值,请再次初始化颜色对象...