java - 更改 JComponent 的属性

标签 java swing components paintcomponent

到目前为止,我有一个简单的 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; }

无论我是否单击面板,我的“文本”始终显示为绿色。至少这意味着代码以某种方式工作。我仍然不明白:代码说“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() method() 内创建了 boolean whichColor=true;;因此,每次绘制图形时,它都会创建一个 whichcolor 变量,并且它始终为 true。在 paintcomponent 方法之外将其创建为实例变量。

2) 在更改颜色之前创建颜色变量。您正在创建颜色变量,但之后更改颜色。因此颜色变量保持不变。 颜色 RainbowColor = new Color(红、绿、蓝); .这就是为什么它总是绿色的。您可以将颜色创建行移到 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) {
    }

}

关于java - 更改 JComponent 的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32157807/

相关文章:

java - 如何在 JFrame 中使用 ActionListener 刷新 JTable?

java - Wicket - 实现复杂组件的最佳方式

Flash AS3 - 如果我更改标签,舞台上的按钮将不会隐藏 - 错误或丢失某些内容?

java - 不以//开头如何接受单行 Java 注释?

java - 房间类型转换器不工作

java - 为什么我不能访问面板的 getWidth() 和 getHeight() 函数?

java - 复合 JTree 节点允许事件传递到下面的对象

cakephp - 避免 CakePHP 的 Auth 组件显示身份验证错误消息

javascript - 如何使用谷歌应用程序脚本删除事件并发送电子邮件

java - 什么是父类(super class)型方法?