简体   繁体   English

Java Swing容器不会显示

[英]Java Swing Container Won't Appear

I'm designing a game through Java, using swing as my GUI. 我正在使用Swing作为GUI通过Java设计游戏。 NetBeans is my editor, and I'm trying to use their editor mode, however whenever I run the code, nothing appears. NetBeans是我的编辑器,我尝试使用它们的编辑器模式,但是,每当我运行代码时,都不会显示任何内容。 I tried building it into a jar, but when I run the jar nothing appears still. 我尝试将其构建到一个jar中,但是运行jar时,仍然没有任何显示。

Here is my code: 这是我的代码:

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
}

Thanks again for the help here. 再次感谢您的帮助。

When I run this code under JDK1.6, the window shows just fine. 当我在JDK1.6下运行此代码时,窗口显示就很好。 Although, I had to comment out the following line which is JDK1.7 API 虽然,我不得不注释掉以下行,即JDK1.7 API

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

And I think that line is the problem. 我认为这是问题所在。 You made it a pop-up window. 您将其设置为弹出窗口。 I would suggest to try with Type.NORMAL . 我建议尝试使用Type.NORMAL

Small side-note: you should adjust your main method to trigger the UI-code on the EDT: 小注释:您应调整main方法以触发EDT上的UI代码:

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

See the Concurrency in Swing tutorial for more information on the why. 有关原因的更多信息,请参见Swing中并发教程。

I am able to execute the program in Eclipse with absolutely no probs. 我能够完全没有问题地在Eclipse中执行程序。 and my Java version is version "1.7.0_03" 我的Java版本是版本“ 1.7.0_03” 在此输入图像描述

But still follows these two points below to have a better approach. 但是仍然遵循下面的这两点可以有更好的方法。

1. Take out the declaration of the components from main method and put them in the Class scope, at the beginning of the class. 1.从main方法中取出组件的声明,并将其放在类开头的Class范围内。

2. Place the setVisible(true) in the main 2.setVisible(true)放在主setVisible(true)

In the main use this... 主要用途是...

SwingUtilities.invokeLater(new Runnable() {
    @Override
    public void run() {
        Test8 t = new Test8();
        t.setVisible(true);
    }
});

I just pasted your code into Eclipse and it appears to be working (Java 7): 我刚刚将您的代码粘贴到Eclipse中,它似乎可以正常工作(Java 7):

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM