Swing学习21:Java Swing JOptionPane

对话框通常用作从用户处接收附加信息,或者提供发生了某种事件的通知。Java 提供了 JOptionPane 类,用来创建标准对话框,也可以通过扩展 JDialog 类创建自定义的对话框。JOptionPane 类可以用来创建 4 种类型的标准对话框:确认对话框、消息对话框、输入对话框和选项对话框。

确认对话框

确认对话框显示消息,并等待用户单击“确定”按钮来取消对话框,该对话框不返回任何值。而确认对话框询问一个问题,需要用户单击合适的按钮做出响应。确认对话框返回对应被选按钮的值。

创建确认对话框的方法如下:

public static int showConfirmDialog(Component parentComponent,Object message,String title,int optionType,int messageType,Icon icon)

参数 parentComponent、message、title、messageType 和 icon 与 showMessageDialog() 方法中的参数的含义相同。其中,只有 parentComponent 和 message 参数是必需的,title 的默认值为“选择一个选项”。messageType 的默认值是 QUESTION_MESSAGE。optionType 参数用于控制在对话框上显示的按钮,可选值如下:

  • 0 或 JOptionPane.YES_NO_OPTIION。
  • 1 或 JOptionPane.YES_NO_CANCEL_0PTII0N。
  • 2 或 JOptionPane.OK_CANCEL_OPTIION。

例如,使用 showCon&mDialog() 方法创建 3 个确认对话框,该方法中指定的参数个数和参数值都是不同的,语句如下:

JOptionPane.showConfirmDialog(p,"确定要删除吗?","删除提示",0);
JOptionPane.showConfirmDialog(p,"确定要删除吗?","删除提示",1,2);
ImageIcon icon=new ImageIcon("F:\\pic\\n63.gif");
JOptionPane.showConfirmDialog(p,"确定要删除吗?","删除提示",2,1,icon);

这 3 条语句所实现的对话框分别如图 1 所示。
在这里插入图片描述
showConfirmDialog() 方法返回所选选项对应的值,这些值可以是整数或常量值,如下:

  • 0 或 JOptionPane.YES_OPTIION。
  • 1 或 JOptionPane.NO_OPTIION。
  • 2 或 JOptionPane.CANCEL_OPTIION。
  • 0 或 JOptionPane.OK_OPTIION。
  • -1 或 JOptionPane.CLOSED_OPTIION。

提示:除了 CLOSED_OPTIION 外,其他常量值都对应于激活的按钮。CLOSED_OPTIION 表示对话框在没有任何按钮激活的情况下关闭,例如单击对话框上的关闭图标按钮。

消息对话框

消息对话框显示一条提示或警告用户的信息,并等待用户单击 OK 或“确定”按钮以关闭对话框。创建消息对话框的方法如下:

public static void showMessageDialog(Component parentComponent,Object message,String title,int messageType,Icon icon)

其中,只有 parentComponent 参数和 message 参数是必须指定的。parentComponent 可以是任意组件或者为空;message 用来定义提示信息,它是一个对象,但是通常使用字符串表示;title 是设置对话框标题的字符串;messageType 是以下整型或常量中的一个。

  • 0 或 JOptionPane.ERROR_MESSAGE。
  • 1 或 JOptionPane.INFORMATION_MESSAGE。
  • JOptionPane.PLAIN_MESSAGE。
  • 2 或 JOptionPane.WARNING_MESSAGE。
  • 3 或 JOptionPane.QUESTION_MESSAGE。

默认情况下,messageType 的值是 JOptionPane.INFORMATION_MESSAGE。除类型 PLAIN_MESSAGE外,每种类型都有相应的图标,也可以通过 icon 参数提供自己的图标。

例如,下面的代码演示了不同的 messageType 取值实现的效果。

JOptionPane.showMessageDialog(p,"用户名或密码错误!","错误 ",0);
JOptionPane.showMessageDialog(p,"请注册或登录...","提示",1);
JOptionPane.showMessageDialog(p,"普通会员无权执行删除操作!","警告",2);
JOptionPane.showMessageDialog(p,"你是哪一位?请输入用户名","问题",3);
JOptionPane.showMessageDialog(p,"扫描完毕,没有发现病毒!","提示",JOptionPane.PLAIN_MESSAGE);

第一行语句表示创建一个错误对话框。第二行语句表示创建一个提示对话框。第三行语句表示创建一个警告对话框。第四行语句表示创建一个问题对话框。第五行语句表示创建一个无图标对话框。这 5 条语句创建的对话框效果分别如图 2 所示。
在这里插入图片描述

输入对话框

