请将我的代码放到eclipse 上运行下,如何在这个窗口顶端建一个文本域!
文本域的宽度是窗口的宽度,高度是Button 按钮的一半。应该比较简单吧:import java.awt.*;
import javax.swing.*;
public class CounterFrame extends JFrame{
public void conStruct(){
setBounds(300,300,300,400);
setLocation(300, 400);
setBackground(Color.lightGray);
setLayout(new GridLayout(4,4));
add(new Button("1"));
add(new Button("2"));
add(new Button("3"));
add(new Button("+"));
add(new Button("4"));
add(new Button("5"));
add(new Button("6"));
add(new Button("-"));
add(new Button("7"));
add(new Button("8"));
add(new Button("9"));
add(new Button("*"));
add(new Button("0"));
add(new Button("CE"));
add(new Button("="));
add(new Button("/"));
setVisible(true);
}
public static void main(String args[]){
new CounterFrame().conStruct();
}
}
文本域的宽度是窗口的宽度,高度是Button 按钮的一半。应该比较简单吧:import java.awt.*;
import javax.swing.*;
public class CounterFrame extends JFrame{
public void conStruct(){
setBounds(300,300,300,400);
setLocation(300, 400);
setBackground(Color.lightGray);
setLayout(new GridLayout(4,4));
add(new Button("1"));
add(new Button("2"));
add(new Button("3"));
add(new Button("+"));
add(new Button("4"));
add(new Button("5"));
add(new Button("6"));
add(new Button("-"));
add(new Button("7"));
add(new Button("8"));
add(new Button("9"));
add(new Button("*"));
add(new Button("0"));
add(new Button("CE"));
add(new Button("="));
add(new Button("/"));
setVisible(true);
}
public static void main(String args[]){
new CounterFrame().conStruct();
}
}
import java.awt.event.*;
public class Calculator extends WindowAdapter implements ActionListener {
private double result=0,data1=0,radixPointDepth=1;
private boolean radixPointIndicate=false,resultIndicate=false;
private char prec='+';
private Frame f;
private TextField tf;
private Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17;
private Panel p;
private static String a="";
static public void main(String args[]) {
Calculator de=new Calculator();
de.go();
}
public void go() {
f=new Frame("傻瓜计算器");
p=new Panel();
p.setLayout(new GridLayout(4,4));
tf=new TextField(30);
b1=new Button("7");
b2=new Button("8");
b3=new Button("9");
b4=new Button("+");
b5=new Button("4");
b6=new Button("5");
b7=new Button("6");
b8=new Button("-");
b9=new Button("1");
b10=new Button("2");
b11=new Button("3");
b12=new Button("*");
b13=new Button("0");
b14=new Button(".");
b15=new Button("=");
b16=new Button("/");
b17=new Button("清零");
f.add(tf,"North");
f.add(p,"Center");
f.add(b17,"South");
p.add(b1);
p.add(b2);
p.add(b3);
p.add(b4);
p.add(b5);
p.add(b6);
p.add(b7);
p.add(b8);
p.add(b9);
p.add(b10);
p.add(b11);
p.add(b12);
p.add(b13);
p.add(b14);
p.add(b15);
p.add(b16);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b10.addActionListener(this);
b11.addActionListener(this);
b12.addActionListener(this);
b13.addActionListener(this);
b14.addActionListener(this);
b15.addActionListener(this);
b16.addActionListener(this);
b17.addActionListener(this);
f.addWindowListener(this);
f.setSize(250,190);
f.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String s;
s=e.getActionCommand();
switch(s.charAt(0)) {
case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
if(resultIndicate) {
result=0;
data1=0;
prec='+';
}
Integer Int1=new Integer(s);
if(radixPointIndicate) {
radixPointDepth=radixPointDepth/10;
data1=data1+(Int1.intValue())*radixPointDepth;
} else {
data1=data1*10+(Int1.intValue());
}
Double displayNumber=new Double(data1);
tf.setText(a+displayNumber.toString());
resultIndicate=false;
break;
case '+': case '-':case '*':case '/':case '=':
if(s.charAt(0)!='='&&resultIndicate) { //不是等于而且真
prec=s.charAt(0);
resultIndicate=false;
} else {
switch(prec) {
case '+':
result=result+data1;
break;
case '-':
result=result-data1;
break;
case '*':
result=result*data1;
break;
case '/':
result=result/data1;
break;
}
}
radixPointIndicate=false;
radixPointDepth=1;
displayNumber=new Double(result);
a=new String(displayNumber.toString()+s.charAt(0));
if(s.charAt(0)=='='){
tf.setText(displayNumber.toString());
}else{
tf.setText(a);
}
if(s.charAt(0)!='=') { //不是等于
data1=0;
prec=s.charAt(0);
} else {
resultIndicate=true;
}
break;
case '.':
radixPointIndicate=true;
break;
}
if(s.equals("清零")) {
result=0;
data1=0;
radixPointDepth=1;
tf.setText("");
a="";
}
}
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
import java.awt.*;
public class Test_6
{
public static void main(String[] args)
{
JFrame mainframe = new JFrame();
mainframe.setTitle("计算器");
mainframe.setSize(400, 500);
JPanel mypanel = new JPanel();
JPanel anotherpanel = new JPanel();
mypanel.setLayout(new GridLayout(4,4));
JButton b1 = new JButton("7");
JButton b2 = new JButton("8");
JButton b3 = new JButton("9");
JButton b4 = new JButton("+");
JButton b5 = new JButton("4");
JButton b6 = new JButton("5");
JButton b7 = new JButton("6");
JButton b8 = new JButton("-");
JButton b9 = new JButton("1");
JButton b10 = new JButton("2");
JButton b11 = new JButton("3");
JButton b12 = new JButton("*");
JButton b13 = new JButton("=");
JButton b14 = new JButton("0");
JButton b15 = new JButton(".");
JButton b16 = new JButton("/");
JButton c = new JButton("Clear");
JButton i = new JButton("Info");
mypanel.add(b1);
mypanel.add(b2);
mypanel.add(b3);
mypanel.add(b4);
mypanel.add(b5);
mypanel.add(b6);
mypanel.add(b7);
mypanel.add(b8);
mypanel.add(b9);
mypanel.add(b10);
mypanel.add(b11);
mypanel.add(b12);
mypanel.add(b13);
mypanel.add(b14);
mypanel.add(b15);
mypanel.add(b16); anotherpanel.add(c);
anotherpanel.add(i);
Container part = mainframe.getContentPane();
part.add(mypanel,BorderLayout.CENTER);
part.add(anotherpanel,BorderLayout.SOUTH);
JTextField input = new JTextField();
part.add(input, BorderLayout.NORTH);
mainframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainframe.setVisible(true);
}
}