Java:CardLayout在卡之间切换 [英] Java: CardLayout switching between cards
问题描述
我有类'Frame'扩展 JFrame
和separetad JPanels
: MainMenu
和 SinglePanel
我使用 CardLayout
,但我有使用 buttonSingle
和 powrot
按钮切换回面板时出现问题。所以我的问题是如何使用这些按钮更改/交换卡片?
I've got class 'Frame' which extends JFrame
and separetad JPanels
: MainMenu
and SinglePanel
I am using CardLayout
, but I've got problem when switching back to panels using buttonSingle
and powrot
buttons. So my question is how can I change/swap between cards using these buttons?
我的框架
类:
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
class:
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
class
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
或下一步
等等。
You need a reference to the CardLayout
of the JFrame
inside your panel classes. What you can do is pass the Frame
to the JPanel
classes as reference, then you can use the CardLayout
of the Frame
in those classes to show
or next
etc. Something like
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
When you create the MainPanel
, you need to pass Frame.this
to it, referencing the current Frame
MainMenu menuPanel = new MainMenu(Frame.this);
编辑
我刚注意到你的 swapView
方法。因此,不要直接在面板类中使用 CardLayout
,实际上只需在<$ c $中调用 swapView
c> actionPerformed
I just noticed your swapView
method. So instead of use the CardLayout
directly in the panel class, you can actually just call swapView
in the actionPerformed
frame.swapView("single");
或者更好的是,不要公开 Frame
class,你可以让 Frame
类实现一个接口
说 SwapInterface
有一个方法 swapView
你需要覆盖。并将 SwapInterface
传递给面板类。类似
Or better yet, as to not expose the Frame
class, you can have the Frame
class implement an interface
say SwapInterface
that has a method swapView
you need to override. And pass the SwapInterface
to the panel classes. Something like
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在评论中指出的那样,你应该使用字符串包含字符串卡值,所以没有错误。类似
As HovercraftFullOfEels pointed out in his comment, you should make use of String contants for the String card values so there's no mistakes. Something like
private static final String SINGLE_CARD = "single";
然后在使用single的地方
,使用 SINGLE_CARD
而不是
Then in places where you use "single"
, use SINGLE_CARD
instead
这篇关于Java:CardLayout在卡之间切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!