输入对话框用于接收用户的输入。输入组件可以由文本框、下拉列表或者列表框进行实现。如果没有指定可选值,那么就使用文本框接收输入;如果指定了一组可选值,可选值的个数小于 20,那么将使用下拉列表显示;如果可选值的个数大于或等于 20,那么这些可选值将通过列表框显示。

创建输入对话框的方法如下:

public static String showInputDialog(Component parentComponent,Object message,String title,int messageType)
public static Object showInputDSalog(Component parentComponent,Object message,String title,int messageType,Icon icon,Object[] selectionValue,Object initValue)

其中,第一个 showInputDialog() 方法用于使用文本框输入,第二个 showInputDialog() 方法用于下拉列表或列表框的显示方式。参数 parentComponent 是必需的,message 默认为空,title 默认值为“输入”,messageType 的值默认为 3 或 JOptionPane.QUESTION_MESSAGE。

例如,使用 showInputDialog() 方法创建两个输入文本框,语句如下:

JOptionPane.showInputDialog(panel,"请输入用户名","输入用户名",1);
String[] str={"admin","maxianglin","calcl23456","adminl23"};
JOptionPane.showInputDialog(panel,"请选择用户名","选择用户名",1,null,str,str[0]);

第一个对话框没有指定列表值,那么将显示文本框;第二个对话框值显示为下拉列表的形式,如图 3 所示。
在这里插入图片描述
提示:showInputDialog() 方法中没有 optionType 参数,表示输入对话框的按钮是不可以设置的,通常显示“确定”和“取消”按钮。

选项对话框

选项对话框允许用户自己定制按钮内容。创建选项对话框的方法如下:

public static int showOptionDialog(Component parentComponent,Object message,String title,int optionType,int messageType,icon icon,Object[] options,Object initValue)

其中,使用 options 参数指定按钮,initValue 参数用于指定默认获得焦点的按钮。该方法返回表明激活的按钮的一个整型值。

例如,创建一个 JButton 按钮数组,然后使用 showOptionDialog() 方法创建一个选项对话框,根据这个 JButton 数组来显示对话框的按钮,如下:

JButton[] bs={new JButton("确定"),new JButton("取消"),new JButton("重置")};
JOptionPane.showOptionDialog(panel,"请选择其中的一项:","选择",1,3,null,bs,bs[0]);

显示效果如图 4 所示。
在这里插入图片描述

