gpt4 book ai didi

java - 更改 JComponent 的属性

转载 作者:行者123 更新时间:2023-12-01 11:12:49 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com