到目前为止,我有一个简单的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,因此应设置“红色”。只有' 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);
}
答案 0 :(得分:1)
有2个问题
1)您在boolean whichColor=true;
方法()中创建了paintComponent()
;因此,每次绘制图形时,它都会创建一个whichcolor
变量,并且它始终为true。在paintcomponent
方法之外创建它作为实例变量。
2)在更改颜色之前创建颜色变量。您正在创建颜色变量但在此之后更改颜色。所以颜色变量保持不变。
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) {
}
}
答案 1 :(得分:0)
更改红色,蓝色,绿色变量的值后,您尚未初始化颜色对象。 如果要更改红蓝绿变量的值,请再次初始化颜色对象...