谢谢观看

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
package com.shou.loginfjame; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Component; import java.awt.Container; import java.awt.Cursor; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Font; import java.awt.Image; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.SQLException; import java.util.List; import javax.swing.BorderFactory; import javax.swing.Box; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.xml.bind.util.ValidationEventCollector; import com.shou.LoginUtil.LoginUser; import com.shou.dao.LoginDao; import com.shuo.util.ValidCode; public class LoginFjame extends JFrame implements ActionListener { private JFrame frame = new JFrame("登录"); private JPanel panel = new JPanel(); private JLabel tiel = new JLabel("龍丶逸小说登录系统"); // 创建标题 private JLabel userLabel = new JLabel("用户名:"); // 创建UserJLabel private JTextArea userText=new JTextArea("请输入内容",7, 30); // 获取登录名 private JLabel passLabel = new JLabel("密 码:"); // 创建PassJLabel private JPasswordField passText = new JPasswordField(20); // 密码框隐藏 private JLabel verCodeLa = new JLabel("验证码:"); // 验证码 private JTextField inputCode = new JTextField(); // 验证码框 private ValidCode vcode = new ValidCode(); // 验证码内容 JTextField jt_code; private JButton loginButton = new JButton("登录"); // 创建登录按钮 private JButton registerButton = new JButton("注 册"); // 创建注册按钮 private JButton newPasswordButton = new JButton("忘记密码"); // 创建注册按钮 private JButton exitButton = new JButton("退出"); JTextField field = null; public LoginFjame() { System.out.println("====================================="); System.out.println("== 龍丶逸小说系统 =="); System.out.println("== V1.1.1.0 =="); System.out.println("====================================="); WinLogin(); } public void WinLogin() { panel.setLayout(null); // 设置布局为 null // 创建标题名称 this.tiel.setFont(new Font("宋体", 1, 20)); this.tiel.setBounds(150, 30, 300, 25); this.panel.add(this.tiel); // 创建 UserJLabel this.userLabel.setFont(new Font("宋体", 1, 13)); this.userLabel.setBounds(70, 80, 80, 25); this.panel.add(userLabel); // 创建文本域用于用户输入 this.userText.setBounds(145, 80, 165, 25); this.panel.add(this.userText); // 注册 this.registerButton.setFont(new Font("宋体", 1, 15)); this.registerButton.setContentAreaFilled(false); this.registerButton.setBorderPainted(false); /* registerButton.setBackground(Color.red); */ this.registerButton.setBounds(320, 80, 100, 25); this.panel.add(this.registerButton); // 变成小手 this.registerButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); // 创建PassJLabel this.passLabel.setFont(new Font("宋体", 1, 13)); this.passLabel.setBounds(70, 110, 80, 25); this.panel.add(this.passLabel); // 密码输入框 隐藏 this.passText.setBounds(145, 110, 165, 25); this.panel.add(this.passText); // 忘记密码 this.newPasswordButton.setFont(new Font("宋体", 1, 15)); this.newPasswordButton.setContentAreaFilled(false); this.newPasswordButton.setBorderPainted(false); /* registerButton.setBackground(Color.red); */ this.newPasswordButton.setBounds(320, 110, 100, 25); this.panel.add(this.newPasswordButton); // 变成小手 this.newPasswordButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); // 验证码code this.verCodeLa.setFont(new Font("宋体", 1, 13)); this.verCodeLa.setBounds(70, 140, 80, 25); this.panel.add(this.verCodeLa); // 验证码框 this.inputCode.setBounds(145, 140, 165, 25); this.panel.add(this.inputCode); // 验证码图片 this.vcode.setBounds(320, 140, 165, 25); this.panel.add(this.vcode); System.out.println(this.vcode); // 创建登录按钮 this.loginButton.setFont(new Font("宋体", 1, 15)); this.loginButton.setBounds(95, 190, 80, 25); this.panel.add(this.loginButton); // 变成小手 this.loginButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); // 退出按钮 this.exitButton.setFont(new Font("宋体", 1, 15)); this.exitButton.setBounds(230, 190, 80, 25); this.panel.add(this.exitButton); // 变成小手 this.exitButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); // 设置窗体的位置及大小 this.frame.setSize(460, 355); frame.setLocationRelativeTo(null); // 在屏幕中居中显示 frame.add(this.panel); // 添加面板 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置X号后关闭 //设置按钮 this.registerButton.addActionListener(this); //注册按钮 this.newPasswordButton.addActionListener(this); //忘记密码 this.loginButton.addActionListener(this); //登录 this.exitButton.addActionListener(this); //退出 // 往窗体里放其他控件 frame.setVisible(true); // 设置窗体可见 } @Override public void actionPerformed(ActionEvent e) { JButton bt = (JButton) e.getSource(); // 获取按钮信息 String str = bt.getText(); // 获取用户名 String name = this.userText.getText().trim(); // 获取密码 String password = this.passText.getText().trim(); // 获取验证码 String code = this.inputCode.getText().trim(); // 获取jsp验证码 String vcode = this.vcode.getCode(); // 登录 if (str.equals("登录")) { System.out.println("登录"); // 验证码转为大写 String Dcode = code.toUpperCase(); String Dvcode = vcode.toUpperCase(); // 验证码判断 if (Dcode.equals(Dvcode)) { //获取页面的用户名 String username=this.userText.getText().trim(); // 根据用户名查看是否有该用户 try { List loginUser=new LoginDao().queryAll(username); String a=loginUser.toString(); System.out.println(a.toString()); if(!a.toString().equals("[]")){ //密码判断 String mysqlPasword=loginUser.get(0).l_password(); if(mysqlPasword.equals(password)){ //登录成功 JOptionPane pane = new JOptionPane("登录成功"); JDialog dialog = pane.createDialog(this, "警告"); dialog.show(); }else{ JOptionPane pane = new JOptionPane("密码错误错误,请重新输入"); JDialog dialog = pane.createDialog(this, "警告"); dialog.show(); } }else{ JOptionPane pane = new JOptionPane("用户名错误,请重新输入"); JDialog dialog = pane.createDialog(this, "警告"); dialog.show(); } /*System.out.println(loginUser.toString()); String sqlUername=loginUser.get(0).getL_username();*/ /*int sqlpassword=loginUser.get(0).getL_power();*/ /*System.out.println("loginF:"+sqlUername);*/ } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } else { JOptionPane pane = new JOptionPane("验证码错误,请重新输入"); JDialog dialog = pane.createDialog(this, "警告"); System.out.println(dialog.getFont()); dialog.show(); } } else // 退出 if (str.equals("退出")) { System.out.println("退出"); System.exit(0); } else // 注册 if (str.equals("注 册")) { System.out.println("注 册"); } // 注册 else if (str.equals("忘记密码")) { System.out.println("忘记密码"); } else { System.out.println("异常错误"); } } public boolean isValidCodeRight() { System.out.println(this.jt_code.getText()); if (this.jt_code == null) { return false; } if (this.vcode == null) { return true; } if (this.vcode.getCode().equals(this.jt_code.getText())) { return true; } return false; } public static void main(String[] args) { new LoginFjame(); } }

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鱼小洲

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值