当前位置: 技术问答>java相关
求助~!!!程序功能设计问题,请执教!!
来源: 互联网 发布时间:2015-09-11
本文导语: 这段代码没有错误,但功能上有缺陷 只有点击菜单中的“加”时,才会出现结果 ,并且全部出现 但点级别的就没有反应如“减”; 请高手指正!! import java.awt.*; import java.awt.event.*; public class TheMenuEtc extends Frame...
这段代码没有错误,但功能上有缺陷
只有点击菜单中的“加”时,才会出现结果 ,并且全部出现
但点级别的就没有反应如“减”;
请高手指正!!
import java.awt.*;
import java.awt.event.*;
public class TheMenuEtc extends Frame{
static TextField tf1=new TextField(10);
static TextField tf2=new TextField(10);
static TextArea ta=new TextArea(8,16);
public static void main(String args[]){
TheMenuEtc tm=new TheMenuEtc("a simple calculater");
tm.addMenu();
tm.setcalculator();
tm.go();
}
TheMenuEtc(String str){
super(str);
}
public void addMenu(){
MenuBar mb=new MenuBar();
Menu calcu=new Menu("计算");
Menu edit=new Menu("编辑");
Menu help=new Menu("帮助");
MenuItem add=new MenuItem("加");
add.addActionListener(new MenuEvent1());
MenuItem minus=new MenuItem("减");
add.addActionListener(new MenuEvent2());
MenuItem multiply=new MenuItem("乘");
add.addActionListener(new MenuEvent3());
MenuItem divide=new MenuItem("除");
add.addActionListener(new MenuEvent4());
calcu.add(add);
calcu.add(minus);
calcu.add(multiply);
calcu.add(divide);
mb.add(calcu);
mb.add(edit);
mb.add(help);
setMenuBar(mb);
}
public void setcalculator(){
Panel p1=new Panel();
Label first=new Label("opr1");
Label second=new Label("opr2");
p1.add(first);
tf1.setEditable(true);
tf1.resize(tf1.preferredSize());
p1.add(tf1);
p1.add(second);
tf2.setEditable(true);
tf2.resize(tf2.preferredSize());
p1.add(tf2);
add("North",p1);
ta.setEditable(false);
add("Center",ta);
}
public void go(){
TheAdapterTest tat=new TheAdapterTest();
addWindowListener(tat);
setSize(400,300);
setVisible(true);
}
}
class TheAdapterTest extends WindowAdapter{
public void windowClosing(WindowEvent e)
{
System.exit(1);
}
}
class MenuEvent1 implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String newline=System.getProperty("line.separator");
String str1=TheMenuEtc.tf1.getText();
String str2=TheMenuEtc.tf2.getText();
int m=Integer.parseInt(str1);
int n=Integer.parseInt(str2);
m=m+n;
String str=Integer.toString(m);
TheMenuEtc.ta.append("the result is"+str+newline);
}
}
class MenuEvent2 implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String newline=System.getProperty("line.separator");
String str1=TheMenuEtc.tf1.getText();
String str2=TheMenuEtc.tf2.getText();
int m=Integer.parseInt(str1);
int n=Integer.parseInt(str2);
m=m-n;
String str=Integer.toString(m);
TheMenuEtc.ta.append("the result is"+str+newline);
}
}
class MenuEvent3 implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String newline=System.getProperty("line.separator");
String str1=TheMenuEtc.tf1.getText();
String str2=TheMenuEtc.tf2.getText();
int m=Integer.parseInt(str1);
int n=Integer.parseInt(str2);
m=m*n;
String str=Integer.toString(m);
TheMenuEtc.ta.append("the result is"+str+newline);
}
}
class MenuEvent4 implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String newline=System.getProperty("line.separator");
String str1=TheMenuEtc.tf1.getText();
String str2=TheMenuEtc.tf2.getText();
int m=Integer.parseInt(str1);
int n=Integer.parseInt(str2);
m=m/n;
String str=Integer.toString(m);
TheMenuEtc.ta.append("the result is"+str+newline);
}
}
只有点击菜单中的“加”时,才会出现结果 ,并且全部出现
但点级别的就没有反应如“减”;
请高手指正!!
import java.awt.*;
import java.awt.event.*;
public class TheMenuEtc extends Frame{
static TextField tf1=new TextField(10);
static TextField tf2=new TextField(10);
static TextArea ta=new TextArea(8,16);
public static void main(String args[]){
TheMenuEtc tm=new TheMenuEtc("a simple calculater");
tm.addMenu();
tm.setcalculator();
tm.go();
}
TheMenuEtc(String str){
super(str);
}
public void addMenu(){
MenuBar mb=new MenuBar();
Menu calcu=new Menu("计算");
Menu edit=new Menu("编辑");
Menu help=new Menu("帮助");
MenuItem add=new MenuItem("加");
add.addActionListener(new MenuEvent1());
MenuItem minus=new MenuItem("减");
add.addActionListener(new MenuEvent2());
MenuItem multiply=new MenuItem("乘");
add.addActionListener(new MenuEvent3());
MenuItem divide=new MenuItem("除");
add.addActionListener(new MenuEvent4());
calcu.add(add);
calcu.add(minus);
calcu.add(multiply);
calcu.add(divide);
mb.add(calcu);
mb.add(edit);
mb.add(help);
setMenuBar(mb);
}
public void setcalculator(){
Panel p1=new Panel();
Label first=new Label("opr1");
Label second=new Label("opr2");
p1.add(first);
tf1.setEditable(true);
tf1.resize(tf1.preferredSize());
p1.add(tf1);
p1.add(second);
tf2.setEditable(true);
tf2.resize(tf2.preferredSize());
p1.add(tf2);
add("North",p1);
ta.setEditable(false);
add("Center",ta);
}
public void go(){
TheAdapterTest tat=new TheAdapterTest();
addWindowListener(tat);
setSize(400,300);
setVisible(true);
}
}
class TheAdapterTest extends WindowAdapter{
public void windowClosing(WindowEvent e)
{
System.exit(1);
}
}
class MenuEvent1 implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String newline=System.getProperty("line.separator");
String str1=TheMenuEtc.tf1.getText();
String str2=TheMenuEtc.tf2.getText();
int m=Integer.parseInt(str1);
int n=Integer.parseInt(str2);
m=m+n;
String str=Integer.toString(m);
TheMenuEtc.ta.append("the result is"+str+newline);
}
}
class MenuEvent2 implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String newline=System.getProperty("line.separator");
String str1=TheMenuEtc.tf1.getText();
String str2=TheMenuEtc.tf2.getText();
int m=Integer.parseInt(str1);
int n=Integer.parseInt(str2);
m=m-n;
String str=Integer.toString(m);
TheMenuEtc.ta.append("the result is"+str+newline);
}
}
class MenuEvent3 implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String newline=System.getProperty("line.separator");
String str1=TheMenuEtc.tf1.getText();
String str2=TheMenuEtc.tf2.getText();
int m=Integer.parseInt(str1);
int n=Integer.parseInt(str2);
m=m*n;
String str=Integer.toString(m);
TheMenuEtc.ta.append("the result is"+str+newline);
}
}
class MenuEvent4 implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String newline=System.getProperty("line.separator");
String str1=TheMenuEtc.tf1.getText();
String str2=TheMenuEtc.tf2.getText();
int m=Integer.parseInt(str1);
int n=Integer.parseInt(str2);
m=m/n;
String str=Integer.toString(m);
TheMenuEtc.ta.append("the result is"+str+newline);
}
}
|
MenuBar mb=new MenuBar();
Menu calcu=new Menu("计算");
Menu edit=new Menu("编辑");
Menu help=new Menu("帮助");
MenuItem add=new MenuItem("加");
add.addActionListener(new MenuEvent1());
MenuItem minus=new MenuItem("减");
add.addActionListener(new MenuEvent2());-------here
MenuItem multiply=new MenuItem("乘");
add.addActionListener(new MenuEvent3());-----here
MenuItem divide=new MenuItem("除");
add.addActionListener(new MenuEvent4(());-----here
calcu.add(add);
calcu.add(minus);
calcu.add(multiply);
calcu.add(divide);
mb.add(calcu);
mb.add(edit);
mb.add(help);
setMenuBar(mb);
你反复只给add加了监听器.应该是
multiply.addActionListener();......
Menu calcu=new Menu("计算");
Menu edit=new Menu("编辑");
Menu help=new Menu("帮助");
MenuItem add=new MenuItem("加");
add.addActionListener(new MenuEvent1());
MenuItem minus=new MenuItem("减");
add.addActionListener(new MenuEvent2());-------here
MenuItem multiply=new MenuItem("乘");
add.addActionListener(new MenuEvent3());-----here
MenuItem divide=new MenuItem("除");
add.addActionListener(new MenuEvent4(());-----here
calcu.add(add);
calcu.add(minus);
calcu.add(multiply);
calcu.add(divide);
mb.add(calcu);
mb.add(edit);
mb.add(help);
setMenuBar(mb);
你反复只给add加了监听器.应该是
multiply.addActionListener();......