import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Calculator {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable(){
public void run(){
********************************MFrame f=new MFrame("Calculator");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
});
}
}
class MFrame extends JFrame{
public MFrame(String title){
super(title);
setBounds(100,100,306,360);
this.setResizable(false);
setLayout(null);
TextField tf=new TextField();
tf.setLocation(20, 20);
tf.setSize(260,50);
this.add(tf);
Button bd[]=new Button[12];
String[]s={"7","8","9","/","4","5","6","*","1","2","3","-"};
int[]bdx=new int[4];
int[]bdy=new int[3];
for(int i=0;i<4;i++){
bdx[i]=20+70*i;//设置按钮的x轴坐标
}
for(int i=0;i<3;i++){
bdy[i]=80+70*i;//设置按钮的y轴坐标
}
for(int i=0;i<bd.length;i++){
for(int j=0;j<4;j++){
for(int k=0;k<3;k++){
****************************************bd[i].setBounds(bdx[j],bdy[k],50,50);//定义按钮的位置以及大小
bd[i].setName(s[i]);//定义按钮的名称
}
}
this.add(bd[i]);
}
}
}
class Monitor implements ActionListener{
public void actionPerformed(ActionEvent e) {
// TODO 自动生成方法存根
}
}没有报错但是******行出现了问题还有就是一大堆的下面的情况Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at MFrame.<init>(Calculator.java:38)
at Calculator$1.run(Calculator.java:8)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:705)
at java.awt.EventQueue.access$000(EventQueue.java:101)
at java.awt.EventQueue$3.run(EventQueue.java:666)
at java.awt.EventQueue$3.run(EventQueue.java:664)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:675)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
解决方案 »
- 用ArrayList或LinkedList实现先进先出队列Queue
- linux-C实现SSLSocket服务端, 可以在客户端端用JSSE实现SSLSocket通讯吗
- 关于Java转换pdf文件的相关知识
- 请教一个超级算法难题——java根据定位坐标计算区域问题!
- (100分)《thinking in java》中关于内部类继承问题~~谢啦``~~
- 在一个字符串中怎么判断是英文还是中文?
- 面向对象的基本概念:类,实例,对象,引用的感悟,大家看看我的理解如何
- JList简单问题,在线放分!!!
- 再请教个I/O的小问题,谢谢。
- 帮帮我吧,各位大虾,为什么我这个程序老出不来结果,也不报错,也不出现异常
- java类名可以用Class这类的么?
- 求教SASL
//给你参考一下:
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.*;import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JButton;public class Calc extends JFrame implements ActionListener {
private JPanel contentPane; private JButton JBone = new JButton("1");
private JButton JBtwo = new JButton("2");
private JButton JBthree = new JButton("3");
private JButton JBfour = new JButton("4");
private JButton JBfive = new JButton("5");
private JButton JBsix = new JButton("6");
private JButton JBseven = new JButton("7");
private JButton JBeight = new JButton("8");
private JButton JBnight = new JButton("9");
private JButton JBzero = new JButton("0"); private JButton JBpoint = new JButton("."); private JButton JBadd = new JButton("+");
private JButton JBmultiply = new JButton("*");
private JButton JBdivide = new JButton("/");
private JButton JBsubtract = new JButton("-");
private JButton JBequals = new JButton("="); private JButton JBdbzero = new JButton("00"); private String tempOperand1 = null; //增加全局变量增加全局变量增加全局变量增加全局变量
private String tempOperand2 = null; private JTextField tf = new JTextField();// 显示框 private JButton JBclear = new JButton("清零"); boolean add = false, substract = false, multiply = false, divide = false; public Calc() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 369, 448);
setTitle("JAVA版计算器");
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(null);
setContentPane(contentPane); tf.setBounds(10, 10, 333, 34);
contentPane.add(tf);
tf.setColumns(10);
tf.setEditable(false); JBone.setBounds(30, 283, 52, 42);// 数字1
contentPane.add(JBone);
JBone.addActionListener(this); JBtwo.setBounds(92, 283, 52, 42);// 数字2
contentPane.add(JBtwo);
JBtwo.addActionListener(this); JBthree.setBounds(154, 283, 52, 42);// 数字3
contentPane.add(JBthree);
JBthree.addActionListener(this); JBfour.setBounds(30, 231, 52, 42);// 数字4
contentPane.add(JBfour);
JBfour.addActionListener(this); JBfive.setBounds(92, 231, 52, 42);// 数字5
contentPane.add(JBfive);
JBfive.addActionListener(this); JBsix.setBounds(154, 231, 52, 42);// 数字6
contentPane.add(JBsix);
JBsix.addActionListener(this); JBseven.setBounds(30, 179, 52, 42);// 数字7
contentPane.add(JBseven);
JBseven.addActionListener(this); JBeight.setBounds(92, 179, 52, 42);// 数字8
contentPane.add(JBeight);
JBeight.addActionListener(this); JBnight.setBounds(154, 179, 52, 42);// 数字9
contentPane.add(JBnight);
JBnight.addActionListener(this); JBzero.setBounds(30, 343, 52, 42);// 数字0
contentPane.add(JBzero);
JBzero.addActionListener(this); JBpoint.setBounds(92, 343, 52, 42);// 按钮.
contentPane.add(JBpoint);
JBpoint.addActionListener(this); JBadd.setBounds(221, 283, 52, 102);// 按钮+
contentPane.add(JBadd);
JBadd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
add = true;
tempOperand1 = tf.getText().trim();
tf.setText("");
}
});
JBmultiply.setBounds(221, 231, 52, 42);// 按钮*
contentPane.add(JBmultiply);
JBmultiply.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
multiply = true;
tempOperand1 = tf.getText().trim();
tf.setText("");
}
}); JBdivide.setBounds(283, 231, 52, 42);// 按钮/
contentPane.add(JBdivide);
JBdivide.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
divide = true;
tempOperand1 = tf.getText().trim();
tf.setText("");
}
}); JBsubtract.setBounds(283, 283, 52, 42);// 按钮-
contentPane.add(JBsubtract);
JBsubtract.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
substract = true;
tempOperand1 = tf.getText().trim();
tf.setText("");
}
}); JBequals.setBounds(283, 343, 52, 42);// 按钮=
contentPane.add(JBequals);
JBequals.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { tempOperand2 = tf.getText().trim();
;
double operand1 = Double.parseDouble(tempOperand1);// 既然都有小数点了,就干脆用Double型把
double operand2 = Double.parseDouble(tempOperand2);
double result = 0;
if (add) {
result = operand1 + operand2;
add = false;
}
if (substract) {
result = operand1 - operand2;
substract = false;
}
if (multiply) {
result = operand1 * operand2;
multiply = false;
}
if (divide) {
result = operand1 / operand2;
divide = false;
}
tf.setText(result + "");
tempOperand1 = null;
tempOperand2 = null;
} }); JBdbzero.setBounds(154, 343, 52, 42);// 按钮00 ////干嘛用的???????????????
contentPane.add(JBdbzero);
JBdbzero.addActionListener(this);//是不是想添加正负号?????? JBclear.setBounds(270, 179, 65, 42);
contentPane.add(JBclear);
JBclear.addActionListener(this); } public void actionPerformed(ActionEvent e) {
String m = null;
m = tf.getText().toString();
if (e.getSource() == JBone) {
m += "1";
tf.setText(m);
} else if (e.getSource() == JBtwo) {
m += "2";
tf.setText(m);
} else if (e.getSource() == JBthree) {
m += "3";
tf.setText(m);
} else if (e.getSource() == JBfour) {
m += "4";
tf.setText(m);
} else if (e.getSource() == JBfive) {
m += "5";
tf.setText(m);
} else if (e.getSource() == JBsix) {
m += "6";
tf.setText(m);
} else if (e.getSource() == JBseven) {
m += "7";
tf.setText(m);
} else if (e.getSource() == JBeight) {
m += "8";
tf.setText(m);
} else if (e.getSource() == JBnight) {
m += "9";
tf.setText(m);
} else if (e.getSource() == JBdbzero) {
m += "00";
tf.setText(m);
} else if (e.getSource() == JBpoint) {
m += ".";
tf.setText(m);
} else if (e.getSource() == JBclear) { tf.setText("");
}
} public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Calc frame = new Calc();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}}
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Calculator {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable(){
public void run(){
//********************************
MFrame f=new MFrame("Calculator");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
});
}
}class MFrame extends JFrame{
Container container=null;
JPanel btnJpanel=null; public MFrame(String title){
super(title);
container=this.getContentPane();
setBounds(100,100,306,360);
this.setResizable(false);
// setLayout(null);
container.setLayout(new BorderLayout());
JTextField tf=new JTextField();
tf.setLocation(20, 20);
tf.setSize(260,50);
container.add(tf,BorderLayout.NORTH); btnJpanel=new JPanel();
container.add(btnJpanel,BorderLayout.CENTER); btnJpanel.setLayout(new GridLayout(3,4));
JButton bd[]=new JButton[12];
String[]s={"7","8","9","/","4","5","6","*","1","2","3","-"};
int[]bdx=new int[4];
int[]bdy=new int[3];
for(int i=0;i<4;i++){
bdx[i]=20+70*i;//设置按钮的x轴坐标
}
for(int i=0;i<3;i++){
bdy[i]=80+70*i;//设置按钮的y轴坐标
}
for(int i=0;i<bd.length;i++){
bd[i]=new JButton(s[i]);
for(int j=0;j<4;j++){
for(int k=0;k<3;k++){
System.out.println("第"+j+"行");
bd[i].setBounds(bdx[j],bdy[k],50,50);//定义按钮的位置以及大小
btnJpanel.add(bd[i]);
// this.add(bd[i]);
// bd[i].setName();//定义按钮的名称
} }
}
}}
class Monitor implements ActionListener{
public void actionPerformed(ActionEvent e) {
// TODO 自动生成方法存根 }}
我觉得还是避免用数组,,
你把各个按钮的操作都在MFrame进行 写法看起来很简单
上面的程序是前段时间另外一个初学者写的,我只是修正了一下。
现在我觉得不但不要用数组,而且要自己为每个按钮建一个类,继承JButton。
然后在每个类里面设置按钮的属性,监听事件,和其他方法,达到“自己的事自己干,别去麻烦人家”的效果
不得不和其他控件进行交互时,可以将操作委托給MFrame,由MyFrame把消息传递给别的按钮或控件