JavaSwing游戏:JFrame没有显示

时间:2016-01-08 15:12:38

标签: java swing jframe jpanel

我正在尝试编写一个简单的游戏。我处在一切的开始,我的目标是创建一个听我的键盘的盒子。在尝试在JPanel中绘制一个框时,JFrame会启动,然后在3秒内关闭。有人可以帮忙吗?我迷失在这个Java Swing JFrame,JPanel的情况中。

package abbygail;

public class Abbygail {
    public static void main(String[] args) {
        GUI gui = new GUI();
    }

}

不同的课程:

package abbygail;

import javax.swing.JFrame;
import javax.swing.JPanel;



public class GUI extends JFrame {



    public GUI(){
        setSize(640, 480);
        setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        Game_Board brd = new Game_Board();
        setContentPane(brd);
        getContentPane().setLayout(null);


    }
}

不同的课程:

package abbygail;

import javax.swing.JPanel;


public class Game_Board extends JPanel{

    public Game_Board(){
        setSize(640, 480);
        Blue_Box blbx = new Blue_Box();
        add(blbx);
    }
}

不同的课程:

package abbygail;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.geom.Rectangle2D;
import javax.swing.JPanel;


public class Blue_Box extends JPanel{

  /**
    *
    * @param g
    */
    private Point p1 = new Point(100, 100);
    private Point p2 = new Point(540, 380);

    public Blue_Box(){

    }

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setColor(Color.blue);
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setStroke(new BasicStroke(8,BasicStroke.CAP_ROUND,      BasicStroke.JOIN_BEVEL));
        g2d.draw(new Rectangle2D.Double(p1.x, p1.y, 40, 30));
    }

}

1 个答案:

答案 0 :(得分:6)

您永远不会在GUI实例上调用setVisible(true);

所以:

public class Abbygail {
    public static void main(String[] args) {
        GUI gui = new GUI();
        gui.setVisible(true);
    }
}

此外,关于使用null布局的一个小问题,而null布局和setBounds()似乎是Swing新手,比如创建复杂GUI的最简单和最好的方法,你创建的Swing GUI越多越严重使用它们时会遇到的困难。当GUI调整大小时,它们不会调整组件的大小,它们是增强或维护的皇室女巫,当它们被放置在滚动窗格中时它们完全失败,当它们在所有平台上观看时或者与原始平台不同的屏幕分辨率时它们看起来很糟糕

关于你的蓝盒子 - 你的Blue_Box JPanel的大小为0,0。给它一个getPreferredSize方法并返回一个合适的大小:

public class Blue_Box extends JPanel {

    private static final int PREF_W = 640;
    private static final int PREF_H = 480;
    private Point p1 = new Point(100, 100);
    private Point p2 = new Point(540, 380);

    public Blue_Box() {

    }

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setColor(Color.blue);
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setStroke(new BasicStroke(8, BasicStroke.CAP_ROUND, BasicStroke.JOIN_BEVEL));
        g2d.draw(new Rectangle2D.Double(p1.x, p1.y, 40, 30));
    }

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

}

并摆脱空布局