问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

救急!怎样用JAVA的GUI(图形用户界面)来设计一个小程序!求程序和详解。

发布网友 发布时间:2022-04-29 02:57

我来回答

1个回答

热心网友 时间:2023-10-08 16:40

给你找了一个,我试过,可以用
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.*;//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("宋体", Font.BOLD, 36);
Font font2 = new Font("宋体", Font.BOLD, 20);

a1 = new JButton("1");
a1.setFont(font);
a1.addActionListener(this);
a2 = new JButton("2");
a2.setFont(font);
a2.addActionListener(this);
a3 = new JButton("3");
a3.setFont(font);
a3.addActionListener(this);
a4 = new JButton("4");
a4.setFont(font);
a4.addActionListener(this);
a5 = new JButton("5");
a5.setFont(font);
a5.addActionListener(this);
a6 = new JButton("6");
a6.setFont(font);
a6.addActionListener(this);
a7 = new JButton("7");
a7.setFont(font);
a7.addActionListener(this);
a8 = new JButton("8");
a8.setFont(font);
a8.addActionListener(this);
a9 = new JButton("9");
a9.setFont(font);
a9.addActionListener(this);
a0 = new JButton("0");
a0.setFont(font);
a0.addActionListener(this);

b1 = new JButton("清空");
b1.addActionListener(this);
b2 = new JButton("返回");
b2.addActionListener(this);
b3 = new JButton(".");
b3.addActionListener(this);

b4 = new JButton("=");
b4.addActionListener(this);

c1 = new JButton("+");
c1.addActionListener(this);

c2 = new JButton("-");
c2.addActionListener(this);
c3 = new JButton("x");
c3.addActionListener(this);
c4 = new JButton("÷");
c4.addActionListener(this);

t1 = new JTextField(25);
t2 = new JTextField(35);
t1.setFont(font2);
t2.setFont(font2);

p1 = new JPanel();
p2 = new JPanel();

bq1 = new JLabel("结");
bq2 = new JLabel("果");

p1.setLayout(new GridLayout(2,3));
p2.setLayout(new GridLayout(4,4));

p1.add(t1);p1.add(b1);p1.add(b2);
p1.add(t2);p1.add(bq1);p1.add(bq2 );

p2.add(a1);p2.add(a2);p2.add(a3);p2.add(c1);
p2.add(a4);p2.add(a5);p2.add(a6);p2.add(c2);
p2.add(a7);p2.add(a8);p2.add(a9);p2.add(c3);
p2.add(b3);p2.add(a0);p2.add(b4);p2.add(c4);

this.add(p1,BorderLayout.NORTH);
this.add(p2,BorderLayout.CENTER);

