java confirmdialog_使用showConfirmDialog显示确认框

packagecom.siwuxie095.showdialog;

importjava.awt.BorderLayout;

importjava.awt.EventQueue;

importjavax.swing.JFrame;

importjavax.swing.JOptionPane;

importjavax.swing.JPanel;

importjavax.swing.UIManager;

importjavax.swing.UnsupportedLookAndFeelException;

importjavax.swing.border.EmptyBorder;

importcom.sun.java.swing.plaf.windows.WindowsLookAndFeel;

importjavax.swing.JButton;

importjava.awt.event.MouseAdapter;

importjava.awt.event.MouseEvent;

public classTestConfirmDialog extendsJFrame {

privateJPanel contentPane;

/**

* Launch the application.

*/

public staticvoidmain(String[] args) {

EventQueue.invokeLater(newRunnable() {

publicvoidrun() {

try{

TestConfirmDialog frame = newTestConfirmDialog();

frame.setVisible(true);

} catch(Exception e) {

e.printStackTrace();

}

}

});

}

/**

* Create the frame.

*/

publicTestConfirmDialog() {

try{

UIManager.setLookAndFeel(newWindowsLookAndFeel());

} catch(UnsupportedLookAndFeelException e1) {

e1.printStackTrace();

}

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setBounds(100, 100, 450, 300);

contentPane = newJPanel();

contentPane.setBorder(newEmptyBorder(5, 5, 5, 5));

contentPane.setLayout(newBorderLayout(0, 0));

setContentPane(contentPane);

JButton btnshowconfirmdialog = newJButton("显示确认框(showConfirmDialog)");

//为按钮添加鼠标点击事件

btnshowconfirmdialog.addMouseListener(newMouseAdapter() {

@Override

publicvoidmouseClicked(MouseEvent e) {

//直接通过静态方法调用,指定父级窗体和信息

//返回值是int类型,创建以接收返回值

//没有关闭确认框时,后面的主窗体是完全无法操作的(即阻塞)

// int value=JOptionPane.showConfirmDialog(TestConfirmDialog.this,

// "你确认退出吗?");

//

// if (value==JOptionPane.YES_OPTION) {

// System.out.println("你选择了是");

// System.exit(0);

// }else if (value==JOptionPane.NO_OPTION) {

// System.out.println("你选择了否");

// }else if (value==JOptionPane.CANCEL_OPTION) {

// System.out.println("你选择了取消");

// }else if (value==JOptionPane.CLOSED_OPTION) {

// System.out.println("你直接将窗体关闭了,没有选择");

// }

//需要指定父级窗体,信息,标题,选项类型,信息类型

intvaluex=JOptionPane.showConfirmDialog(TestConfirmDialog.this,

"你确认要退出程序吗?", "请确认",

JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);

//判断时要根据第四个参数optionType

//可以选择一个最有用的按钮来判断它的值并处理即可,不用全部都判断

if(valuex==JOptionPane.YES_OPTION) {

System.exit(0);

}

}

});

btnshowconfirmdialog.setFocusable(false);

contentPane.add(btnshowconfirmdialog, BorderLayout.NORTH);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要自定义 Java Swing 的 ConfirmDialog,您需要创建一个新的 JDialog,并在其添加您需要的组件。您可以添加标签、按钮和其他控件来创建您希望的外观和行为。 以下是一个简单的示例,展示如何创建一个自定义 ConfirmDialog: ```java import javax.swing.*; import java.awt.*; import java.awt.event.*; public class CustomConfirmDialog extends JDialog { private boolean answer; public CustomConfirmDialog(JFrame parent, String title, String message) { super(parent, title, true); JLabel label = new JLabel(message); JButton yesButton = new JButton("Yes"); JButton noButton = new JButton("No"); yesButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { answer = true; dispose(); } }); noButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { answer = false; dispose(); } }); JPanel panel = new JPanel(new GridLayout(2, 1)); panel.add(label); JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER)); buttonPanel.add(yesButton); buttonPanel.add(noButton); panel.add(buttonPanel); getContentPane().add(panel, BorderLayout.CENTER); setDefaultCloseOperation(DISPOSE_ON_CLOSE); pack(); setLocationRelativeTo(parent); setVisible(true); } public boolean getAnswer() { return answer; } } ``` 这个例子创建了一个新的 JDialog,其包含了一个文本标签和两个按钮(“Yes”和“No”)。当用户单击其一个按钮时,JDialog 会关闭,并且您可以使用 getAnswer() 方法来获取用户的响应(true 表示用户单击了“Yes”按钮,false 表示用户单击了“No”按钮)。 要使用这个自定义 ConfirmDialog,您可以像下面这样调用它: ```java CustomConfirmDialog dialog = new CustomConfirmDialog(frame, "Confirm", "Are you sure you want to continue?"); if (dialog.getAnswer()) { // User clicked "Yes" } else { // User clicked "No" } ``` 这里的 frame 是您的 JFrame,它是新 JDialog 的父级。您可以根据需要使用不同的窗体或面板作为您的父级。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值