(Java)组件未显示在面板上 [英] (Java) Component not showing on Panel

查看:190
本文介绍了(Java)组件未显示在面板上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题已经被问过很多次,并且经常被回答,我读了几个小时的关于这个话题的文章,试图让人们继续工作,但是却无法使其运转.

I know this question was asked many times and frequently answered, i read some hours on this topic trying to get following to work, but couldn't manage to get it running.

我想向用户显示一个面板,他或她可以在其中单击以选择点,记住坐标并在以后对它们进行一些处理.

I want to show the user a panel where he or she can click on to select points, remember the coordinates and do some stuff with them later.

我想在单击时显示点,但它们不会显示.

I want to show the points when clicked, but they just don't show up.

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

The frame I paint around the panel does show up, but the points I set don't.

对于给出的任何建议都会很高兴,这对Java GUI编程来说确实是新的.

Would be glad for any advice given, im really new to java GUI programming.

//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);
    }
}

使用此代码,我看到矩形,但是没有点椭圆形.有什么提示我错了吗?

With this code i see the rectangle, but none of the point-ovals. Any clue where i'm wrong?

推荐答案

椭圆不应在扩展Component的类或任何其他GUI类中,而应在逻辑"类中,换句话说,非GUI类.让DrawBoard进行所有绘制,包括遍历MyPoint对象的ArrayList(而非数组)并绘制它们.另外,所有绘制都应在paintComponent覆盖范围内完成,而不是在paint覆盖范围内进行.

The ovals should not be in a class that extends Component or any other GUI class, but instead should be in a "logical" class, in other words, a non-GUI class. Have DrawBoard do all the drawing, including iterating through its ArrayList (not array) of MyPoint objects, and drawing them. In addition all drawing should be done within the paintComponent override, not a paint override.

例如

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)组件未显示在面板上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