消息弹出框案例:
package cn.dzz.swing; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; public class Demo06 { JFrame jFrame = new JFrame("消息弹出框案例"); JTextArea jTextArea = new JTextArea(6, 30); JButton jButton = new JButton(new AbstractAction("弹出消息框") { @Override public void actionPerformed(ActionEvent e) { // 弹出一个对话框,显示文本域中输入的内容 String text = jTextArea.getText(); JOptionPane.showMessageDialog( jFrame, text, "消息提示", JOptionPane.INFORMATION_MESSAGE ); } }); public void init(){ jFrame.add(jTextArea, BorderLayout.CENTER); jFrame.add(jButton, BorderLayout.SOUTH); jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jFrame.pack(); jFrame.setVisible(true); } public static void main(String[] args) { new Demo06().init(); } }
几种消息提示框的选择,不同的消息对应配有不同的图标提示
JOptionPane.INFORMATION_MESSAGE // JOptionPane.ERROR_MESSAGE // JOptionPane.WARNING_MESSAGE // JOptionPane.QUESTION_MESSAGE // JOptionPane.PLAIN_MESSAGE, 可以再加上 new ImageIcon(String path) 显示图像
确认对话框案例:
package cn.dzz.swing; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; public class Demo06 { JFrame jFrame = new JFrame("消息弹出框案例"); JTextArea jTextArea = new JTextArea(6, 30); JButton jButton = new JButton(new AbstractAction("弹出确认框") { @Override public void actionPerformed(ActionEvent e) { String text = jTextArea.getText(); // JOptionPane.DEFAULT_OPTION // JOptionPane.YES_NO_OPTION // JOptionPane.YES_NO_CANCEL_OPTION int result = JOptionPane.showConfirmDialog(jFrame, text, "确认对话框", JOptionPane.YES_NO_CANCEL_OPTION); switch (result) { case JOptionPane.YES_OPTION: // case JOptionPane.OK_OPTION: jTextArea.append(" 用户选择了YES 和确认的结果一样"); break; case JOptionPane.NO_OPTION: jTextArea.append(" 用户选择了NO"); break; case JOptionPane.CANCEL_OPTION: jTextArea.append(" 用户选择了 取消"); break; } } }); public void init(){ jFrame.add(jTextArea, BorderLayout.CENTER); jFrame.add(jButton, BorderLayout.SOUTH); jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jFrame.pack(); jFrame.setVisible(true); } public static void main(String[] args) { new Demo06().init(); } }
文本输入框案例:
package cn.dzz.swing; import jdk.nashorn.internal.scripts.JO; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; public class Demo06 { JFrame jFrame = new JFrame("消息弹出框案例"); JTextArea jTextArea = new JTextArea(6, 30); JButton jButton = new JButton(new AbstractAction("弹出输入框") { @Override public void actionPerformed(ActionEvent e) { String text = jTextArea.getText(); String input = JOptionPane.showInputDialog( jFrame, "请填写你的输入信息", "输入对话框", JOptionPane.INFORMATION_MESSAGE ); jTextArea.append(input + " "); } }); public void init(){ jFrame.add(jTextArea, BorderLayout.CENTER); jFrame.add(jButton, BorderLayout.SOUTH); jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jFrame.pack(); jFrame.setVisible(true); } public static void main(String[] args) { new Demo06().init(); } }
选择对话框案例:
package cn.dzz.swing; import jdk.nashorn.internal.scripts.JO; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; public class Demo06 { JFrame jFrame = new JFrame("消息弹出框案例"); JTextArea jTextArea = new JTextArea(6, 30); JButton jButton = new JButton(new AbstractAction("弹出输入框") { @Override public void actionPerformed(ActionEvent e) { String text = jTextArea.getText(); String[] strings = { "选项1", "选项2", "选项3", }; int result = JOptionPane.showOptionDialog( jFrame, "选择提示信息", "选项对话框", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, strings, "初始选项" ); jTextArea.append(strings[result] + " "); } }); public void init(){ jFrame.add(jTextArea, BorderLayout.CENTER); jFrame.add(jButton, BorderLayout.SOUTH); jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jFrame.pack(); jFrame.setVisible(true); } public static void main(String[] args) { new Demo06().init(); } }