package picture; import java.awt.EventQueue; import java.awt.Font; import javax.swing.JFrame; import java.awt.BorderLayout; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JTextArea; import javax.swing.JPanel; import java.awt.GridLayout; import java.awt.JobAttributes; import javax.swing.JButton; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class JDialogDemo { private JFrame frame; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { JDialogDemo window = new JDialogDemo(); window.frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the application. */ public JDialogDemo() { initialize(); } /** * Initialize the contents of the frame. */ private void initialize() { frame = new JFrame(); frame.setBounds(100, 100, 573, 350); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(new BorderLayout(0, 0)); JLabel label = new JLabel("\u8BF7\u9009\u62E9\u64CD\u4F5C\uFF1A"); frame.getContentPane().add(label, BorderLayout.NORTH); JTextArea textArea = new JTextArea(); frame.getContentPane().add(textArea, BorderLayout.CENTER); JPanel panel = new JPanel(); frame.getContentPane().add(panel, BorderLayout.SOUTH); panel.setLayout(new GridLayout(1, 0, 0, 0)); JPanel panel_1 = new JPanel(); panel.add(panel_1); JButton btnNewButton = new JButton("\u63D0\u793A"); /** * JOptionPane.showMessageDialog的三个参数第一个参数是窗口弹出的位置,第二个是消息内容,第三个是标题,第四个是提供的按钮选项(消息类型) **/ btnNewButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { JOptionPane.showMessageDialog(btnNewButton, "撸起袖子加油干", "消息", JOptionPane.INFORMATION_MESSAGE); textArea.append("显示消息对话框\n"); } }); panel.add(btnNewButton); JButton btnNewButton_1 = new JButton("\u8F93\u5165"); btnNewButton_1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String strTemp = JOptionPane.showInputDialog(btnNewButton_1, "请输入一个数值:");// 输入类型 try { double num = Double.parseDouble(strTemp); // 把字符串转换为double类型 // 在文本框中追加内容 textArea.append(num + "+" + num + "=" + (num + num) + "\n"); } catch (NumberFormatException e1) { textArea.append(strTemp + "不是数值字符串"); } } }); panel.add(btnNewButton_1); JButton btnNewButton_2 = new JButton("\u786E\u8BA4\u6E05\u7A7A"); btnNewButton_2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int r = JOptionPane.showConfirmDialog(btnNewButton_2, "您确认要删除文本框中的内容吗?", "确认", JOptionPane.YES_NO_OPTION); if (r == JOptionPane.YES_OPTION) { textArea.setText(""); } else { textArea.append("\n+(取消清除内容)+\n"); } } }); panel.add(btnNewButton_2); JButton btnNewButton_3 = new JButton("\u9009\u62E9\u5B57\u4F53"); btnNewButton_3.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Object[] fonts = { "宋体", "隶书", "楷体" }; /*弹出位置,内容,标题,指定可用于对话框的选项的整数,指定消息种类的整数,在对话框中显示的图标,知识用户可能选择的对象组成的数组,如果对象是组件则可以正确呈现,对话框默认选择的对象(前提是第七个参数的对象,前一个)*/ int option=JOptionPane.showOptionDialog(btnNewButton_3, "选择字体", "选择", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, fonts, fonts[0]); if(option!=JOptionPane.CLOSED_OPTION) { textArea.append("显示选择对话框!"); textArea.setFont(new Font(fonts[option].toString(),Font.BOLD,20)); } } }); panel.add(btnNewButton_3); JPanel panel_2 = new JPanel(); panel.add(panel_2); } }
(SWING)JDialog的运用
猜你喜欢
转载自blog.csdn.net/qq_37334150/article/details/80243463
今日推荐
周排行