GUI未显示在我的屏幕上

时间:2013-12-09 18:40:42

标签: java swing constructor layout-manager gridbaglayout

之前我已经在我的计算机上运行了GUI,但是通过这个程序,我想尝试实现GridBag,这样我就可以制作一个简单的游戏。我不知道为什么它没有运行。这是代码:

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

public class GridBagTest extends JFrame{
    public static void main(String[] args){
        new GridBagTest();
    }

    public void GridBagTest(){
        JButton atk, mag, fort, pot1, pot2, flee;
        JPanel gamePanel = new JPanel();
        gamePanel.setLayout(new GridBagLayout());

        JFrame gameFrame = new JFrame("FightQuest"); 
        gameFrame.getContentPane().add(gamePanel);
        gameFrame.setSize(800, 600);
        gameFrame.pack();
        gameFrame.setVisible(true);

        atk = new JButton("Strike");
        mag = new JButton("Magic");
        fort = new JButton("Fortify");
        pot1 = new JButton("Potion 1");
        pot2 = new JButton("Potion 2");
        flee = new JButton("Flee");
        addItem(gamePanel, atk, 0, 0, 1, 1, GridBagConstraints.SOUTHEAST);
        addItem(gamePanel, mag, 1, 0, 1, 1, GridBagConstraints.SOUTH);
        addItem(gamePanel, fort, 2, 0, 1, 1, GridBagConstraints.SOUTHWEST);
        addItem(gamePanel, pot1, 0, 1, 1, 1, GridBagConstraints.NORTHEAST);
        addItem(gamePanel, pot2, 1, 1, 1, 1, GridBagConstraints.NORTH);
        addItem(gamePanel, flee, 2, 1, 1, 1, GridBagConstraints.NORTHWEST);

    }

    private void addItem(JPanel p, JComponent c, int x, int y, int width, int height, int align){
        GridBagConstraints gc = new GridBagConstraints();
        gc.gridx = x;
        gc.gridy = y;
        gc.gridwidth = width;
        gc.gridheight = height;
        gc.weightx = 100.0;
        gc.weighty = 100.0;
        gc.insets = new Insets(0, 0, 0, 0);
        gc.anchor = align;
        gc.fill = GridBagConstraints.NONE;
        p.add(c, gc);
    }
}

我不知道这是否有任何区别,但我从Java 6的Java参考书中获得了大部分代码,即使我正在运行Java 7,因为这是我学校的全部内容。我也在XFCE操作系统上完成所有代码。

4 个答案:

答案 0 :(得分:1)

更改此行

public void GridBagTest()

public GridBagTest()

构造函数没有返回类型。此外,您应该在将pack()setVisible(true)添加到容器后调用public class GridBagTest extends JFrame public class GridBagTest{ 来调整和显示组件。

另请注意,在这种情况下不需要延长。

更改

public class GridBagTest{
    public static void main(String[] args){
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new GridBagTest();
            }
        });
    }

    public GridBagTest(){
        JButton atk, mag, fort, pot1, pot2, flee;
        JPanel gamePanel = new JPanel();
        gamePanel.setLayout(new GridBagLayout());

        JFrame gameFrame = new JFrame("FightQuest"); 
        gameFrame.getContentPane().add(gamePanel);
        gameFrame.setSize(800, 600);


        atk = new JButton("Strike");
        mag = new JButton("Magic");
        fort = new JButton("Fortify");
        pot1 = new JButton("Potion 1");
        pot2 = new JButton("Potion 2");
        flee = new JButton("Flee");
        addItem(gamePanel, atk, 0, 0, 1, 1, GridBagConstraints.SOUTHEAST);
        addItem(gamePanel, mag, 1, 0, 1, 1, GridBagConstraints.SOUTH);
        addItem(gamePanel, fort, 2, 0, 1, 1, GridBagConstraints.SOUTHWEST);
        addItem(gamePanel, pot1, 0, 1, 1, 1, GridBagConstraints.NORTHEAST);
        addItem(gamePanel, pot2, 1, 1, 1, 1, GridBagConstraints.NORTH);
        addItem(gamePanel, flee, 2, 1, 1, 1, GridBagConstraints.NORTHWEST);
        gameFrame.pack();
        gameFrame.setVisible(true);

    }

    private void addItem(JPanel p, JComponent c, int x, int y, int width, int height, int align){
        GridBagConstraints gc = new GridBagConstraints();
        gc.gridx = x;
        gc.gridy = y;
        gc.gridwidth = width;
        gc.gridheight = height;
        gc.weightx = 100.0;
        gc.weighty = 100.0;
        gc.insets = new Insets(0, 0, 0, 0);
        gc.anchor = align;
        gc.fill = GridBagConstraints.NONE;
        p.add(c, gc);
    }
}

