<pre name="code" class="java">import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Demo_menu extends JFrame implements ActionListener{
JMenu menu;
JMenuItem simple, high;
JMenuBar jmb;
JTextField jtf;
Demo_menu(){
setLayout(new FlowLayout());
JFrame jf = new JFrame("菜单练习");
jtf = new JTextField(10);
jmb = new JMenuBar();
menu = new JMenu("切换模式");
jmb.add(menu);
simple = new JMenuItem("简单数值计算");
high = new JMenuItem("高级计算");
simple.addActionListener(this);
high.addActionListener(this);
menu.add(simple);
menu.add(high);
add(jtf);
setJMenuBar(jmb);
setVisible(true);
setBounds(30, 40, 350, 150);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e){
if(e.getActionCommand().equals("简单数值计算")){
jtf.setText("0.0");
}
if(e.getActionCommand().equals("高级计算")){
jtf.setText("-_-");
}
}
public static void main(String[] args){
new Demo_menu();
}
}
java的菜单控件使用
最新推荐文章于 2024-03-26 08:11:37 发布