求java的GUI小程序(2个数的和)

求java的GUI小程序(2个数的和),第1张

import javax.swing.*

import javax.swing.JButton

import java.awt.*

import java.awt.event.*

import java.awt.event.ActionEvent

import java.awt.event.ActionListener

public class Calculator

{

JFrame f

JTextField op1,op2,op,op3

JPanel p1,p2

public void display()

{

f = new JFrame("加法计算器")

f.setSize(280,300)f.setLocation(320,240)

f.setBackground(Color.lightGray)

f.setLayout(new FlowLayout(FlowLayout.LEFT))

f.setVisible(true)

op1= new JTextField("0",4)

op2= new JTextField("0",4)

op3= new JTextField("0",4)

p1=new JPanel()

p1.add(op1)

p1.add(new JLabel("+"))

p1.add(op2)

p1.add(new JLabel("="))

p1.add(op3)

f.add(p1)

JButton button = new JButton()

button.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

op3.setText(""+(Integer.parseInt(op1.getText())+Integer.parseInt(op2.getText())))

}

})

button.setText("提交")

p1.add(button)

p2=new JPanel()

f.add(p2)

f.addWindowListener(new WinClose())

}

public static void main(String arg[])

{

(new Calculator()).display()

}

}

class WinClose implements WindowListener

{

public void windowClosing(WindowEvent e)

{

System.exit(0)

}

public void windowOpened(WindowEvent e){ } //窗口打开时调

public void windowIconified(WindowEvent e){ }//差掘窗口图标化时调用

public void windowDeiconified(WindowEvent e){ }//窗口非图标化时调用

public void windowClosed(WindowEvent e){ }//窗口关闭时调用

public void windowActivated(WindowEvent e){ }//窗口激活时调用

public void windowDeactivated(WindowEvent e){ }//窗口非激活时调用

}

这是以前一个程序临时修改的,你看符合要求吗,不符合再给你写一个,没出纳袜界面的话最小化然后回复就洞庆激刷新出啦了

监听键盘是可以的,自己搜一下keylistener使用,昌做就是简单的事件的使用方式,看个例子就明白了。

但你这样做根本就是在折磨你的数据库,每输一个字都调用查询,做着玩是可以,数据量大了不行的。解决方案就是想baidu一样把你要搜索的表读到内存中做成内存数据库,耐者衡这能提高几百倍的嫌大速度。

给你找了一个,我试过,可以用

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=0i<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=0i<s.length()-1i++){

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

}

}

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/12554517.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-26
下一篇 2023-05-26

发表评论

登录后才能评论

评论列表(0条)

保存