現在、メニュー付きのゲームを作ろうとしています。メニューはこんな感じ。 http://puu.sh/xGoC
理想的には、ボタンを押すとゲームに移動します。ゲームはこんな感じ。 http://puu.sh/xGoV
現在、メニュークラスまたはゲームクラス(どちらもJPanels)を実行するメインクラスでJFrame()を初期化します。
CardLayoutを使用してゲームメニューを初期化し、ボタンをクリックしたときにパネルをゲームパネルに変更するにはどうすればよいですか?
現在、メニュー付きのゲームを作ろうとしています。メニューはこんな感じ。 http://puu.sh/xGoC
理想的には、ボタンを押すとゲームに移動します。ゲームはこんな感じ。 http://puu.sh/xGoV
現在、メニュークラスまたはゲームクラス(どちらもJPanels)を実行するメインクラスでJFrame()を初期化します。
CardLayoutを使用してゲームメニューを初期化し、ボタンをクリックしたときにパネルをゲームパネルに変更するにはどうすればよいですか?
サンプルコードをいくつか用意しました。完璧ではありませんが、機能するはずです。基本的に、レイアウトマネージャーでNEXTまたはPREVIOUS呼び出しを使用する必要があります。
パネルは2つしかないので、NEXT呼び出しを使用してパネルを循環します。Swingのドキュメントを読むのがおそらく最善ですが、これは機能しますが、他の要件もあるかもしれません。
import java.awt.CardLayout;
public class CardLayoutUsage extends javax.swing.JFrame {
public CardLayoutUsage() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
containerPanel = new javax.swing.JPanel();
menuPanel = new javax.swing.JPanel();
jButton2 = new javax.swing.JButton();
gamePanel = new javax.swing.JPanel();
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
containerPanel.setLayout(new java.awt.CardLayout());
menuPanel.setBackground(new java.awt.Color(153, 255, 255));
jButton2.setText("Go to Game");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
javax.swing.GroupLayout menuPanelLayout = new javax.swing.GroupLayout(menuPanel);
menuPanel.setLayout(menuPanelLayout);
menuPanelLayout.setHorizontalGroup(
menuPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, menuPanelLayout.createSequentialGroup()
.addContainerGap(135, Short.MAX_VALUE)
.addComponent(jButton2)
.addGap(149, 149, 149))
);
menuPanelLayout.setVerticalGroup(
menuPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, menuPanelLayout.createSequentialGroup()
.addContainerGap(141, Short.MAX_VALUE)
.addComponent(jButton2)
.addGap(134, 134, 134))
);
containerPanel.add(menuPanel, "card2");
gamePanel.setBackground(new java.awt.Color(255, 255, 204));
jButton1.setText("Go to Menu");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout gamePanelLayout = new javax.swing.GroupLayout(gamePanel);
gamePanel.setLayout(gamePanelLayout);
gamePanelLayout.setHorizontalGroup(
gamePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, gamePanelLayout.createSequentialGroup()
.addContainerGap(138, Short.MAX_VALUE)
.addComponent(jButton1)
.addGap(147, 147, 147))
);
gamePanelLayout.setVerticalGroup(
gamePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, gamePanelLayout.createSequentialGroup()
.addContainerGap(152, Short.MAX_VALUE)
.addComponent(jButton1)
.addGap(123, 123, 123))
);
containerPanel.add(gamePanel, "card3");
getContentPane().add(containerPanel, java.awt.BorderLayout.CENTER);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
CardLayout cl = (CardLayout)(containerPanel.getLayout());
cl.next(containerPanel);
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
CardLayout cl = (CardLayout)(containerPanel.getLayout());
cl.next(containerPanel);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
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(CardLayoutUsage.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(CardLayoutUsage.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(CardLayoutUsage.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(CardLayoutUsage.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new CardLayoutUsage().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JPanel containerPanel;
private javax.swing.JPanel gamePanel;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JPanel menuPanel;
// End of variables declaration
}
そのために使用しないでくださいCardLayout
。
代わりに、フレームのコンテンツ ペインからメニュー パネルを削除し、ゲーム パネルを追加します。
フレームも
忘れずに。validate()
このようなもの:
public static final int VIEW_MENU = 0;
public static final int VIEW_GAME = 1;
private static Container content;
...
content = frame.getContentPane();
public static void setView(int view){
if(view == VIEW_MENU){
content.remove(menuPanel);
content.add(gamePanel);
} else {
content.remove(gamePanel);
content.add(menuPanel);
}
frame.validate();
}
私が現在取り組んでいるプログラムには、各ビューの静的インスタンスを作成し、定数フィールドに対応するインデックスを持つ配列にそれらを追加するViewManagerというクラスがあります。また、パンくずリスト/戻るための以前のビューを追跡するためのフィールドもあります。車輪の再発明のケースかもしれませんが、それについてのすべてを制御することができます. そしてそれは効率的です。