Swing JDialogTest实现

本文介绍了一个使用 Java Swing 创建多种对话框的示例程序,包括消息对话框、确认对话框、选项选择对话框及输入对话框。通过具体代码展示了如何响应按钮点击并触发不同类型的对话框。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class JDialogTest extends JFrame implements ActionListener{
JButton b1=new JButton("showMessageDialog");
JButton b2=new JButton("showConfirmDialog");
JButton b3=new JButton("showOptionDialog");
JButton b4=new JButton("showInputDialog");
JDialogTest(){
getContentPane().setLayout(new GridLayout(2,2));
getContentPane().add(b1);
getContentPane().add(b2);
getContentPane().add(b3);
getContentPane().add(b4);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
setSize(250,200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==b1)
JOptionPane.showMessageDialog(null,"你点击了消息对话框","showMessageDialog",JOptionPane.INFORMATION_MESSAGE);
else if(e.getSource()==b2)
JOptionPane.showConfirmDialog(null,"你点击了确认对话框","showConfirmDialog",JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.INFORMATION_MESSAGE);
else if(e.getSource()==b3){
Object[] str={"asp","vb","java"};
JOptionPane.showOptionDialog(null,"请选择一门课程","showOptionDialog",JOptionPane.DEFAULT_OPTION,JOptionPane.WARNING_MESSAGE,null,str,str[1]);
}
else if(e.getSource()==b4){
String inputValue = JOptionPane.showInputDialog("Please input a value"); }
}
public static void main(String[] args){
new JDialogTest();
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值