this.setSize(460,380);
this.setTitle("简易计算器");
this.setLocation(200,200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e){
Object temp = e.getSource();
if(temp == a1){
if(chose==true){t1.setText("");t2.setText("");}
t1.setText(t1.getText()+""+"1");
chose=false;
}
if(temp == a2){
if(chose==true){t1.setText("");t2.setText("");}
t1.setText(t1.getText()+""+"2");
chose=false;

}
if(temp == a3){
if(chose==true){t1.setText("");t2.setText("");}
t1.setText(t1.getText()+""+"3");
chose=false;

}
if(temp == a4){
if(chose==true){t1.setText("");t2.setText("");}
t1.setText(t1.getText()+""+"4");
chose=false;
}
if(temp == a5){
if(chose==true){t1.setText("");t2.setText("");}
t1.setText(t1.getText()+""+"5");
chose=false;
}
if(temp == a6){
if(chose==true){t1.setText("");t2.setText("");}
t1.setText(t1.getText()+""+"6");
chose=false;
}
if(temp == a7){
if(chose==true){t1.setText("");t2.setText("");}
t1.setText(t1.getText()+""+"7");
chose=false;
}
if(temp == a8){
if(chose==true){t1.setText("");t2.setText("");}
t1.setText(t1.getText()+""+"8");
chose=false;
}
if(temp == a9){
if(chose==true){t1.setText("");t2.setText("");}
t1.setText(t1.getText()+""+"9");
chose=false;
}
if(temp == a0){
if(chose==true){t1.setText("");t2.setText("");}
t1.setText(t1.getText()+""+"0");
chose=false;
}
if(temp==b3){
cliks=true;
for(int i=0;i<t1.getText().length();i++){
if('.'==t1.getText().charAt(i)){
cliks=false;
break;
}
if(cliks==true){
t1.setText(t1.getText()+".");
}
}
}

if(temp== c1){
count=Double.parseDouble(t1.getText());
t1.setText("");
fuhao = "+";
}
if(temp== c2){
count=Double.parseDouble(t1.getText());
t1.setText("");
fuhao = "-";
}
if(temp== c3){
count=Double.parseDouble(t1.getText());
t1.setText("");
fuhao = "*";
}

if(temp== c4){
count=Double.parseDouble(t1.getText());
t1.setText("");
fuhao = "÷";
}
if(temp==b1){
t1.setText("");
t2.setText("");
}
if(temp==b2){
String s=t1.getText();
t1.setText("");
for(int i=0;i<s.length()-1;i++){
char a = s.charAt(i);
t1.setText(t1.getText()+a);
}
}
if(temp== b4){
count2=Double.parseDouble(t1.getText());
t1.setText("");
if(fuhao=="+"){
//int sum=count+count2;
t1.setText(count+""+fuhao+""+count2+""+"=");
t2.setText(count+count2+"");
chose=true;
}
if(fuhao=="-"){
//int sum=count+count2;
t1.setText(count+""+fuhao+""+count2+""+"=");
t2.setText(count-count2+"");
chose=true;
}
if(fuhao=="*"){
//int sum=count+count2;
t1.setText(count+""+fuhao+""+count2+""+"=");
t2.setText(count*count2+"");
chose=true;
}
if(fuhao=="÷"){
//int sum=count+count2;
if(count2==0){
t1.setText(count+""+fuhao+""+count2+"");
t2.setText("除数不能为0");
return;
}
t1.setText(count+""+fuhao+""+count2+""+"=");
t2.setText(count/count2+"");
chose=true;
}

}
}
}追问有详解吗?

热心网友 时间:2023-10-08 16:40

给你找了一个,我试过,可以用
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.*;//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("宋体", Font.BOLD, 36);
Font font2 = new Font("宋体", Font.BOLD, 20);

a1 = new JButton("1");
a1.setFont(font);
a1.addActionListener(this);
a2 = new JButton("2");
a2.setFont(font);
a2.addActionListener(this);
a3 = new JButton("3");
a3.setFont(font);
a3.addActionListener(this);
a4 = new JButton("4");
a4.setFont(font);
a4.addActionListener(this);
a5 = new JButton("5");
a5.setFont(font);
a5.addActionListener(this);
a6 = new JButton("6");
a6.setFont(font);
a6.addActionListener(this);
a7 = new JButton("7");
a7.setFont(font);
a7.addActionListener(this);
a8 = new JButton("8");
a8.setFont(font);
a8.addActionListener(this);
a9 = new JButton("9");
a9.setFont(font);
a9.addActionListener(this);
a0 = new JButton("0");
a0.setFont(font);
a0.addActionListener(this);

b1 = new JButton("清空");
b1.addActionListener(this);
b2 = new JButton("返回");
b2.addActionListener(this);
b3 = new JButton(".");
b3.addActionListener(this);

b4 = new JButton("=");
b4.addActionListener(this);

c1 = new JButton("+");
c1.addActionListener(this);

c2 = new JButton("-");
c2.addActionListener(this);
c3 = new JButton("x");
c3.addActionListener(this);
c4 = new JButton("÷");
c4.addActionListener(this);

t1 = new JTextField(25);
t2 = new JTextField(35);
t1.setFont(font2);
t2.setFont(font2);

p1 = new JPanel();
p2 = new JPanel();

bq1 = new JLabel("结");
bq2 = new JLabel("果");

p1.setLayout(new GridLayout(2,3));
p2.setLayout(new GridLayout(4,4));

p1.add(t1);p1.add(b1);p1.add(b2);
p1.add(t2);p1.add(bq1);p1.add(bq2 );

p2.add(a1);p2.add(a2);p2.add(a3);p2.add(c1);
p2.add(a4);p2.add(a5);p2.add(a6);p2.add(c2);
p2.add(a7);p2.add(a8);p2.add(a9);p2.add(c3);
p2.add(b3);p2.add(a0);p2.add(b4);p2.add(c4);

