0
所以我正在製作一個帶有下拉菜單的GUI菜單。我想讓GUI在下拉菜單中選擇一個選項時創建一個彈出窗口。我是否應該將彈出窗口的代碼放入actionPerformed方法中,或者創建一個單獨的方法並將其稱爲其他位置?把彈出代碼放在哪裏
這裏是我的代碼:
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTextField;
public class FinalGUI2 extends JApplet{
private JTextField text = new JTextField(10);
private ActionListener listen = new ActionListener(){
public void actionPerformed(ActionEvent e){
}
};
private JMenu[] menu = {new JMenu("Food"), new JMenu("Fluid"), new JMenu("Sleep")};
private JMenuItem[] items = {new JMenuItem("Add"), new JMenuItem("Add"), new JMenuItem("Add"),
new JMenuItem("Edit"), new JMenuItem("Edit"), new JMenuItem("Edit"),
new JMenuItem("Delete"), new JMenuItem("Delete"), new JMenuItem("Delete")};
public void init(){
for(int i = 0; i < items.length; i++){
items[i].addActionListener(listen);
menu[i%3].add(items[i]);
}
JMenuBar bar = new JMenuBar();
for(int i = 0; i < menu.length; i++)
bar.add(menu[i]);
setJMenuBar(bar);
Container contain = getContentPane();
contain.setLayout(new FlowLayout());
contain.add(text);
}
public static void main(String[] args){
run(new FinalGUI2(), 200, 75);
}
public static void run(JApplet app, int width, int height){
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(app);
frame.setSize(width, height);
app.init();
app.start();
frame.setVisible(true);
}
}