3
我一直在试图找出CardLayout与按钮 (所以像操作侦听 - 开始于负载了页面级和点击一个按钮切换到不同的“卡”CardLayout(摆动)与按钮上的动作侦听器?
我的代码不会甚至现在运行我不完全确定为什么 - 大多数实现我可以找到使用ItemListeners和组合框
我所做的基本过程是创建一个主JPanel,但我的卡JPanel到主JPanel,但我的不同的卡片插入到JPanel卡片中,然后将主JPanel添加到帧中以显示...
另外,对于我的一张卡片,我只需要显示一张pi cture - 我以前通过创建一个新的弹出窗口来实现这个功能,但是能够切换帧以显示它......我不知道为什么我无法弄清楚这个问题
这里是我的代码:
import java.awt.*;
/**
* Game
* Main class that specifies the frame and widgets of the GUI
*/
public class Game implements Runnable {
public void run(){
final String ON_OPEN = "Welcome!"; //Opening frame
final String GAME = "Play!"; // Game Frame
final String STATS = "Stats"; // Post-Game Stat Frame
final String HELP = "Help"; //tutorial frame
JPanel cards = new JPanel();
JPanel master; // a panel for the card layout
final JFrame frame = new JFrame(ON_OPEN);
frame.setLocation(500,200);
//Create the master layout for the program
master = (JPanel) frame.getContentPane();
master.setLayout(new BorderLayout()); // creating master layout
//Create panel for all the cards in CardLayout
final CardLayout cLay = new CardLayout();
cards.setLayout(cLay);
// all the cards
final JPanel help = new JPanel();
final JPanel stats = new JPanel();
final JPanel game = new JPanel (new BorderLayout());
final JPanel open = new JPanel (new FlowLayout());
// setting up ON_OPEN layout - uses JPanel open
final ImageIcon img = new ImageIcon("Instructions.png", "My Instructions..."); // the image I want shown under HELP card
final JButton info = new JButton("Help");
info.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// cLay.show(help, HELP); // WHAT I NORMALLY SHOULD BE DOING, RATHER JUST MAKE A NEW FRAME FOR THIS THOUGH
// frame.pack();
final JFrame infoFrame = new JFrame("Tutorial");
infoFrame.setLocation(500,50);
JLabel tutorialImg = new JLabel(img);
// int w = img.getIconWidth();
// int h = img.getIconHeight();
//infoFrame.setSize(w, h);
infoFrame.pack();
infoFrame.add(tutorialImg);
infoFrame.setVisible(true);
}
});
open.add(info); // the open-tutorial button
//Add them to the cards JPanel
cards.add(open, ON_OPEN);
cards.add(help, HELP);
cards.add(stats, STATS);
cards.add(game, GAME);
//Add the cards panel to the Master layout panel
master.add(cards);
// This code is all commented out because I'm not sure what I'm doing here...
// frame.add(cards);
// cLay.show(cards, ON_OPEN);
// frame.add(open, BorderLayout.CENTER);
// Main playing area - I want this to be shown in the GAME card...
GridLayout tileGrid = new GridLayout(4,4);
final JPanel grid = new JPanel(tileGrid);
// game.add(grid, BorderLayout.CENTER);
// grid.setLayout(tileGrid);
// frame.add(grid, BorderLayout.CENTER);
// Input - holds typing box
// final JPanel status_panel = new JPanel();
// frame.add(cards, BorderLayout.CENTER);
// frame.add(open, BorderLayout.CENTER);
final JTextField typingArea = new JTextField();
typingArea.setFocusTraversalKeysEnabled(false);
typingArea.setEditable(true);
typingArea.setFocusable(true);
typingArea.requestFocus();
frame.add(typingArea, BorderLayout.SOUTH);
typingArea.addKeyListener(new KeyAdapter() {
public void keyPressed (KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) { // enter key is pressed
String userWord = typingArea.getText().toLowerCase();
typingArea.setText("");
}
}
});
final JLabel status = new JLabel("Running...");
// status_panel.add(status);
// Reset button
final JPanel control_panel = new JPanel();
frame.add(control_panel, BorderLayout.NORTH);
]
// Put the frame on the screen
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args){
SwingUtilities.invokeLater(new Game());
}
}
添加了JLetterField类 - 也是我在哪里尝试添加的contentPane回到自己,我说什么frame.add(主,BorderLayout.CENTER)? 我试图让它打开只有两个按钮,你可以看到的是“信息”按钮,说“帮助”,并更改布局显示“Instructions.png”图片 - 然后在该按钮下一个标有“Play”(我还没有制作)的按钮将会改变布局为“GAME”卡。 – gadu 2013-04-23 22:05:04
@ user2310306:而不是给我们这个类,为了论坛目的,你应该只使用一个JTextField。这个类与您的问题无关,因此您的工作是简化您的代码,以便它仍然可以编译但显示您的问题。请现在就做。关于contentPane,主JPanel *是JFrame的contentPane。再说一遍,你为什么要加回来?没有意义。它已经在那里!我建议你放弃上面的代码,并试着用尽可能简单的代码解决手头的问题。 – 2013-04-23 22:07:18
editted ..谢谢我很新的Java和本网站感谢您的耐心 – gadu 2013-04-23 22:11:54