this.add(p1,BorderLayout.NORTH);
this.add(p2,BorderLayout.CENTER);

this.setSize(460,380);
this.setTitle("简易计算器");
this.setLocation(200,200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e){
Object temp = e.getSource();
if(temp == a1){
if(chose==true){t1.setText("");t2.setText("");}
t1.setText(t1.getText()+""+"1");
chose=false;
}
if(temp == a2){
if(chose==true){t1.setText("");t2.setText("");}
t1.setText(t1.getText()+""+"2");
chose=false;

}
if(temp == a3){
if(chose==true){t1.setText("");t2.setText("");}
t1.setText(t1.getText()+""+"3");
chose=false;

}
if(temp == a4){
if(chose==true){t1.setText("");t2.setText("");}
t1.setText(t1.getText()+""+"4");
chose=false;
}
if(temp == a5){
if(chose==true){t1.setText("");t2.setText("");}
t1.setText(t1.getText()+""+"5");
chose=false;
}
if(temp == a6){
if(chose==true){t1.setText("");t2.setText("");}
t1.setText(t1.getText()+""+"6");
chose=false;
}
if(temp == a7){
if(chose==true){t1.setText("");t2.setText("");}
t1.setText(t1.getText()+""+"7");
chose=false;
}
if(temp == a8){
if(chose==true){t1.setText("");t2.setText("");}
t1.setText(t1.getText()+""+"8");
chose=false;
}
if(temp == a9){
if(chose==true){t1.setText("");t2.setText("");}
t1.setText(t1.getText()+""+"9");
chose=false;
}
if(temp == a0){
if(chose==true){t1.setText("");t2.setText("");}
t1.setText(t1.getText()+""+"0");
chose=false;
}
if(temp==b3){
cliks=true;
for(int i=0;i<t1.getText().length();i++){
if('.'==t1.getText().charAt(i)){
cliks=false;
break;
}
if(cliks==true){
t1.setText(t1.getText()+".");
}
}
}

if(temp== c1){
count=Double.parseDouble(t1.getText());
t1.setText("");
fuhao = "+";
}
if(temp== c2){
count=Double.parseDouble(t1.getText());
t1.setText("");
fuhao = "-";
}
if(temp== c3){
count=Double.parseDouble(t1.getText());
t1.setText("");
fuhao = "*";
}

if(temp== c4){
count=Double.parseDouble(t1.getText());
t1.setText("");
fuhao = "÷";
}
if(temp==b1){
t1.setText("");
t2.setText("");
}
if(temp==b2){
String s=t1.getText();
t1.setText("");
for(int i=0;i<s.length()-1;i++){
char a = s.charAt(i);
t1.setText(t1.getText()+a);
}
}
if(temp== b4){
count2=Double.parseDouble(t1.getText());
t1.setText("");
if(fuhao=="+"){
//int sum=count+count2;
t1.setText(count+""+fuhao+""+count2+""+"=");
t2.setText(count+count2+"");
chose=true;
}
if(fuhao=="-"){
//int sum=count+count2;
t1.setText(count+""+fuhao+""+count2+""+"=");
t2.setText(count-count2+"");
chose=true;
}
if(fuhao=="*"){
//int sum=count+count2;
t1.setText(count+""+fuhao+""+count2+""+"=");
t2.setText(count*count2+"");
chose=true;
}
if(fuhao=="÷"){
//int sum=count+count2;
if(count2==0){
t1.setText(count+""+fuhao+""+count2+"");
t2.setText("除数不能为0");
return;
}
t1.setText(count+""+fuhao+""+count2+""+"=");
t2.setText(count/count2+"");
chose=true;
}

}
}
}追问有详解吗?

热心网友 时间:2023-10-08 16:40

给你找了一个,我试过,可以用
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.*;//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("宋体", Font.BOLD, 36);
Font font2 = new Font("宋体", Font.BOLD, 20);

