我上过课#' Frame'它扩展了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();
}
}
答案 0 :(得分:6)
您需要引用面板类中CardLayout
的{{1}}。您可以做的是将JFrame
作为参考传递给Frame
类,然后您可以将这些类中的JPanel
的{{1}}用于CardLayout
或Frame
等等。
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
swapView
或者更好的是,为了不公开actionPerformed
类,您可以让frame.swapView("single");
类实现Frame
说Frame
,其方法为interface
你需要覆盖。并将SwapInterface
传递给面板类。像
swapView
旁注
正如HovercraftFullOfEels在他的评论中指出的那样,你应该使用字符串包含String卡值,这样就没有错误了。像
这样的东西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");
}
}
}
的地方,请使用private static final String SINGLE_CARD = "single";
代替