每次弹出的都是很小一块 必须手动拖拉
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class Main extends JFrame implements ActionListener{
private JTextField resultField;
private JButton s1,s2,s3,s4,s5,s6,s7,s8,s9,s0,b1,b2,b3,b4,f1,f2,f3;
private boolean end,add,sub,mul,div;
private String str;
private double num1,num2;public Main(){
super(" MyCalculator");
resultField=new JTextField("0");
resultField.setHorizontalAlignment(JTextField.RIGHT);
GridBagLayout layout = new GridBagLayout();
getContentPane().setLayout(layout);
s1=new JButton(" 1 ");
s2=new JButton(" 2 ");
s3=new JButton(" 3 ");
s4=new JButton(" 4 ");
s5=new JButton(" 5 ");
s6=new JButton(" 6 ");
s7=new JButton(" 7 ");
s8=new JButton(" 8 ");
s9=new JButton(" 9 ");
s0=new JButton(" 0 ");
b1=new JButton(" + ");
b2=new JButton(" - ");
b3=new JButton(" * ");
b4=new JButton(" / ");
f1=new JButton(" . ");
f2=new JButton(" = ");
f3=new JButton(" C ");
add(layout, resultField, 0, 0, 4, 1);
add(layout, s1, 1, 0, 1, 1);
add(layout, s2, 1, 1, 1, 1);
add(layout, s3, 1, 2, 1, 1);
add(layout, s4, 2, 0, 1, 1);
add(layout, s5, 2, 1, 1, 1);
add(layout, s6, 2, 2, 1, 1);
add(layout, s7, 3, 0, 1, 1);
add(layout, s8, 3, 1, 1, 1);
add(layout, s9, 3, 2, 1, 1);
add(layout, f1, 4, 0, 1, 1);
add(layout, s0, 4, 1, 1, 1);
add(layout, b1, 1, 3, 1, 1);
add(layout, b2, 2, 3, 1, 1);
add(layout, b3, 3, 3, 1, 1);
add(layout, b4, 4, 3, 1, 1);
add(layout, f2, 4, 2, 1, 1);
add(layout, f3, 5, 0, 4, 1);
}
public void num(int i){
String s = null;
s=String.valueOf(i);
if(end){
//如果数字输入结束,则将文本框置零,重新输入
resultField.setText("0");
end=false;}
if((resultField.getText()).equals("0")){
//如果文本框的内容为零,则覆盖文本框的内容
resultField.setText(s);}
else{
//如果文本框的内容不为零,则在内容后面添加数字
str = resultField.getText() + s;
resultField.setText(str);}
}public void actionPerformed(ActionEvent e){ //数字事件
if(e.getSource()==s1)
num(1);
else if(e.getSource()==s2)
num(2);
else if(e.getSource()==s3)
num(3);
else if(e.getSource()==s4)
num(4);
else if(e.getSource()==s5)
num(5);
else if(e.getSource()==s6)
num(6);
else if(e.getSource()==s7)
num(7);
else if(e.getSource()==s8)
num(8);
else if(e.getSource()==s9)
num(9);
else if(e.getSource()==s0)
num(0);//符号事件
else if(e.getSource()==b1)
sign(1);
else if(e.getSource()==b2)
sign(2);
else if(e.getSource()==b3)
sign(3);
else if(e.getSource()==b4)
sign(4);
else if(e.getSource()==f3)
sign(5);
//等号
else if(e.getSource()==f1){
str=resultField.getText();
if(str.indexOf(".")<=1){
str+=".";
resultField.setText(str);
}
}
else if(e.getSource()==f2){
num2=Double.parseDouble(resultField.getText());
if(add){
num1=num1 + num2;}
else if(sub){
num1=num1 - num2;}
else if(mul){
num1=num1 * num2;}
else if(div){
num1=num1 / num2;}
resultField.setText(String.valueOf(num1));
end=true;
}}
public void sign(int s){
if(s==1){
add=true;
sub=false;
mul=false;
div=false;
}
else if(s==2){
add=false;
sub=true;
mul=false;
div=false;
}
else if(s==3){
add=false;
sub=false;
mul=true;
div=false;
}
else if(s==4){
add=false;
sub=false;
mul=false;
div=true;
}
else if(s==5){
add=false;
sub=false;
mul=false;
div=false;
num1 = 0;
num2 = 0;
resultField.setText("0");
}
num1=Double.parseDouble(resultField.getText());
end=true;
}
public static void main(String[] args){
Main th1=new Main();
th1.show();
} private void add(GridBagLayout layout, Component component, int row, int col, int width, int height)
{
GridBagConstraints constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.BOTH;
constraints.insets = new Insets(10, 2, 10, 2);
constraints.weightx = 100;
constraints.weighty = 100;
constraints.gridx = col;
constraints.gridy = row;
constraints.gridwidth = width;
constraints.gridheight = height;
layout.setConstraints(component, constraints);
if (component instanceof JButton)
((JButton)component).addActionListener(this);
getContentPane().add(component);
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class Main extends JFrame implements ActionListener{
private JTextField resultField;
private JButton s1,s2,s3,s4,s5,s6,s7,s8,s9,s0,b1,b2,b3,b4,f1,f2,f3;
private boolean end,add,sub,mul,div;
private String str;
private double num1,num2;public Main(){
super(" MyCalculator");
resultField=new JTextField("0");
resultField.setHorizontalAlignment(JTextField.RIGHT);
GridBagLayout layout = new GridBagLayout();
getContentPane().setLayout(layout);
s1=new JButton(" 1 ");
s2=new JButton(" 2 ");
s3=new JButton(" 3 ");
s4=new JButton(" 4 ");
s5=new JButton(" 5 ");
s6=new JButton(" 6 ");
s7=new JButton(" 7 ");
s8=new JButton(" 8 ");
s9=new JButton(" 9 ");
s0=new JButton(" 0 ");
b1=new JButton(" + ");
b2=new JButton(" - ");
b3=new JButton(" * ");
b4=new JButton(" / ");
f1=new JButton(" . ");
f2=new JButton(" = ");
f3=new JButton(" C ");
add(layout, resultField, 0, 0, 4, 1);
add(layout, s1, 1, 0, 1, 1);
add(layout, s2, 1, 1, 1, 1);
add(layout, s3, 1, 2, 1, 1);
add(layout, s4, 2, 0, 1, 1);
add(layout, s5, 2, 1, 1, 1);
add(layout, s6, 2, 2, 1, 1);
add(layout, s7, 3, 0, 1, 1);
add(layout, s8, 3, 1, 1, 1);
add(layout, s9, 3, 2, 1, 1);
add(layout, f1, 4, 0, 1, 1);
add(layout, s0, 4, 1, 1, 1);
add(layout, b1, 1, 3, 1, 1);
add(layout, b2, 2, 3, 1, 1);
add(layout, b3, 3, 3, 1, 1);
add(layout, b4, 4, 3, 1, 1);
add(layout, f2, 4, 2, 1, 1);
add(layout, f3, 5, 0, 4, 1);
}
public void num(int i){
String s = null;
s=String.valueOf(i);
if(end){
//如果数字输入结束,则将文本框置零,重新输入
resultField.setText("0");
end=false;}
if((resultField.getText()).equals("0")){
//如果文本框的内容为零,则覆盖文本框的内容
resultField.setText(s);}
else{
//如果文本框的内容不为零,则在内容后面添加数字
str = resultField.getText() + s;
resultField.setText(str);}
}public void actionPerformed(ActionEvent e){ //数字事件
if(e.getSource()==s1)
num(1);
else if(e.getSource()==s2)
num(2);
else if(e.getSource()==s3)
num(3);
else if(e.getSource()==s4)
num(4);
else if(e.getSource()==s5)
num(5);
else if(e.getSource()==s6)
num(6);
else if(e.getSource()==s7)
num(7);
else if(e.getSource()==s8)
num(8);
else if(e.getSource()==s9)
num(9);
else if(e.getSource()==s0)
num(0);//符号事件
else if(e.getSource()==b1)
sign(1);
else if(e.getSource()==b2)
sign(2);
else if(e.getSource()==b3)
sign(3);
else if(e.getSource()==b4)
sign(4);
else if(e.getSource()==f3)
sign(5);
//等号
else if(e.getSource()==f1){
str=resultField.getText();
if(str.indexOf(".")<=1){
str+=".";
resultField.setText(str);
}
}
else if(e.getSource()==f2){
num2=Double.parseDouble(resultField.getText());
if(add){
num1=num1 + num2;}
else if(sub){
num1=num1 - num2;}
else if(mul){
num1=num1 * num2;}
else if(div){
num1=num1 / num2;}
resultField.setText(String.valueOf(num1));
end=true;
}}
public void sign(int s){
if(s==1){
add=true;
sub=false;
mul=false;
div=false;
}
else if(s==2){
add=false;
sub=true;
mul=false;
div=false;
}
else if(s==3){
add=false;
sub=false;
mul=true;
div=false;
}
else if(s==4){
add=false;
sub=false;
mul=false;
div=true;
}
else if(s==5){
add=false;
sub=false;
mul=false;
div=false;
num1 = 0;
num2 = 0;
resultField.setText("0");
}
num1=Double.parseDouble(resultField.getText());
end=true;
}
public static void main(String[] args){
Main th1=new Main();
th1.show();
} private void add(GridBagLayout layout, Component component, int row, int col, int width, int height)
{
GridBagConstraints constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.BOTH;
constraints.insets = new Insets(10, 2, 10, 2);
constraints.weightx = 100;
constraints.weighty = 100;
constraints.gridx = col;
constraints.gridy = row;
constraints.gridwidth = width;
constraints.gridheight = height;
layout.setConstraints(component, constraints);
if (component instanceof JButton)
((JButton)component).addActionListener(this);
getContentPane().add(component);
}
}
解决方案 »
- 快捷键问题
- webwork入门的helloworld问题,按照书上的代码来试验,搞了好几天都不成功,快郁闷死了,大家帮我看看是哪里的问题
- 偶尔出现javax.naming.NameNotFoundException过一阵儿又好了,是什么原因?
- 关于event listener的问题
- JAVA初级程序员问题!
- Java , Installanywhere 在 Redhat 上的中文问题解决方法
- 请问二维数组如何动态进行初始化
- 关于 JPasswordField?
- 有没有类似于字典一样的,介绍JAVA 各种包的功能的书?
- 请问上海地区现在什么地方有卖JBuilder4.0或者5.0????谢谢!
- JTable小问题
- 关于变量初始化的问题
main 加上pack(); 如下public static void main(String[] args) {
Main th1 = new Main();
th1.pack();
th1.show();
}