java jdialog 取消关闭按钮_的JDialog关闭按钮事件

将WindowListener添加到JDialog,并在windowClosed设置布尔值或关闭时的东西。也有一个buttonClicked布尔值,如果他们点击了按钮,它将是真实的,如果他们点击窗口顶部的退出按钮,它将是错误的。

boolean closed;

boolean buttonClicked;

JButton exitbutton;

JDialog dialog;

...

public void actionPerformed(ActionEvent arg0) {

buttonClicked = true;

dialog.dispose();

}

...

dialog.addWindowListener(new WindowListener() {

public void windowActivated(WindowEvent arg0) {

// Do nothing

}

public void windowClosed(WindowEvent arg0) {

closed = true;

if(buttonClicked) {

//They cliked the button to close it.

} else {

// They didn't click the button, they clicked exit in the top right corner of screen.

}

}

public void windowClosing(WindowEvent arg0) {

// Do nothing

}

public void windowDeactivated(WindowEvent arg0) {

// Do nothing

}

public void windowDeiconified(WindowEvent arg0) {

// Do nothing

}

public void windowIconified(WindowEvent arg0) {

// Do nothing

}

public void windowOpened(WindowEvent arg0) {

// Do nothing

}

});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现点击按钮事件弹出子窗口,可以按照以下步骤进行: 1. 在Java中使用Swing库创建一个主窗口。 2. 在主窗口中添加一个按钮,并为其注册事件监听器。 3. 在事件监听器中实现点击按钮弹出子窗口的逻辑。 4. 在逻辑中创建一个子窗口,并设置其属性和布局。 5. 将子窗口添加到主窗口中,并显示出来。 以下是一个简单的示例代码: ``` import javax.swing.*; import java.awt.*; import java.awt.event.*; public class MainFrame extends JFrame implements ActionListener { private JButton btnOpen; private JDialog dlg; public MainFrame() { // 创建主窗口 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(400, 300); setLocationRelativeTo(null); // 添加按钮 btnOpen = new JButton("打开子窗口"); btnOpen.addActionListener(this); getContentPane().add(btnOpen, BorderLayout.CENTER); // 显示主窗口 setVisible(true); } @Override public void actionPerformed(ActionEvent e) { // 创建子窗口 dlg = new JDialog(this, "子窗口", true); dlg.setSize(200, 100); dlg.setLocationRelativeTo(null); // 添加标签 JLabel label = new JLabel("这是子窗口"); dlg.getContentPane().add(label, BorderLayout.CENTER); // 显示子窗口 dlg.setVisible(true); } public static void main(String[] args) { new MainFrame(); } } ``` 在上面的代码中,我们创建了一个继承自JFrame的主窗口,并添加了一个按钮。当按钮被点击时,会触发我们实现的ActionListener接口的actionPerformed方法,在该方法中创建一个JDialog子窗口,并显示出来。最后,我们将主窗口显示出来。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值