关于一个小JAVA程序菜单监听没反应的问题~!
importjavax.swing.*;importjava.awt.event.*;importjava.awt.*;importjavax.swing.event.*...
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.event.*;
public class ddd {
public ddd() {
JFrame jf=new JFrame();
JPanel p=new JPanel();
JMenuBar menuBar = new JMenuBar();
JMenu function = new JMenu("GUI编程");
JMenuItem mcompute = new JMenu("商品计算");
JMenuItem mlist = new JMenu("列表框");
function.add(mcompute);
function.add(mlist);
menuBar.add(function);
//function.addItem("商品计算");
//function.addItem("列表框");
//setJMenubar(menuBar);
mcompute.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
new compute();
}
});
mlist.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
new list();
}
});
jf.setSize(500,500);
jf.getContentPane().add(menuBar,BorderLayout.NORTH);
jf.show();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String args[]){
new ddd();
}
}
class compute{
public compute(){
JFrame jf=new JFrame();
jf.setResizable(false);
JPanel p=new JPanel();
JLabel labPrice=new JLabel("商品单价");
p.add(labPrice);
final JTextField txtPrice=new JTextField(20);
p.add(txtPrice);
JLabel labAmount=new JLabel("商品数量");
p.add(labAmount);
final JTextField txtAmount=new JTextField(20);
p.add(txtAmount);
JLabel labTotal=new JLabel("商品总价");
p.add(labTotal);
final JTextField txtTotal=new JTextField(20);
txtTotal.setEditable(false);
p.add(txtTotal);
txtPrice.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
txtAmount.requestFocus();
txtAmount.selectAll();
}
});
txtAmount.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
double price=Double.parseDouble(txtPrice.getText());
int amount=Integer.parseInt(txtAmount.getText());
txtTotal.setText("" + price*amount);
txtPrice.requestFocus();
txtPrice.selectAll();
}
catch(Exception ex){
ex.printStackTrace();
txtTotal.setText("不能计算");
txtPrice.requestFocus();
txtPrice.selectAll();
}
}
});
jf.getContentPane().add(p);
jf.setSize(300,110);
jf.show();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class list{
public list(){
JFrame jf=new JFrame();
JPanel p=new JPanel();
p.add(new JLabel("选择字体"));
final JComboBox comFont=new JComboBox();
final JLabel labDescript=new JLabel();
comFont.setEditable(true);
comFont.addItem("宋体");
comFont.addItem("楷体");
comFont.addItem("仿宋");
comFont.addItem("黑体");
comFont.setSelectedIndex(-1);//不选中任何项
comFont.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String font=(String)comFont.getSelectedItem();
labDescript.setText("这是+font+字体");
labDescript.setFont(new Font(font,Font.BOLD,14));
}
});
p.add(comFont);
jf.getContentPane().add(p,BorderLayout.NORTH);
jf.getContentPane().add(labDescript,BorderLayout.NORTH);
jf.setSize(300,110);
jf.show();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
点没用 展开
import java.awt.event.*;
import java.awt.*;
import javax.swing.event.*;
public class ddd {
public ddd() {
JFrame jf=new JFrame();
JPanel p=new JPanel();
JMenuBar menuBar = new JMenuBar();
JMenu function = new JMenu("GUI编程");
JMenuItem mcompute = new JMenu("商品计算");
JMenuItem mlist = new JMenu("列表框");
function.add(mcompute);
function.add(mlist);
menuBar.add(function);
//function.addItem("商品计算");
//function.addItem("列表框");
//setJMenubar(menuBar);
mcompute.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
new compute();
}
});
mlist.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
new list();
}
});
jf.setSize(500,500);
jf.getContentPane().add(menuBar,BorderLayout.NORTH);
jf.show();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String args[]){
new ddd();
}
}
class compute{
public compute(){
JFrame jf=new JFrame();
jf.setResizable(false);
JPanel p=new JPanel();
JLabel labPrice=new JLabel("商品单价");
p.add(labPrice);
final JTextField txtPrice=new JTextField(20);
p.add(txtPrice);
JLabel labAmount=new JLabel("商品数量");
p.add(labAmount);
final JTextField txtAmount=new JTextField(20);
p.add(txtAmount);
JLabel labTotal=new JLabel("商品总价");
p.add(labTotal);
final JTextField txtTotal=new JTextField(20);
txtTotal.setEditable(false);
p.add(txtTotal);
txtPrice.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
txtAmount.requestFocus();
txtAmount.selectAll();
}
});
txtAmount.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
double price=Double.parseDouble(txtPrice.getText());
int amount=Integer.parseInt(txtAmount.getText());
txtTotal.setText("" + price*amount);
txtPrice.requestFocus();
txtPrice.selectAll();
}
catch(Exception ex){
ex.printStackTrace();
txtTotal.setText("不能计算");
txtPrice.requestFocus();
txtPrice.selectAll();
}
}
});
jf.getContentPane().add(p);
jf.setSize(300,110);
jf.show();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class list{
public list(){
JFrame jf=new JFrame();
JPanel p=new JPanel();
p.add(new JLabel("选择字体"));
final JComboBox comFont=new JComboBox();
final JLabel labDescript=new JLabel();
comFont.setEditable(true);
comFont.addItem("宋体");
comFont.addItem("楷体");
comFont.addItem("仿宋");
comFont.addItem("黑体");
comFont.setSelectedIndex(-1);//不选中任何项
comFont.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String font=(String)comFont.getSelectedItem();
labDescript.setText("这是+font+字体");
labDescript.setFont(new Font(font,Font.BOLD,14));
}
});
p.add(comFont);
jf.getContentPane().add(p,BorderLayout.NORTH);
jf.getContentPane().add(labDescript,BorderLayout.NORTH);
jf.setSize(300,110);
jf.show();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
点没用 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询