我想知道如何使用Netbeans在已经打开的GUI中打开一个新的选项卡/窗口。
此外我正在尝试制作的游戏每个窗口会有1个问题并且有4个答案按钮供您选择,如果您得到正确答案,您的分数将保存在可在整个代码中使用的变量中。< / p>
**package pokemontrivia;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
public class triviagui extends javax.swing.JFrame implements ActionListener{
public triviagui() {
initComponents();
init();
}
private void init(){
setTitle("Pokemon Trivia Game");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(600, 480);
setLocationRelativeTo(null);
setVisible(true);
quitButton.addActionListener(new CloseListener());
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jTextField1 = new javax.swing.JTextField();
easyqestionsLabel = new javax.swing.JTextField();
mediumqestionsLabel = new javax.swing.JTextField();
hardqestionsLabel = new javax.swing.JTextField();
playButton3 = new javax.swing.JButton();
playButton2 = new javax.swing.JButton();
playButton1 = new javax.swing.JButton();
quitButton = new javax.swing.JButton();
settingsButton = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jTextField1.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
jTextField1.setText("Welcome to Pokemon Trivia !");
easyqestionsLabel.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
easyqestionsLabel.setText("Easy Questions");
mediumqestionsLabel.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
mediumqestionsLabel.setText("Medium Questions");
hardqestionsLabel.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
hardqestionsLabel.setText("Hard Questions");
playButton3.setText("Play !");
playButton2.setText("Play !");
playButton1.setText("Play !");
quitButton.setText("Quit");
settingsButton.setText("Settings");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(85, 85, 85)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(mediumqestionsLabel)
.addComponent(playButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(easyqestionsLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(hardqestionsLabel)
.addComponent(playButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(playButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(settingsButton, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(quitButton, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addGap(84, 84, 84))
.addGroup(layout.createSequentialGroup()
.addGap(116, 116, 116)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(127, Short.MAX_VALUE))
);
layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {easyqestionsLabel, hardqestionsLabel, mediumqestionsLabel});
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 86, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addComponent(settingsButton, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(17, 17, 17)
.addComponent(quitButton, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(easyqestionsLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(playButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(mediumqestionsLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(playButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(hardqestionsLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(playButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(69, 69, 69))
);
layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {playButton1, playButton2, playButton3});
pack();
}// </editor-fold>
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(triviagui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(triviagui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(triviagui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(triviagui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new triviagui().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JTextField easyqestionsLabel;
private javax.swing.JTextField hardqestionsLabel;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField mediumqestionsLabel;
private javax.swing.JButton playButton1;
private javax.swing.JButton playButton2;
private javax.swing.JButton playButton3;
private javax.swing.JButton quitButton;
private javax.swing.JButton settingsButton;
// End of variables declaration
@Override
public void actionPerformed(ActionEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}**
答案 0 :(得分:3)
我倾向于只为问题提供JLabel
,在按钮组中为答案提供4 x JRadioButton
个对象。当用户选择答案并被告知是/否时,请更改问题的文本(使用setText()
)和可能的答案。
这样做的好处是只需要一个面板就可以提出多达数千个问题。
如果有必要为问题设置一个面板而另一个面板需要结果,则使用带有两张卡片的CardLayout
。一个是问题,另一个是答案。
此short example显示了如何使用CardLayout
。