我正在通过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
}
再次感谢您的帮助。
答案 0 :(得分: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();
}
} );
}
有关原因的详细信息,请参阅Concurrency in Swing教程。
答案 1 :(得分:2)
我能够在Eclipse中执行程序,绝对没有probs。 我的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);
}
});
答案 2 :(得分:2)
我只是将您的代码粘贴到Eclipse中,它似乎正在运行(Java 7):