CardLayout JFrame . , Frame JPanel , 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, . 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- String-, . -
private static final String SINGLE_CARD = "single";
, "single", SINGLE_CARD