gpt4 book ai didi

java - (Java) 组件未显示在面板上

转载 作者:行者123 更新时间:2023-12-02 04:04:52 24 4
gpt4 key购买 nike

我知道这个问题被问了很多次并且经常得到回答,我读了几个小时关于这个主题的文章,试图让它正常工作,但无法让它运行。

我想向用户展示一个面板,他或她可以在其中单击以选择点、记住坐标并稍后使用它们做一些事情。

我想在点击时显示点,但它们就是不显示。

我在面板周围绘制的框架确实显示了,但我设置的点没有显示。

很高兴收到任何建议,我对 Java GUI 编程真的很陌生。

//in main class
SwingUtilities.invokeLater(() -> {
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
drawBoard = new DrawBoard();
drawBoard.setOpaque(true);
container conti = getContentPane();
conti.setLayout(new BorderLayout());
conti.add(drawBoard, BorderLayout.CENTER);
setLocation(400, 200);
setSize(1000, 600);
setVisible(true);
setResizable(false);
});

int timerDelay = 200;
new Timer(timerDelay, e -> {
drawBoard.repaint();
}).start();
//end of main class

public class drawBoard extends JPanel {
int n = 0;
MyPoint[] myPoints = new MyPoint[5];

public drawBoard(){
MyMouseAdapter ma = new MyMouseAdapter(this);
this.addMouseListener(ma);
}

public void addMyPoint(Point p){
this.myPoints[this.n] = new MyPoint(p);
add(myPoints[this.n]);
n++; // overflow ignored cause example
}

@Override
public void paint(Graphics g) {
super.paint(g);
g.drawRect(0,0,600,500);
}

}

public class MyMouseAdapter extends MouseAdapter{
drawBoard owner;

public MyMouseAdapter(drawBoard owner){
this.owner = owner;
}

public void mouseClicked(MouseEvent b) {
owner.addMyPoint(b.getPoint());
owner.invalidate();
owner.repaint();
}
}

public class MyPoint extends Component {
public int x;
public int y;

public MyPoint(Point p){
x = (int)p.getX();
y = (int)p.getY();
}

@Override
public void paint(Graphics g) {
super.paint(g);
g.drawOval(x - 3, y - 3, 6, 6);
}
}

通过这段代码,我看到了矩形,但没有看到任何点椭圆。有什么线索我错了吗?

最佳答案

椭圆不应位于扩展 Component 或任何其他 GUI 类的类中,而应位于“逻辑”类中,换句话说,非 GUI 类中。让 DrawBoard 执行所有绘图操作,包括迭代其 MyPoint 对象的 ArrayList(而不是数组)并绘制它们。此外,所有绘制都应在 PaintComponent 覆盖内完成,而不是在 Paint 覆盖内完成。

例如,

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;

public class MainFoo {

public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
createAndShowGui();
});
}

private static void createAndShowGui() {
DrawBoard mainPanel = new DrawBoard();
JFrame frame = new JFrame("Main Foo");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}

// DrawBoard should start with a capital letter
class DrawBoard extends JPanel {
private static final int PREF_W = 1000;
private static final int PREF_H = 600;
int n = 0;
List<MyPoint> myPoints = new ArrayList<>();

public DrawBoard() {
MyMouseAdapter ma = new MyMouseAdapter(this);
this.addMouseListener(ma);
}

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

public void addMyPoint(Point p) {
MyPoint myPoint = new MyPoint(p);
myPoints.add(myPoint);
repaint();
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawRect(0, 0, 600, 500);

// use Graphics2D to allow for smoothing
Graphics2D g2 = (Graphics2D) g;

// smooth with rendering hiints
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

// iterate through each MyPoint in the list
for (MyPoint myPoint : myPoints) {
myPoint.draw(g2); // and draw it
}

}

}

class MyMouseAdapter extends MouseAdapter {
DrawBoard owner;

public MyMouseAdapter(DrawBoard owner) {
this.owner = owner;
}

// btetter to use mousePressed, not mouseClicked
@Override
public void mousePressed(MouseEvent b) {
owner.addMyPoint(b.getPoint());
}
}

class MyPoint {
public int x;
public int y;

public MyPoint(Point p) {
x = (int) p.getX();
y = (int) p.getY();
}

public void draw(Graphics2D g2) {
g2.drawOval(x - 3, y - 3, 6, 6);
}
}

关于java - (Java) 组件未显示在面板上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34435794/

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