gpt4 book ai didi

java - 我在 GUI 中实现鼠标操作监听器时遇到问题

转载 作者:行者123 更新时间:2023-12-01 07:52:13 25 4
gpt4 key购买 nike

我创建了一个简单的 GUI,其中包含一个填充随机颜色的圆圈。我现在试图做到这一点,以便当单击鼠标到另一种随机颜色时,圆圈的颜色会发生变化。我创建了一个最初绘制圆圈的绘制组件方法和一个将更改圆圈颜色的重绘方法。然后我在鼠标监听器事件类中调用了此方法。问题是当我将操作监听器添加到 Pane 时出现错误。错误如下:

没有可访问的类型taskTwo 的封闭实例。必须使用类型为 taskTwo 的封闭实例来限定分配(例如 x.new A(),其中 x 是 taskTwo 的实例)。

我明白为什么会收到此错误,但不知道如何修复它,我尝试将操作监听器类移动到它自己的类中,但随后我无法在监听器中调用我的重绘方法。这是我的代码:

package weekThree;

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

public class taskTwo extends JComponent {

static Random rand = new Random();
JPanel pane = new JPanel();

public static void main(String[] args) {

JFrame window = new JFrame("Task Two");
JPanel pane = new JPanel();
pane.setLayout(new FlowLayout());
taskTwo t2 = new taskTwo();
window.setContentPane(t2);

t2.paint(null);
pane.addMouseListener(new MouseClick());

window.setBackground(Color.WHITE);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(300,300);
window.setVisible(true);

}

public void paintComponent(Graphics g) {

float red = rand.nextFloat();
float green = rand.nextFloat();
float blue = rand.nextFloat();

Color randomColor = new Color(red, green, blue);

g.drawOval(300, 300, 200, 200);
g.setColor(randomColor);
g.fillOval(300, 300, 200, 200);

}

public void repaint(Graphics g) {

float red = rand.nextFloat();
float green = rand.nextFloat();
float blue = rand.nextFloat();

Color randomColor = new Color(red, green, blue);

g.drawOval(300, 300, 200, 200);
g.setColor(randomColor);
g.fillOval(300, 300, 200, 200);

}

class MouseClick implements MouseListener {

public void mouseClicked(MouseEvent e) {
repaint();
}

public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}

}

}

谢谢,这也是一个扩展,以便仅当我在圆圈内单击时颜色才会改变,任何有关如何执行此操作的提示将不胜感激。

最佳答案

我会在实例世界中添加 MouseListener,而不是在静态世界中,并且该错误将会消失。我还建议您摆脱那个奇怪的重绘方法,因为它看起来与 Swing JComponent 自己的重绘方法太相似。你从不调用它,所以它对你没有好处。

还在鼠标监听器中随机化颜色,而不是在 PaintComponent 方法中(应该受到保护)。我也更喜欢扩展 JPanel 而不是 JComponent。

例如,

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Random;

import javax.swing.*;

@SuppressWarnings("serial")
public class MyTaskToo extends JPanel {
private static final int PREF_W = 400;
private static final int PREF_H = PREF_W;
private Color circleColor = Color.RED;
private int circX = 10;
private int circY = circX;
private int circW = PREF_W - 2 * circX;
private int circH = PREF_H - 2 * circY;

public MyTaskToo() {
// add in constructor -- in the "instance realm" not in the static realm
addMouseListener(new MyMouse());
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);

// to smooth out graphics
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

g2.setColor(circleColor);
g2.fillOval(circX, circY, circW, circH);

g2.setColor(Color.BLACK);
g2.drawOval(circX, circY, circW, circH);
}

@Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}

private class MyMouse extends MouseAdapter {
Random rand = new Random();

@Override
public void mousePressed(MouseEvent e) {
float red = rand.nextFloat();
float green = rand.nextFloat();
float blue = rand.nextFloat();
circleColor = new Color(red, green, blue);
repaint();
}
}

private static void createAndShowGui() {
JFrame frame = new JFrame("MyTaskToo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new MyTaskToo());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}

关于java - 我在 GUI 中实现鼠标操作监听器时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35365819/

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