a1 = new JButton("1");
a1.setFont(font);
a1.addActionListener(this);
a2 = new JButton("2");
a2.setFont(font);
a2.addActionListener(this);
a3 = new JButton("3");
a3.setFont(font);
a3.addActionListener(this);
a4 = new JButton("4");
a4.setFont(font);
a4.addActionListener(this);
a5 = new JButton("5");
a5.setFont(font);
a5.addActionListener(this);
a6 = new JButton("6");
a6.setFont(font);
a6.addActionListener(this);
a7 = new JButton("7");
a7.setFont(font);
a7.addActionListener(this);
a8 = new JButton("8");
a8.setFont(font);
a8.addActionListener(this);
a9 = new JButton("9");
a9.setFont(font);
a9.addActionListener(this);
a0 = new JButton("0");
a0.setFont(font);
a0.addActionListener(this);

b1 = new JButton("清空");
b1.addActionListener(this);
b2 = new JButton("返回");
b2.addActionListener(this);
b3 = new JButton(".");
b3.addActionListener(this);

b4 = new JButton("=");
b4.addActionListener(this);

c1 = new JButton("+");
c1.addActionListener(this);

c2 = new JButton("-");
c2.addActionListener(this);
c3 = new JButton("x");
c3.addActionListener(this);
c4 = new JButton("÷");
c4.addActionListener(this);

t1 = new JTextField(25);
t2 = new JTextField(35);
t1.setFont(font2);
t2.setFont(font2);

p1 = new JPanel();
p2 = new JPanel();

bq1 = new JLabel("结");
bq2 = new JLabel("果");

p1.setLayout(new GridLayout(2,3));
p2.setLayout(new GridLayout(4,4));

p1.add(t1);p1.add(b1);p1.add(b2);
p1.add(t2);p1.add(bq1);p1.add(bq2 );

p2.add(a1);p2.add(a2);p2.add(a3);p2.add(c1);
p2.add(a4);p2.add(a5);p2.add(a6);p2.add(c2);
p2.add(a7);p2.add(a8);p2.add(a9);p2.add(c3);
p2.add(b3);p2.add(a0);p2.add(b4);p2.add(c4);

this.add(p1,BorderLayout.NORTH);
this.add(p2,BorderLayout.CENTER);

