2

我正在通过 Java 设计游戏,使用 swing 作为我的 GUI。NetBeans 是我的编辑器,我正在尝试使用他们的编辑器模式,但是每当我运行代码时,什么都没有出现。我尝试将它构建成一个罐子,但是当我运行罐子时,什么也没有出现。

这是我的代码:

package kraz;
public class KrazGUI extends javax.swing.JFrame {
    public KrazGUI() {
        initComponents();
        setVisible(true);
    }
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        PathOptions = new javax.swing.ButtonGroup();
        InventoryOptions = new javax.swing.ButtonGroup();
        Settings = new javax.swing.ButtonGroup();
        GameOptions = new javax.swing.ButtonGroup();
        Holder = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        EventField = new javax.swing.JTextPane();
        PathLabel = new javax.swing.JLabel();
        Left = new javax.swing.JButton();
        Right = new javax.swing.JButton();
        Middle = new javax.swing.JButton();
        InvLabel = new javax.swing.JLabel();
        PickUp = new javax.swing.JButton();
        SetDown = new javax.swing.JButton();
        Use = new javax.swing.JButton();
        SettingLabel = new javax.swing.JLabel();
        HighScores = new javax.swing.JButton();
        EditName = new javax.swing.JButton();
        EditGame = new javax.swing.JButton();
        ProgressLabel = new javax.swing.JLabel();
        ProgressBar = new javax.swing.JProgressBar();
        HealthBar = new javax.swing.JProgressBar();
        HealthLabel = new javax.swing.JLabel();
        RestartGame = new javax.swing.JButton();
        EndGame = new javax.swing.JButton();
        Exit = new javax.swing.JButton();
        GameLabel = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setAlwaysOnTop(true);
        setType(java.awt.Window.Type.POPUP);

        jScrollPane1.setViewportView(EventField);

        PathLabel.setText("Path Options:");

        Left.setText("Go Left");
        PathOptions.add(Left);

        Right.setText("Go Right");
        PathOptions.add(Right);

        Middle.setText("Go Middle");
        PathOptions.add(Middle);

        InvLabel.setText("Inventory Options:");

        PickUp.setText("Pick Up");
        InventoryOptions.add(PickUp);

        SetDown.setText("Set Down");
        InventoryOptions.add(SetDown);

        Use.setText("Use");
        InventoryOptions.add(Use);

        SettingLabel.setText("Settings:");

        HighScores.setText("High Scores");
        Settings.add(HighScores);

        EditName.setText("Edit Name");
        Settings.add(EditName);

        EditGame.setText("Edit Game");
        Settings.add(EditGame);

        ProgressLabel.setText("Progress:");

        HealthLabel.setText("Health:");

        RestartGame.setText("Restart Game");
        GameOptions.add(RestartGame);

        EndGame.setText("End Game");
        GameOptions.add(EndGame);

        Exit.setText("Exit");
        GameOptions.add(Exit);

