我希望我的按鈕來運行一個全新的類,將裏面做不同的事情。我不知道這是否甚至可能,因爲我在java上真的很糟糕。我的代碼目前看起來像這樣:按鈕運行一個全新的類
public class MainMenu {
private class GardenActivities {
public GardenActivities() {
JFrame GardenAct = new JFrame();
GardenAct.setSize(400, 400);
GardenAct.setVisible(true);
}
}
public static void main(String[] args) {
JFrame choice = new JFrame();
choice.setSize(700, 500);
choice.setLocationRelativeTo(null);
choice.setTitle("Seeds");
choice.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(0, 1));
JButton Labora = new JButton();
Labora.setText("Laboratory");
Labora.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ev) {
GardenActivities();
}
});
JButton Garden = new JButton();
Garden.setText("Garden");
Garden.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ev) {
}
});
choice.getContentPane().add(panel);
ButtonGroup group = new ButtonGroup();
group.add(Garden);
group.add(Labora);
panel.add(Garden);
panel.add(Labora);
choice.setVisible(true);
}
}
就像我說的。我只需按Garden
按鈕即可運行我的GardenActivities
類。
這幾乎是一樣的,你如何顯示在窗口中的主要方法,本質上只是複製粘貼代碼(相關顯示窗口),並把它在你的'ActionListener'。 –
參見[使用多個JFrames,好/壞的做法嗎?](http://stackoverflow.com/a/9554657/418556) –