this.setSize(460,380);
this.setTitle("简易计算器");
this.setLocation(200,200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e){
Object temp = e.getSource();
if(temp == a1){
if(chose==true){t1.setText("");t2.setText("");}
t1.setText(t1.getText()+""+"1");
chose=false;
}
if(temp == a2){
if(chose==true){t1.setText("");t2.setText("");}
t1.setText(t1.getText()+""+"2");
chose=false;

}
if(temp == a3){
if(chose==true){t1.setText("");t2.setText("");}
t1.setText(t1.getText()+""+"3");
chose=false;

}
if(temp == a4){
if(chose==true){t1.setText("");t2.setText("");}
t1.setText(t1.getText()+""+"4");
chose=false;
}
if(temp == a5){
if(chose==true){t1.setText("");t2.setText("");}
t1.setText(t1.getText()+""+"5");
chose=false;
}
if(temp == a6){
if(chose==true){t1.setText("");t2.setText("");}
t1.setText(t1.getText()+""+"6");
chose=false;
}
if(temp == a7){
if(chose==true){t1.setText("");t2.setText("");}
t1.setText(t1.getText()+""+"7");
chose=false;
}
if(temp == a8){
if(chose==true){t1.setText("");t2.setText("");}
t1.setText(t1.getText()+""+"8");
chose=false;
}
if(temp == a9){
if(chose==true){t1.setText("");t2.setText("");}
t1.setText(t1.getText()+""+"9");
chose=false;
}
if(temp == a0){
if(chose==true){t1.setText("");t2.setText("");}
t1.setText(t1.getText()+""+"0");
chose=false;
}
if(temp==b3){
cliks=true;
for(int i=0;i<t1.getText().length();i++){
if('.'==t1.getText().charAt(i)){
cliks=false;
break;
}
if(cliks==true){
t1.setText(t1.getText()+".");
}
}
}

if(temp== c1){
count=Double.parseDouble(t1.getText());
t1.setText("");
fuhao = "+";
}
if(temp== c2){
count=Double.parseDouble(t1.getText());
t1.setText("");
fuhao = "-";
}
if(temp== c3){
count=Double.parseDouble(t1.getText());
t1.setText("");
fuhao = "*";
}

if(temp== c4){
count=Double.parseDouble(t1.getText());
t1.setText("");
fuhao = "÷";
}
if(temp==b1){
t1.setText("");
t2.setText("");
}
if(temp==b2){
String s=t1.getText();
t1.setText("");
for(int i=0;i<s.length()-1;i++){
char a = s.charAt(i);
t1.setText(t1.getText()+a);
}
}
if(temp== b4){
count2=Double.parseDouble(t1.getText());
t1.setText("");
if(fuhao=="+"){
//int sum=count+count2;
t1.setText(count+""+fuhao+""+count2+""+"=");
t2.setText(count+count2+"");
chose=true;
}
if(fuhao=="-"){
//int sum=count+count2;
t1.setText(count+""+fuhao+""+count2+""+"=");
t2.setText(count-count2+"");
chose=true;
}
if(fuhao=="*"){
//int sum=count+count2;
t1.setText(count+""+fuhao+""+count2+""+"=");
t2.setText(count*count2+"");
chose=true;
}
if(fuhao=="÷"){
//int sum=count+count2;
if(count2==0){
t1.setText(count+""+fuhao+""+count2+"");
t2.setText("除数不能为0");
return;
}
t1.setText(count+""+fuhao+""+count2+""+"=");
t2.setText(count/count2+"");
chose=true;
}

}
}
}追问有详解吗?

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
...古代 近代 现代 当代 远古 。这些怎么传递的,还有评价是什么... 游离甲状腺素偏高是怎么回事 叶问前传有那些人拍? 有叶问前传里叶天赐这个人么 请问:1、小学教育与初中教育有什么不同? 2、小学教育与师范教育又... 北师大附小为什么好 考科二需要注意哪些 华为手机荣耀9和v9哪个比较好 万能钥匙wufai连网网速怎么样 我用迅雷下载了高清电影,用魔影工厂转换格式,出来一个窗口,是迅雷的让... 解压文件之后,只有一个txt文件,其他的什么都没有,请问这是什么原因 java编程 完成下图所示的图形用户界面设计,要求在界面中输入个人的班级、学号、姓名信息后,点击“ java图形用户界面设计 求助:java图形用户界面程序设计题。 java设计和实现图形用户界面的主要工作有哪些 Java图形用户界面程序设计作业 JAVA程序设计这门课程第十章图形用户界面的知识点有哪些? 怎么样在word文档中上下标数字? 刚学了C,C++,请问如何做图形用户界面程序 图形用户界面(GUI)包含哪些内容? java的图形用户界面程序设计 CAD图形用户界面的设计应包含哪些内容? 什么是图形用户界面,设计方法有几种 怎样延长空调的使用时间 空调如何使用才能更耐久? 怎样使空调只能用遥控器开启,而不能手动开启? 怎样才能使空调接受不到遥控信号 怎样使空调无风 没有遥控器如何使空调处于待机 在不破坏空调的情况下,怎么让空调不能开机? 郑州哪里有买竹笆的市场?集中点的市场?谢谢 求一个java程序 基于Swing的图形用户界面设计 周公解梦梦见有好多孩子穿红衣服,老多了特别吓人。还给我好多钱,还有冥币。还梦见屎。 女朋友对你说;UP~是什么意思?请帮帮忙、谢谢! 电脑usb怎样改成快充 最近常听人讲UP,这UP是什么意思啊常看人留言啊,发言啊都讲UP,到底什么意思 电脑usb变快充 大家好!请问白度贴吧!吧友讲up是什么意思啊! UP是什么意思``能说清楚下么` 在贴吧中的&quot;up&quot;是什么意思? 华硕K450JF笔记本的USB3.0快速充电功能用不了 女生说好呢是什么意思 女生tpl是什么意思啊? 女生聊天说哈哈颂是什么意思? 快冲手机可以用电脑的USb接口充电吗,会不会对手机有损害 女生一般聊天,说噢。是什么意思。 手机QQ动态怎么转发 怎么把qq动态分享到微信好友中去 少儿舞蹈学校周年庆优惠活动该怎么办,有那些方案可供参考 急求招生方案,做美术培训班的?美术机构搞周年庆活动如何让更多人参加_百度问一问 办公场所消防要求是根据的哪条法规?