58,454
社区成员
发帖
与我相关
我的任务
分享
public class TestFrame extends JFrame implements ActionListener {
TestFrame() {
setSize(480, 300);
JMenuBar jb = new JMenuBar();
JMenu jmenu1 = new JMenu("商品管理");
JMenuItem jitem1 = new JMenuItem("商品增加");
JMenuItem jitem2 = new JMenuItem("商品修改");
JMenuItem jitem3 = new JMenuItem("商品删除");
JMenuItem jitem4 = new JMenuItem("商品查询");
jmenu1.add(jitem1);
jmenu1.add(jitem2);
jmenu1.add(jitem3);
jmenu1.addSeparator();
jmenu1.add(jitem4);
JMenu jmenu2 = new JMenu("设置");
JMenuItem jitem5 = new JMenuItem("红色");
JMenuItem jitem6 = new JMenuItem("黄色");
JMenuItem jitem7 = new JMenuItem("绿色");
jmenu2.add(jitem5);
jmenu2.add(jitem6);
jmenu2.add(jitem7);
jb.add(jmenu1);
jb.add(jmenu2);
jitem4.addActionListener(this);
jitem5.addActionListener(this);
jitem6.addActionListener(this);
jitem7.addActionListener(this);
setJMenuBar(jb);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new TestFrame();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.out.println(e.getActionCommand());
if (e.getActionCommand().equals("商品查询")) {
new GueryFrame();
} else if (e.getActionCommand().equals("红色")) {
getContentPane().setBackground(Color.red);
repaint();
} else if (e.getActionCommand().equals("黄色")) {
getContentPane().setBackground(Color.yellow);
repaint();
} else if (e.getActionCommand().equals("绿色")) {
getContentPane().setBackground(Color.green);
repaint();
}
}
}
public class GueryFrame extends JFrame {
GueryFrame(){
setSize(480, 300);
setTitle("商品查询");
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setVisible(true);
}
}