{{1}}

更改显示在代码中:

{{1}}

你得到你的输出:

enter image description here

答案 1 :(得分:0)

不确定这是否是您的问题,但您对gameFrame.setSize(800, 600);gameFrame.pack();的来电似乎有冲突,因为pack()会根据当前内容重新设置尺寸帧。 (在你称之为pack()的那一点上,什么都没有)

顺便说一句,您还应该在Event Dispatching Thread上创建和更新Swing组件。

答案 2 :(得分:0)

在审查这样的问题时,我首先要寻找的是应用程序的启动方式。请注意you are not properly launching your GUI。您应该确保在EDT(事件调度线程)上启动GUI。

以下是如何启动特定GUI的示例。

SSCCE:

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

/**
 * http://stackoverflow.com/questions/20478125/gui-not-showing-on-my-screen/20478279#20478279
 */
public class Q20478125 {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Q20478125();
            }
        });
    }

    public Q20478125() {
        JButton atk, mag, fort, pot1, pot2, flee;
        JPanel gamePanel = new JPanel();
        gamePanel.setLayout(new GridBagLayout());
        gamePanel.setPreferredSize(new Dimension(800, 600));

        JFrame gameFrame = new JFrame("FightQuest");
        gameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gameFrame.getContentPane().add(gamePanel);
        gameFrame.pack();
        gameFrame.setVisible(true);

        atk = new JButton("Strike");
        mag = new JButton("Magic");
        fort = new JButton("Fortify");
        pot1 = new JButton("Potion 1");
        pot2 = new JButton("Potion 2");
        flee = new JButton("Flee");
        addItem(gamePanel, atk, 0, 0, 1, 1, GridBagConstraints.SOUTHEAST);
        addItem(gamePanel, mag, 1, 0, 1, 1, GridBagConstraints.SOUTH);
        addItem(gamePanel, fort, 2, 0, 1, 1, GridBagConstraints.SOUTHWEST);
        addItem(gamePanel, pot1, 0, 1, 1, 1, GridBagConstraints.NORTHEAST);
        addItem(gamePanel, pot2, 1, 1, 1, 1, GridBagConstraints.NORTH);
        addItem(gamePanel, flee, 2, 1, 1, 1, GridBagConstraints.NORTHWEST);

    }

    private void addItem(JPanel p, JComponent c, int x, int y, int width, int height, int align) {
        GridBagConstraints gc = new GridBagConstraints();
        gc.gridx = x;
        gc.gridy = y;
        gc.gridwidth = width;
        gc.gridheight = height;
        gc.weightx = 100.0;
        gc.weighty = 100.0;
        gc.insets = new Insets(0, 0, 0, 0);
        gc.anchor = align;
        gc.fill = GridBagConstraints.NONE;
        p.add(c, gc);
    }
}

答案 3 :(得分:0)

您正在创建一个psudo构造函数,用于在void之前放置GridBagTest(),指定GridBagTest()是函数而不是构造函数。所以请致电:

new GridBagTest().GridBagTest();

主要功能内部。显然你不应该这样做:而是删除返回类型void

public GridBagTest(){ //<--- void is removed


        JFrame gameFrame = new JFrame("FightQuest"); 
        // other code
}

但是,请始终使用SwingUtilities.invokeLater(Runnable)将其提交至EventQueue,从EDT开始您的计划:

SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {

               new GridBagTest();
            }
        });