        GameLabel.setText("Game Options:");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout HolderLayout = new javax.swing.GroupLayout(Holder);
        Holder.setLayout(HolderLayout);
        HolderLayout.setHorizontalGroup(
            HolderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(HolderLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(HolderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(HolderLayout.createSequentialGroup()
                        .addGroup(HolderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(HolderLayout.createSequentialGroup()
                                .addGroup(HolderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(HolderLayout.createSequentialGroup()
                                        .addComponent(InvLabel)
                                        .addGap(18, 18, 18)
                                        .addComponent(PickUp)
                                        .addGap(18, 18, 18)
                                        .addComponent(SetDown)
                                        .addGap(18, 18, 18)
                                        .addComponent(Use))
                                    .addGroup(HolderLayout.createSequentialGroup()
                                        .addComponent(SettingLabel)
                                        .addGap(18, 18, 18)
                                        .addComponent(HighScores)
                                        .addGap(18, 18, 18)
                                        .addComponent(EditName)
                                        .addGap(18, 18, 18)
                                        .addComponent(EditGame))
                                    .addGroup(HolderLayout.createSequentialGroup()
                                        .addComponent(PathLabel)
                                        .addGap(18, 18, 18)
                                        .addGroup(HolderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addGroup(HolderLayout.createSequentialGroup()
                                                .addComponent(Left)
                                                .addGap(18, 18, 18)
                                                .addComponent(Right)
                                                .addGap(18, 18, 18)
                                                .addComponent(Middle)))))
                                .addGap(0, 17, Short.MAX_VALUE))
                            .addGroup(HolderLayout.createSequentialGroup()
                                .addComponent(ProgressLabel)
                                .addGap(18, 18, 18)
                                .addComponent(ProgressBar, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(HealthLabel)
                                .addGap(18, 18, 18)
                                .addComponent(HealthBar, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addContainerGap())
                    .addGroup(HolderLayout.createSequentialGroup()
                        .addComponent(GameLabel)
                        .addGap(18, 18, 18)
                        .addComponent(RestartGame)
                        .addGap(18, 18, 18)
                        .addComponent(EndGame)
                        .addGap(18, 18, 18)
                        .addComponent(Exit)
                        .addGap(0, 0, Short.MAX_VALUE))))
        );
        HolderLayout.setVerticalGroup(
            HolderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(HolderLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(HolderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(HolderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(HolderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(Right)
                        .addComponent(Middle)
                        .addComponent(Left))
                    .addComponent(PathLabel))
                .addGap(18, 18, 18)
                .addGroup(HolderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(InvLabel)
                    .addComponent(PickUp)
                    .addComponent(SetDown)
                    .addComponent(Use))
                .addGap(18, 18, 18)
                .addGroup(HolderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(SettingLabel)
                    .addComponent(HighScores)
                    .addComponent(EditName)
                    .addComponent(EditGame))
                .addGap(18, 18, 18)
                .addGroup(HolderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(ProgressLabel)
                    .addComponent(ProgressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(HealthBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(HealthLabel))
                .addGap(18, 18, 18)
                .addGroup(HolderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(GameLabel)
                    .addComponent(RestartGame)
                    .addComponent(EndGame)
                    .addComponent(Exit))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(Holder, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(Holder, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>

    public static void main(String args[]) {
        new KrazGUI();
    }
    // Variables declaration - do not modify
    private javax.swing.JButton EditGame;
    private javax.swing.JButton EditName;
    private javax.swing.JButton EndGame;
    private javax.swing.JTextPane EventField;
    private javax.swing.JButton Exit;
    private javax.swing.JLabel GameLabel;
    private javax.swing.ButtonGroup GameOptions;
    private javax.swing.JProgressBar HealthBar;
    private javax.swing.JLabel HealthLabel;
    private javax.swing.JButton HighScores;
    private javax.swing.JPanel Holder;
    private javax.swing.JLabel InvLabel;
    private javax.swing.ButtonGroup InventoryOptions;
    private javax.swing.JButton Left;
    private javax.swing.JButton Middle;
    private javax.swing.JLabel PathLabel;
    private javax.swing.ButtonGroup PathOptions;
    private javax.swing.JButton PickUp;
    private javax.swing.JProgressBar ProgressBar;
    private javax.swing.JLabel ProgressLabel;
    private javax.swing.JButton RestartGame;
    private javax.swing.JButton Right;
    private javax.swing.JButton SetDown;
    private javax.swing.JLabel SettingLabel;
    private javax.swing.ButtonGroup Settings;
    private javax.swing.JButton Use;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration
}

再次感谢这里的帮助。

4

3 回答 3

3

当我在 JDK1.6 下运行这段代码时,窗口显示得很好。虽然,我不得不注释掉下面这行 JDK1.7 API

setType(java.awt.Window.Type.POPUP);

我认为那条线是问题所在。你把它变成了一个弹出窗口。我建议尝试使用 Type.NORMAL.

小旁注:您应该调整您的主要方法以触发 EDT 上的 UI 代码:

  public static void main(String args[]) {
    EventQueue.invokeLater( new Runnable() {
      @Override
      public void run() {
        new KrazGUI();
      }
    } );
  }

有关原因的更多信息,请参阅Swing 中的并发教程。

于 2012-07-17T17:36:27.003 回答
2

我能够在 Eclipse 中毫无问题地执行该程序。 我的 Java 版本是版本“1.7.0_03” 在此处输入图像描述

但是仍然遵循以下两点以获得更好的方法。

1.从main方法中取出组件的声明,放到Class范围内,在类的开头。

2.放在setVisible(true)

在主要用途中...

SwingUtilities.invokeLater(new Runnable() {
    @Override
    public void run() {
        Test8 t = new Test8();
        t.setVisible(true);
    }
});
于 2012-07-17T17:35:15.003 回答
2

我刚刚将您的代码粘贴到 Eclipse 中,它似乎正在工作(Java 7):

在此处输入图像描述

于 2012-07-17T17:36:45.817 回答