Java JPanel边框未正确显示

时间:2016-07-18 20:49:52

标签: java

我对Java比较陌生。我参加了Java编程基础的暑期课程,我在上一次作业时遇到了一些问题。我正在使用Netbeans。

分配说你必须在JFrame窗口上绘制/创建一个矩形,然后开始用填充的黑色圆圈填充矩形,这些圆圈出现在矩形内的随机位置。球不得超出矩形。还应该在某处使用Vector类。

没有指定你必须如何创建矩形,所以我创建了一个从JPanel扩展的GraphicsTest类。这样我就可以通过random.nextInt()生成的球点坐标轻松设置最大限制。然后我覆盖了paintComponent方法(),这样它就会在创建球之后开始绘制球。否则在尝试运行时会抛出NullPointerException。

GraphicsTest类:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
 package jakso7_tehtava2;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.LayoutManager;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import java.util.Iterator;
import java.util.Random;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.border.Border;

/*
 *  Tekijä: Joonas Onatsu
 *  Jakson numero : 7
 *  Jakson tehtävänumero : 1
 *  Päiväys : 17.7.2016
 */
public class ApplicationWindow extends JFrame implements Runnable {

    private JPanel contents;
    private GraphicsTest application;

    ApplicationWindow() {
       initWindow();
    }

    private void initWindow() {

        this.setPreferredSize(new Dimension(800, 600));
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Border panelBorders = BorderFactory.createLineBorder(Color.black);
        LayoutManager layout = new GridBagLayout();

        contents = new JPanel();
        contents.setLayout(layout);
        contents.setBackground(Color.yellow);

        /* For some odd reason, this setBorder statement also sets borders for application JPanel */
        contents.setBorder(panelBorders);

        application = new GraphicsTest();
        //application.setBorder(panelBorders);
        application.setPreferredSize(new Dimension(500, 500));
        application.setBackground(Color.yellow);

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.anchor = GridBagConstraints.CENTER;

        contents.add(application, gbc);

        this.setContentPane(contents);

        this.pack();
        this.setVisible(true);
        this.setLocationRelativeTo(null);
    }

    @Override
    public void run() {
        application.start();
    }

    private class GraphicsTest extends JPanel {

        private Timer timer;
        private boolean paintBalls = false;
        private Dimension dimension;

        private Vector<ColoredBall> balls;
        private Iterator<ColoredBall> iter;
        private ColoredBall ball;

        private static final int NUM_BALLS = 1000;
        private static final int BALL_RADIUS = 5;
        private final Color ballColor = Color.BLACK;

        private Random randomGenerator;

        public GraphicsTest() {
            ActionListener animate = new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent ae) {
                    if (iter.hasNext()) {
                        ball = iter.next();
                    }
                    repaint();
                }
            };

            timer = new Timer(50, animate);
            randomGenerator = new Random();
        }

        public void start() {

            dimension = this.getSize();
            initBalls();
            iter = balls.iterator();
            ball = iter.next();
            paintBalls = true;
            timer.start();
        }

        private void initBalls() {
            balls = new Vector<ColoredBall>(NUM_BALLS);
            for (int i = 0; i < NUM_BALLS; i++) {
                Point point = getRandomPoint(dimension.width - (BALL_RADIUS * 2), dimension.height - (BALL_RADIUS * 2));
                balls.add(i, new ColoredBall(point, ballColor, BALL_RADIUS));
            }
        }

        private Point getRandomPoint(int maxX, int maxY) {
            return new Point(randomGenerator.nextInt(maxX), randomGenerator.nextInt(maxY));
        }

        @Override
        public void paintComponent(Graphics g) {
            if (!paintBalls) {
                super.paintComponent(g);
                g.dispose();
            } else if (paintBalls) {
                Graphics2D g2 = (Graphics2D) g;
                g2.setColor(ballColor);
                g2.fill(ball.getEllipse());
                g.dispose();
            }
        }

        private class ColoredBall {

            private Ellipse2D ball;
            private Color color;

            public ColoredBall(Point point, Color color, int radius) {
                this.color = color;
                ball = new Ellipse2D.Double(point.getX(), point.getY(), (radius * 2), (radius * 2));
            }

            public Ellipse2D getEllipse() {
                return ball;
            }
        }
    }
}

包含类的主要方法(不要注意其名称为Episode7_assignment2):

*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package jakso7_tehtava2;


/*
 *  Tekijä: Joonas Onatsu
 *  Jakson numero : 7
 *  Jakson tehtävänumero : 1
 *  Päiväys : 17.7.2016
 */ 
public class Jakso7_tehtava2 {

     /**
     * @param args the command line arguments
     */
     public static void main(String[] args) {
        ApplicationWindow window = new ApplicationWindow();
        window.run();
    } 
}

这很好用,创建了球并且它们保留在应用程序JPanel中。但是由于某些奇怪的原因,为内容JPanel设置边框,也为应用程序面板设置了相同的边框。

同样在运行程序时,应用程序面板按照需要居中在框架中,但应用程序面板的右边框和下边框已消失。

http://i63.tinypic.com/5z0col.jpg&lt; - 以下是该问题的屏幕截图。

我已经尝试解决这个问题两天了,并且通过无休止的堆栈StackOverflow问题和其他链接进行了搜索,发现没有人遇到同样的问题。

0 个答案:

没有答案