我是Java新手,但是CardLayout
有问题。我有一个CardLayout
,有两张卡。我在第一个面板(卡布局的第一张卡片)中有3个单选按钮,第二个面板的内容取决于我在第一个面板中选择的内容。
因此,当我在面板之间切换时(检查一些单选按钮之后),需要创建此组件,但是在运行程序时会创建它。组件是在第二个面板的构造函数中创建的(组件称为ChartComponent
),因此是在创建第二个面板时创建的,但是我不知道如何在两个面板之间切换时如何创建它。我尝试在Second Panel中创建一个方法,该方法将在切换面板时创建组件并运行它,但这似乎不起作用。
MainMenu:
public class MainMenu_V1 extends JFrame {
private JMenuBar menuBar;
private JMenu file;
private JMenuItem exit;
private JPanel mainPanel;
private JMenuItem back;
private SecondPanel sec;
CardLayout cardLayout = new CardLayout();
public MainMenu_V1() throws IOException{
setTitle("My Layout");
setResizable(true);
setExtendedState(JFrame.MAXIMIZED_BOTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setJMenuBar(createMainMenu());
setLocationRelativeTo(null);
mainPanel = new JPanel();
mainPanel.setLayout(cardLayout);
sec = new SecondPanel(mainPanel);
mainPanel.add(new FirstPanel(mainPanel), "FIRST");
mainPanel.add(sec, "SECOND");
setContentPane(mainPanel);
}
public JMenuBar createMainMenu() {
menuBar = new JMenuBar();
file = new JMenu("Menu");
exit = new JMenuItem("Exit");
back = new JMenuItem("<-");
exit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
back.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
cardLayout.first(mainPanel);
}
});
file.add(back);
file.add(exit);
menuBar.add(file);
return menuBar;
}
public void switchPanel(Container container, String panelName) {
CardLayout card = (CardLayout) (container.getLayout());
card.show(container, panelName);
}
public static void main(String[] args) throws IOException {
new MainMenu_V1().setVisible(true);
}
}
第一张卡片:
public class FirstPanel extends JPanel {
private JButton button;
private JPanel mainPanel;
JRadioButton randomButton;
JRadioButton customButton;
JRadioButton fileButton;
private File inputfile;
private String aWay;
JButton createButton = new JButton("Create graph");
JButton Mode1 = new JButton("Mode 1");
JButton Mode2 = new JButton("Mode 2");
JButton Mode3 = new JButton("Mode 3");
final String random = "Randomize";
final String custom = "Custom";
final String file = "File";
FlowLayout chromLayout = new FlowLayout();
JLabel imageLabel = new JLabel();
public FirstPanel(JPanel mainPanel) throws IOException {
this.mainPanel = mainPanel;
final JPanel panel = new JPanel();
panel.setLayout(chromLayout);
JPanel controls = new JPanel();
setLayout(new BorderLayout());
controls.setLayout(new FlowLayout());
//create radiobuttons
randomButton = new JRadioButton(random);
randomButton.setActionCommand(random);
randomButton.setSelected(true);
customButton = new JRadioButton(custom);
customButton.setActionCommand(custom);
fileButton = new JRadioButton(file);
fileButton.setActionCommand(file);
//add controls to create graphs
final ButtonGroup group = new ButtonGroup();
group.add(randomButton);
group.add(customButton);
group.add(fileButton);
controls.add(randomButton);
controls.add(customButton);
controls.add(fileButton);
try {
ImageIcon ii = new ImageIcon(this.getClass().getResource("gif2.gif"));
imageLabel.setIcon(ii);
} catch (Exception exception) {
exception.printStackTrace();
}
//set action listener
Mode1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String command = group.getSelection().getActionCommand();
ReadAdjMatrix r = new ReadAdjMatrix();
try { r.setGraph(command);
MainMenu_V1 main = new MainMenu_V1();
main.switchPanel(mainPanel, "SECOND");
} catch (IOException ex) { ex.printStackTrace(); }
//Check the selection
}
});
panel.add(Mode1);
panel.add(Mode2);
panel.add(Mode3);
add(panel, BorderLayout.NORTH);
add(controls, BorderLayout.SOUTH);
add(imageLabel, BorderLayout.CENTER);
}
}
第二张卡:
public class SecondPanel extends JPanel {
private JButton button;
private JPanel mainPanel;
public SecondPanel(JPanel mainPanel) throws FileNotFoundException {
this.mainPanel = mainPanel;
setLayout(new BorderLayout());
add(new ChartComponent(), BorderLayout.CENTER);
}
编辑:
我尝试通过这种方式编辑第二个面板: 公共类SecondPanel扩展了JPanel {
private JButton button;
private JPanel mainPanel;
public SecondPanel(JPanel mainPanel) throws FileNotFoundException {
this.mainPanel = mainPanel;
//setPreferredSize(new Dimension(400, 200));
setLayout(new BorderLayout());
}
public void createComp() {
add(new ChartComponent(), BorderLayout.CENTER);
}
}
并通过以下方式在MainMenu中切换面板:
public void switchPanel(Container container, String panelName) {
if (panelName == "SECOND") { sec.createComp(); }
CardLayout card = (CardLayout) (container.getLayout());
card.show(container, panelName);
}
但是当我这样做时,组件并不是全部创建。