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()

}

}

你可以在这上面放上两个label,长条的label实现拖动效果,后面那个实现关闭效果

也可以把logo这个label设置成可拖动的效果,

如果你frame是空布局,那么就不需要改成panel,直接jl.setBounds(x, y, width, height)自己算坐标即可,还需要setComponentZOrder(comp, index)置顶,否则被logo挡住鼠标事件不起作用(label实现按钮的动作只能用鼠标事件)

可拖动效果比较复杂,曾经自己做过,简单说下思路,用鼠标按下、抬起、移动事件,设置一个状态位isMouseDown,鼠标按下为true,抬起为false,只有当true的时候移动事件计算偏移量,然后frame.setLocation(x, y)


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

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

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

发表评论

登录后才能评论

评论列表(0条)

    保存