java 的setJMenuBar问题
importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassJSQextendsFram...
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JSQ extends Frame implements ActionListener{
private double result = 0;
private boolean flag = true; //标志
private JMenu menu;
private JMenuBar menubar;
private JMenuItem item;
private JTextField input1;
private JTextField input2;
private JTextField start;
private JLabel end;
private JTextField output;
private JButton b1;
private JButton [] p;
private String labels[] = {"1","2","3","4","5","6","7","8","9","0",".","清零","+","-","*","/"};
public JSQ(String s){
super(s);
input1 = new JTextField(5);
start = new JTextField(1);
input2 = new JTextField(5);
end = new JLabel("=");
output =new JTextField(10);
b1 = new JButton("等于");
p = new JButton[16];
for(int i=0; i<p.length; i++){
p[i] = new JButton(labels[i]);
}
menubar = new JMenuBar();
menu = new JMenu("File");
item = new JMenuItem("Quit");
menubar.add(menu);
menu.add(item);
add(menubar);
// setJMenuBar(menubar); //这句问什么是错的?
add(input1);
add(start);
add(input2);
add(end);
add(output);
for(int j=0; j<p.length; j++)
add(p[j]);
add(b1);
input1.addActionListener(this);
b1.addActionListener(this);
for(int i=0; i<p.length; i++)
p[i].addActionListener(this);
setLayout(new FlowLayout());
setBounds(60,60,340,220);
setBackground(new Color(200,220,180));
setVisible(true);
}
public static void main(String[] args){
JSQ f= new JSQ("Simple Calculator");
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0); //系统退出
}
});
}
public void actionPerformed(ActionEvent e){
for(int i=0; i<p.length-5; i++)
if((JButton)e.getSource()==p[i] && flag){
input1.setText(input1.getText()+e.getActionCommand()); //得到按钮上的数字
}
for(int j=12; j<p.length; j++){
if((JButton)e.getSource()==p[12]){
start.setText("+");
flag = false;
}
if((JButton)e.getSource()==p[13]){ //点击”+,-,*,/"时,在start中输出相应的运算符
start.setText("-");
flag = false;
}
if((JButton)e.getSource()==p[14]){
start.setText("*");
flag = false;
}
if((JButton)e.getSource()==p[15]){
start.setText("/");
flag = false;
}
}
for(int i=0; i<p.length-5; i++) //向input2中输入值
if((JButton)e.getSource()==p[i] && !flag){
input2.setText(input2.getText()+e.getActionCommand());
}
if(e.getSource() == b1){
String s1 =input1.getText();
String s2 =input2.getText();
String seprator = start.getText();
double d1 = Double.parseDouble(s1);
double d2 = Double.parseDouble(s2);
if(seprator.equals("+")) result = result + (d1 + d2);
if(seprator.equals("-")) result = result + (d1 - d2);
if(seprator.equals("*")) result = result + (d1 * d2);
if(seprator.equals("/")) result = result + (d1 / d2);
output.setText(String.valueOf(result));
}
if(e.getSource() == p[11]){
input1.setText(null);
input2.setText(null);
start.setText(null);
output.setText(null);
flag = true; //将标志flag设为原值,重新计算时,值将从input1输入
}
}
} 展开
import java.awt.event.*;
import javax.swing.*;
public class JSQ extends Frame implements ActionListener{
private double result = 0;
private boolean flag = true; //标志
private JMenu menu;
private JMenuBar menubar;
private JMenuItem item;
private JTextField input1;
private JTextField input2;
private JTextField start;
private JLabel end;
private JTextField output;
private JButton b1;
private JButton [] p;
private String labels[] = {"1","2","3","4","5","6","7","8","9","0",".","清零","+","-","*","/"};
public JSQ(String s){
super(s);
input1 = new JTextField(5);
start = new JTextField(1);
input2 = new JTextField(5);
end = new JLabel("=");
output =new JTextField(10);
b1 = new JButton("等于");
p = new JButton[16];
for(int i=0; i<p.length; i++){
p[i] = new JButton(labels[i]);
}
menubar = new JMenuBar();
menu = new JMenu("File");
item = new JMenuItem("Quit");
menubar.add(menu);
menu.add(item);
add(menubar);
// setJMenuBar(menubar); //这句问什么是错的?
add(input1);
add(start);
add(input2);
add(end);
add(output);
for(int j=0; j<p.length; j++)
add(p[j]);
add(b1);
input1.addActionListener(this);
b1.addActionListener(this);
for(int i=0; i<p.length; i++)
p[i].addActionListener(this);
setLayout(new FlowLayout());
setBounds(60,60,340,220);
setBackground(new Color(200,220,180));
setVisible(true);
}
public static void main(String[] args){
JSQ f= new JSQ("Simple Calculator");
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0); //系统退出
}
});
}
public void actionPerformed(ActionEvent e){
for(int i=0; i<p.length-5; i++)
if((JButton)e.getSource()==p[i] && flag){
input1.setText(input1.getText()+e.getActionCommand()); //得到按钮上的数字
}
for(int j=12; j<p.length; j++){
if((JButton)e.getSource()==p[12]){
start.setText("+");
flag = false;
}
if((JButton)e.getSource()==p[13]){ //点击”+,-,*,/"时,在start中输出相应的运算符
start.setText("-");
flag = false;
}
if((JButton)e.getSource()==p[14]){
start.setText("*");
flag = false;
}
if((JButton)e.getSource()==p[15]){
start.setText("/");
flag = false;
}
}
for(int i=0; i<p.length-5; i++) //向input2中输入值
if((JButton)e.getSource()==p[i] && !flag){
input2.setText(input2.getText()+e.getActionCommand());
}
if(e.getSource() == b1){
String s1 =input1.getText();
String s2 =input2.getText();
String seprator = start.getText();
double d1 = Double.parseDouble(s1);
double d2 = Double.parseDouble(s2);
if(seprator.equals("+")) result = result + (d1 + d2);
if(seprator.equals("-")) result = result + (d1 - d2);
if(seprator.equals("*")) result = result + (d1 * d2);
if(seprator.equals("/")) result = result + (d1 / d2);
output.setText(String.valueOf(result));
}
if(e.getSource() == p[11]){
input1.setText(null);
input2.setText(null);
start.setText(null);
output.setText(null);
flag = true; //将标志flag设为原值,重新计算时,值将从input1输入
}
}
} 展开
展开全部
你的计算器主类容器时Frame,Frame对应的设置菜单的方法是setMenuBar().而JFrame对应的设置菜单的方法才是setJMenuBar();
目前我不想看代码,如果你又疑问,给我发百度消息,代码看多了,真的不想看了
目前我不想看代码,如果你又疑问,给我发百度消息,代码看多了,真的不想看了
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
如果是JMenuItem被覆盖了显示不出来的话那是因为JMenuItem重量级太轻没别的解决办法,不要用带“J”的啦,用Menu吧。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询