java设置关闭窗口按钮

java设置关闭窗口按钮,第1张

package org.t20110219.test

/**

* 修改后的代码

* 能关闭,

* 希望能帮助你

*/

import java.awt.*

import java.awt.event.*

public class MainProgram extends Frame implements ActionListener,WindowListener

{

int i

Button b1 = new Button("确定")

Button b2 = new Button("关闭")

void FlowLayoutEX()

{

this.setTitle("布局管理器")

this.setLayout(new FlowLayout())

this.add(b1)

b1.addActionListener(this)

this.add(b2)

b2.addActionListener(this)

this.addWindowListener(this)

this.setBounds(100, 100, 250, 250)

this.setVisible(true)

}

public void actionPerformed(ActionEvent e)

{

/**------------修改代码--------------*/

/**

* 修改 合理就可以了

* e.getSource() 可以得到 你点击了哪个 按钮

* 判断之后就可以 设定 对应的 *** 作了

*/

if(e.getSource()==b2){

System.exit(0)

}else{

i++

Button bi = new Button("按钮"+i)

this.add(bi)

this.show(true)

}

}

public void windowClosing(WindowEvent f)

{

System.exit(0)

}

public void windowOpened(WindowEvent f){}

public void windowClosed(WindowEvent f){}

public void windowIconified(WindowEvent f){}

public void windowDeiconified(WindowEvent f){}

public void windowActivated(WindowEvent f){}

public void windowDeactivated(WindowEvent f){}

public static void main(String args[])

{

MainProgram window = new MainProgram()

window.FlowLayoutEX()

}

}

你都会编这么多的代码了,怎么就剩下这两步不会?

import java.awt.Button

import java.awt.Color

import java.awt.FlowLayout

import java.awt.Frame

import java.awt.Label

import java.awt.TextField

import java.awt.event.ActionEvent

import java.awt.event.ActionListener

import java.awt.event.WindowAdapter

import java.awt.event.WindowEvent

public class Round extends Frame implements ActionListener {

TextField t1, t2, t3, t4

Button b1

Button btnExit

public Round() {

setLayout(new FlowLayout())

t1 = new TextField(20)

t1.setBackground(Color.orange)

t2 = new TextField(20)

t2.setBackground(Color.orange)

t3 = new TextField(20)

t3.setBackground(Color.orange)

t4 = new TextField(20)

t4.setBackground(Color.orange)

b1 = new Button("计算")

btnExit = new Button("退出")

add(new Label("输入圆的半径:"))

add(t1)

add(new Label("得出圆的直径:"))

add(t2)

add(new Label("得出圆的面积:"))

add(t3)

add(new Label("得出圆的周长:"))

add(t4)

add(b1)

add(btnExit)

addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

System.exit(0)

}

})

b1.addActionListener(this)

btnExit.addActionListener(this)

setVisible(true)

setBounds(200, 200, 200, 300)

validate()

}

public void actionPerformed(ActionEvent e) {

if(e.getSource()==b1){

double temp, r, a, c

temp = Float.parseFloat(t1.getText())

r = 2 * temp

a = 3.14 * temp * temp

c = 2 * 3.14 * temp

t2.setText(String.valueOf(r))

t3.setText(String.valueOf(a))

t4.setText(String.valueOf(c))

}

if(e.getSource()==btnExit){

System.exit(0)

}

}

public static void main(String args[]) {

new Round()

}

}

1、打开eclipse,并且建立java一个工程,具体如下代码:

addActionListene

(newActionListene

()

{

pu

licvoidactionPe

fo

med(ActionEvente)

{

dispose()

}

})

2、执行该程序查看结果,如图所示。


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

原文地址:https://54852.com/bake/11638207.html

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

发表评论

登录后才能评论

评论列表(0条)

    保存