给你找了一个,我试过,可以用
import javaawt;
import javaxswing;
import javaawtevent;
import javaxswing;//GUi之前要吧这两个都引进来
public class Computer extends JFrame implements ActionListener
{
JButton a1,a2,a3,a4,a5,a6,a7,a8,a9,a0;
JButton b1,b2,b3,b4;
JButton c1,c2,c3,c4;
JTextField t1,t2;
JPanel p1,p2;
JLabel bq1,bq2;
String fuhao;
Double count,count2;
boolean chose=false,cliks;
public static void main(String[] args){
Computer l = new Computer();
}
public Computer(){
Font font = new Font("宋体", FontBOLD, 36);
Font font2 = new Font("宋体", FontBOLD, 20);
a1 = new JButton("1");
a1setFont(font);
a1addActionListener(this);
a2 = new JButton("2");
a2setFont(font);
a2addActionListener(this);
a3 = new JButton("3");
a3setFont(font);
a3addActionListener(this);
a4 = new JButton("4");
a4setFont(font);
a4addActionListener(this);
a5 = new JButton("5");
a5setFont(font);
a5addActionListener(this);
a6 = new JButton("6");
a6setFont(font);
a6addActionListener(this);
a7 = new JButton("7");
a7setFont(font);
a7addActionListener(this);
a8 = new JButton("8");
a8setFont(font);
a8addActionListener(this);
a9 = new JButton("9");
a9setFont(font);
a9addActionListener(this);
a0 = new JButton("0");
a0setFont(font);
a0addActionListener(this);
b1 = new JButton("清空");
b1addActionListener(this);
b2 = new JButton("返回");
b2addActionListener(this);
b3 = new JButton("");
b3addActionListener(this);
b4 = new JButton("=");
b4addActionListener(this);
c1 = new JButton("+");
c1addActionListener(this);
c2 = new JButton("-");
c2addActionListener(this);
c3 = new JButton("x");
c3addActionListener(this);
c4 = new JButton("÷");
c4addActionListener(this);
t1 = new JTextField(25);
t2 = new JTextField(35);
t1setFont(font2);
t2setFont(font2);
p1 = new JPanel();
p2 = new JPanel();
bq1 = new JLabel("结");
bq2 = new JLabel("果");
p1setLayout(new GridLayout(2,3));
p2setLayout(new GridLayout(4,4));
p1add(t1);p1add(b1);p1add(b2);
p1add(t2);p1add(bq1);p1add(bq2 );
p2add(a1);p2add(a2);p2add(a3);p2add(c1);
p2add(a4);p2add(a5);p2add(a6);p2add(c2);
p2add(a7);p2add(a8);p2add(a9);p2add(c3);
p2add(b3);p2add(a0);p2add(b4);p2add(c4);
thisadd(p1,BorderLayoutNORTH);
thisadd(p2,BorderLayoutCENTER);
thissetSize(460,380);
thissetTitle("简易计算器");
thissetLocation(200,200);
thissetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
thissetVisible(true);
}
public void actionPerformed(ActionEvent e){
Object temp = egetSource();
if(temp == a1){
if(chose==true){t1setText("");t2setText("");}
t1setText(t1getText()+""+"1");
chose=false;
}
if(temp == a2){
if(chose==true){t1setText("");t2setText("");}
t1setText(t1getText()+""+"2");
chose=false;
}
if(temp == a3){
if(chose==true){t1setText("");t2setText("");}
t1setText(t1getText()+""+"3");
chose=false;
}
if(temp == a4){
if(chose==true){t1setText("");t2setText("");}
t1setText(t1getText()+""+"4");
chose=false;
}
if(temp == a5){
if(chose==true){t1setText("");t2setText("");}
t1setText(t1getText()+""+"5");
chose=false;
}
if(temp == a6){
if(chose==true){t1setText("");t2setText("");}
t1setText(t1getText()+""+"6");
chose=false;
}
if(temp == a7){
if(chose==true){t1setText("");t2setText("");}
t1setText(t1getText()+""+"7");
chose=false;
}
if(temp == a8){
if(chose==true){t1setText("");t2setText("");}
t1setText(t1getText()+""+"8");
chose=false;
}
if(temp == a9){
if(chose==true){t1setText("");t2setText("");}
t1setText(t1getText()+""+"9");
chose=false;
}
if(temp == a0){
if(chose==true){t1setText("");t2setText("");}
t1setText(t1getText()+""+"0");
chose=false;
}
if(temp==b3){
cliks=true;
for(int i=0;i<t1getText()length();i++){
if(''==t1getText()charAt(i)){
cliks=false;
break;
}
if(cliks==true){
t1setText(t1getText()+"");
}
}
}
if(temp== c1){
count=DoubleparseDouble(t1getText());
t1setText("");
fuhao = "+";
}
if(temp== c2){
count=DoubleparseDouble(t1getText());
t1setText("");
fuhao = "-";
}
if(temp== c3){
count=DoubleparseDouble(t1getText());
t1setText("");
fuhao = "";
}
if(temp== c4){
count=DoubleparseDouble(t1getText());
t1setText("");
fuhao = "÷";
}
if(temp==b1){
t1setText("");
t2setText("");
}
if(temp==b2){
String s=t1getText();
t1setText("");
for(int i=0;i<slength()-1;i++){
char a = scharAt(i);
t1setText(t1getText()+a);
}
}
if(temp== b4){
count2=DoubleparseDouble(t1getText());
t1setText("");
if(fuhao=="+"){
//int sum=count+count2;
t1setText(count+""+fuhao+""+count2+""+"=");
t2setText(count+count2+"");
chose=true;
}
if(fuhao=="-"){
//int sum=count+count2;
t1setText(count+""+fuhao+""+count2+""+"=");
t2setText(count-count2+"");
chose=true;
}
if(fuhao==""){
//int sum=count+count2;
t1setText(count+""+fuhao+""+count2+""+"=");
t2setText(countcount2+"");
chose=true;
}
if(fuhao=="÷"){
//int sum=count+count2;
if(count2==0){
t1setText(count+""+fuhao+""+count2+"");
t2setText("除数不能为0");
return;
}
t1setText(count+""+fuhao+""+count2+""+"=");
t2setText(count/count2+"");
chose=true;
}
}
}
}
使用Font类
下边是例子
---------------------------------------------------------------------------------------------
import javaawtFont;
import javaxswingJFrame;
import javaxswingJLabel;
public class FontApp extends JFrame {
public FontApp() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(400, 300);
setLocationRelativeTo(null);
setResizable(false);
getContentPane()setLayout(null);
JLabel lblNewLabel = new JLabel("a String size 18");
lblNewLabelsetFont(new Font(null, FontITALIC, 18));
lblNewLabelsetBounds(12, 30, 232, 29);
getContentPane()add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("b String size 14");
lblNewLabel_1setFont(new Font(null, FontBOLD, 14));
lblNewLabel_1setBounds(12, 97, 232, 29);
getContentPane()add(lblNewLabel_1);
setVisible(true);
}
public static void main(String[] args) {
new FontApp();
}
}
以上就是关于救急啊!!!期末考试!怎样用JAVA的GUI(图形用户界面)来设计一个小程序!全部的内容,包括:救急啊!!!期末考试!怎样用JAVA的GUI(图形用户界面)来设计一个小程序!、java 用GUI写一个程序、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)