gpt4 book ai didi

Java:CardLayout在卡片之间切换

转载 作者:搜寻专家 更新时间:2023-11-01 02:09:01 26 4
gpt4 key购买 nike

我有类 'Frame',它扩展了 JFrame 和 separetad JPanels:MainMenuSinglePanel我正在使用 CardLayout,但在使用 buttonSinglepowrot 按钮切换回面板时遇到问题。所以我的问题是如何使用这些按钮在卡片之间更改/交换?

我的 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();
}

}

最佳答案

您需要在面板类中引用 JFrameCardLayout。你可以做的是将 Frame 传递给 JPanel 类作为引用,然后你可以使用 FrameCardLayout code> 在这些类中 shownext 等。类似

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 类实现一个 interfaceSwapInterface 具有您需要重写的方法 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 在他的评论中指出的那样,您应该为字符串卡值使用字符串常量,这样就不会出现错误。有点像

private static final String SINGLE_CARD = "single";

然后在你使用“single”的地方,使用SINGLE_CARD代替

关于Java:CardLayout在卡片之间切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22593162/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com