[英]Java: CardLayout switching between cards
我有類“框架”延伸JFrame
和separetad JPanels
: MainMenu
和SinglePanel
我使用CardLayout
,但我已經切換回用面板時有問題buttonSingle
和powrot
按鈕。 所以我的問題是如何使用這些按鈕更改/交換卡?
我的Frame
類:
public class Frame extends JFrame{
CardLayout cl = new CardLayout();
final MainMenu menuPanel = new MainMenu();
final SinglePanel singlePanel = new SinglePanel();
public Frame(){
setLayout(cl);
add(menuPanel,"menu");
add(singlePanel,"single");
setSize(200, 200);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setVisible(true);
setEnabled(true);
swapView("menu");
}
public void swapView(String view){
cl.show(getContentPane(),view);
}
}
我的MainMenu
課程:
public class MainMenu extends JPanel{
public MainMenu(){
setLayout(new BoxLayout(this , BoxLayout.Y_AXIS));
add(Box.createVerticalGlue());
JButton buttonSingle = new JButton("Single");
buttonSingle.setAlignmentX(Component.CENTER_ALIGNMENT);
buttonSingle.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
}
});
add(buttonSingle);
add(Box.createVerticalGlue());
JButton buttonMulti = new JButton("Multiplayer");
buttonMulti.setAlignmentX(Component.CENTER_ALIGNMENT);
buttonMulti.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
});
add(buttonMulti);
add(Box.createVerticalGlue());
JButton buttonExit = new JButton("Wyjście");
buttonExit.setAlignmentX(Component.CENTER_ALIGNMENT);
buttonExit.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}});
add(buttonExit);
add(Box.createVerticalGlue());
}
}
我的SinglePanel
類
public class SinglePanel extends JPanel{
SinglePanel(){
setLayout(new FlowLayout());
JButton powrot = new JButton("Wróć do menu");
powrot.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
}
});
add(powrot);
}
}
主要課程:
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
/*MainMenu mM = new MainMenu();*/
Frame f = new Frame();
}
}
您需要在面板類中引用JFrame
的CardLayout
。 您可以做的是將Frame
傳遞給JPanel
類作為參考,然后您可以在這些類中使用Frame
的CardLayout
來show
或next
等等。
public class MainMenu {
private CardLayout layout;
private Frame frame;
public MainMenu(final Frame frame) {
this.frame = frame;
this.layout = (CardLayout)frame.getLayout();
JButton buttonSingle = new JButton("Single");
buttonSingle.setAlignmentX(Component.CENTER_ALIGNMENT);
buttonSingle.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
layout.show(frame, "single");
}
});
}
}
創建MainPanel
,需要將Frame.this
傳遞給它,引用當前的Frame
MainMenu menuPanel = new MainMenu(Frame.this);
編輯
我剛剛注意到你的swapView
方法。 因此,不是直接在面板類中使用CardLayout
,實際上只需在actionPerformed
調用swapView
frame.swapView("single");
或者更好的是,為了不暴露Frame
類,你可以讓Frame
類實現一個interface
說SwapInterface
,它有一個你需要覆蓋的方法swapView
。 並將SwapInterface
傳遞給面板類。 就像是
public interface SwapInterface {
public void swapView(String view);
}
public Frame extends JFrame implements SwapInterface {
MainMenu mainPanel = new MainMenu(Frame.this);
....
@Override
public void swapView(String view) {
cl.show(getContentPane(), view);
}
}
public class MainMenu extends JPanel {
private SwapInterface swap;
public MainMenu(SwapInterface swap) {
this.swap = swap;
...
public void actionPerfomed(ActionEvent e) {
swap.swapView("single");
}
}
}
邊注
正如HovercraftFullOfEels在他的評論中指出的那樣,你應該使用字符串包含String卡值,這樣就沒有錯誤了。 就像是
private static final String SINGLE_CARD = "single";
然后在使用"single"
地方,請使用SINGLE_CARD
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.