我有一个随机涂有3种不同颜色(蓝色,红色和绿色)和3个具有相同颜色的按钮(蓝色,红色和绿色)的圆,如果该圆是红色的,那么我按红色按钮就需要显示如果您选择的颜色不正确,则会在“赢得的标签”中显示。这很简单,但是我无法从paintComponent中调用ActionPerformed变量(即与按钮匹配的颜色)。也为我的语言感到抱歉。
这是具有2个类别的CODE:
PaintPanel.class
public class PaintPanel extends JPanel implements ActionListener {
int x = 200, y = 250;
private JButton b1 = new JButton("BLUE");
private JButton b2 = new JButton("RED");
private JButton b3 = new JButton("GREEN");
JLabel label = new JLabel("Choose the right Color");
JPanel subPanel = new JPanel();
private Color[] colors;
public PaintPanel() {
setLayout(new BorderLayout());
setPreferredSize(new Dimension(440, 440));
add(label, BorderLayout.NORTH);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
subPanel.add(b1);
b1.setForeground(Color.BLUE);
subPanel.add(b2);
b2.setForeground(Color.RED);
subPanel.add(b3);
b3.setForeground(Color.GREEN);
add(subPanel, BorderLayout.SOUTH);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Color[] colors = new Color[3];
colors[0] = Color.BLUE;
colors[1] = Color.RED;
colors[2] = Color.GREEN;
Color c1 = colors[randInt(colors.length)];
g.setColor(c1);
/* this.colors.equals(c1); !!!! HERE I TRIED !!!*/
g.fillOval(x, y, 30, 30);
}
private int randInt(int length) {
// TODO Auto-generated method stub
Random rand = new Random();
int randomColor = rand.nextInt(length);
return randomColor;
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == b1) {
if (Color.BLUE.equals(colors)) {
label.setText("You WIN");
}
}else {
label.setText("You LOSE");
}
if (e.getSource() == b2) {
}
if (e.getSource() == b3) {
}
}
}
另一个-DrawCircle.class-
public class DrawCircle extends JFrame {
private JPanel painted;
public DrawCircle() {
painted = new PaintPanel();
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout());
setBounds(0, 0, 800, 540);
add(painted);
setVisible(true);
}
public static void main(String[] args) {
new DrawCircle();
}
}
答案 0 :(得分:0)
我认为您只是弄乱了括号(和缩进)。请在IDE中使用自动缩进或自动格式化工具,它将迅速找到这些问题。
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == b1) {
if (Color.BLUE.equals(colors)) {
label.setText("You WIN");
}
// v Problem is this extra brace
}else {
label.setText("You LOSE");
}
更改为
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == b1) {
if (Color.BLUE.equals(colors)) {
label.setText("You WIN");
}
else {
label.setText("You LOSE");
}
} else if( //...
答案 1 :(得分:0)
您的colors数组不应在paintComponent()方法中创建。应该将其声明为PaintPanel实例变量,并应在PaintPanel构造函数中创建。
这是一种可能(您不需要单独的类;我在PaintPanel中添加了一个主类):
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
public class PaintPanel extends JPanel implements ActionListener {
int x = 200, y = 250;
private JButton b1 = new JButton("BLUE");
private JButton b2 = new JButton("RED");
private JButton b3 = new JButton("GREEN");
JLabel label = new JLabel("Choose the right Color");
JPanel subPanel = new JPanel();
private Color circleColor;
public PaintPanel() {
setLayout(new BorderLayout());
setPreferredSize(new Dimension(440, 440));
add(label, BorderLayout.NORTH);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
subPanel.add(b1);
b1.setForeground(Color.BLUE);
subPanel.add(b2);
b2.setForeground(Color.RED);
subPanel.add(b3);
b3.setForeground(Color.GREEN);
add(subPanel, BorderLayout.SOUTH);
Random rand = new Random();
int rc = rand.nextInt(3);
switch (rc) {
case 1:
circleColor = Color.RED;
break;
case 2:
circleColor = Color.GREEN;
break;
default:
circleColor = Color.BLUE;
break;
}
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(circleColor);
g.fillOval(x, y, 30, 30);
}
@Override
public void actionPerformed(ActionEvent e) {
JButton b = (JButton) e.getSource();
if (b.getForeground().equals(circleColor)) {
label.setText("You WIN");
} else {
label.setText("You LOSE");
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
// create the main frame
JFrame frame = new JFrame();
// create the component to display in the frame
PaintPanel comp = new PaintPanel();
frame.add(comp, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent arg0) {
System.exit(0);
}
});
}
});
}
}
答案 2 :(得分:0)
请注意以下几点:您不能从静态上下文访问非静态方法。考虑将DrawCircle()
方法放在单独的类中。创建该类的实例,然后从该实例调用DrawCircle()
。
关于PaintPanel.class
,请注意paintComponent()
经常被调用,而不仅仅是在初始化时被调用。您生成的颜色需要保存在actionPerformed()
可以访问的位置。考虑在类结构中创建一个Color tmp
成员,并从那里引用正确的答案。此外,您似乎未接听UpdateUI()
的电话。这段代码并不完美,但是效果很好。就个人而言,除了覆盖paintComponent()之外,我还找到了另一种生成新颜色的方法,但是如果您在那里需要它,那么此示例将证明是有帮助的。在下面发表评论并进行改进:
package com.company;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
public class PaintPanel extends JPanel implements ActionListener
{
int x = 200, y = 250;
private JButton b1 = new JButton("BLUE");
private JButton b2 = new JButton("RED");
private JButton b3 = new JButton("GREEN");
JLabel label = new JLabel("Choose the right Color");
JPanel subPanel = new JPanel();
private Color[] colors;
Color tmp = null;
public PaintPanel()
{
setLayout(new BorderLayout());
setPreferredSize(new Dimension(440, 440));
add(label, BorderLayout.NORTH);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
subPanel.add(b1);
b1.setForeground(Color.BLUE);
subPanel.add(b2);
b2.setForeground(Color.RED);
subPanel.add(b3);
b3.setForeground(Color.GREEN);
add(subPanel, BorderLayout.SOUTH);
}
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
colors = new Color[3]; //Referencing Class member colors instead of local variable
colors[0] = Color.BLUE;
colors[1] = Color.RED;
colors[2] = Color.GREEN;
tmp = colors[randInt(colors.length)]; //Read into a class member instead of a local variable
g.setColor(tmp);
System.out.println("Paint Triggered. New Color is: " + tmp.toString()); //todo remove this debugging line
g.fillOval(x, y, 30, 30);
}
private int randInt(int length)
{
// TODO Auto-generated method stub
Random rand = new Random();
int randomColor = rand.nextInt(length);
return randomColor;
}
@Override
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == b1) {
if (Color.BLUE.equals(tmp)) {
label.setText("You WIN");
} else {
label.setText("You Loose");
}
} else if (e.getSource() == b2) {
if (Color.RED.equals(tmp)) {
label.setText("You WIN");
} else {
label.setText("You Loose");
}
} else if (e.getSource() == b3) {
if (Color.GREEN.equals(tmp)) {
label.setText("You WIN");
} else {
label.setText("You Loose");
}
}
updateUI(); //<---------------IMPORTANT To Sync What you see with each button press.
}
}