消息对话框向用户提供信息。消息对话框是使用JOptionPane创建的。showMessageDialog()方法。
null
我们称之为 静态showMessageDialog() 方法创建消息对话框。我们提供对话框的父级、消息文本、标题和消息类型。消息类型是以下常量之一:
- 错误消息
- 警告信息
- 问题(留言)
- 信息和信息
使用的方法:
- setLayout(…): 方法帮助我们设置容器的布局,通常是一个JPanel,也就是说FlowLayout、BorderLayout、GridLayout、null布局,或者我们想要添加到容器上的任何布局。
- 挫折(…): 方法用于设置JButton等组件的位置和大小,并且只有在JFrame中使用null布局时才有用。
- setVisible(…): 方法用于设置JFrame的可见性状态。
- setVisible(true)将设置JFrame对用户可见。
- setVisible(false)将设置JFrame对用户不可见。
- getSource(): 事件对象包含对生成事件的组件的引用。要从事件对象中提取该引用,我们使用getSource()方法。
- 添加(): 它用于将JButton等组件添加到JFrame的容器中。
下面是上述显示消息对话框方法的实现:
JAVA
// Java program to show ERROR_MESSAGE dialog // in Java. Importing different Package. import java.awt.*; import java.awt.event.*; import javax.swing.*; class Demo extends JFrame implements ActionListener { // Declaration of object of JButton class. JButton b1; // Constructor of Demo class Demo() { // Setting layout as null of JFrame. this .setLayout( null ); // Initialization of object of "JButton" class. b1 = new JButton( "Button 1" ); // Setting Bounds of a JButton. b1.setBounds( 130 , 05 , 100 , 50 ); //"this" keyword in java refers to current object. // Adding JButton on JFrame. this .add(b1); // Adding Listener toJButton. b1.addActionListener( this ); } // Override Method public void actionPerformed(ActionEvent evt) { if (evt.getSource() == b1) { // Code To popup an ERROR_MESSAGE Dialog. JOptionPane.showMessageDialog( this , "Enter a valid Number" , "ERROR" , JOptionPane.ERROR_MESSAGE); } } } class MessageDialogs1 { // Driver code public static void main(String args[]) { // Creating Object of demo class. Demo f = new Demo(); // Setting Bounds of a Frame. f.setBounds( 200 , 200 , 400 , 300 ); // Setting Resizable status of frame as false f.setResizable( false ); // Setting Visible status of frame as true. f.setVisible( true ); } } |
输出:
JAVA
// Java program to show WARNING_MESSAGE dialog // in Java. Importing different Package. import java.awt.*; import java.awt.event.*; import javax.swing.*; class Demo extends JFrame implements ActionListener { // Declaration of object of JButton class. JButton b1; // Constructor of Demo class Demo() { // Setting layout as null of JFrame. this .setLayout( null ); // Initialization of object of "JButton" class. b1 = new JButton( "Button 2" ); // Setting Bounds of a JButton. b1.setBounds( 130 , 05 , 100 , 50 ); //"this" keyword in java refers to current object. // Adding JButton on JFrame. this .add(b1); // Adding Listener toJButton. b1.addActionListener( this ); } // Override Method public void actionPerformed(ActionEvent evt) { if (evt.getSource() == b1) { // Code To popup an WARNING_MESSAGE Dialog. JOptionPane.showMessageDialog( this , "Enter a valid String" , "WARNING" , JOptionPane.WARNING_MESSAGE); } } } class MessageDialogs2 { // Driver code public static void main(String args[]) { // Creating Object of demo class. Demo f = new Demo(); // Setting Bounds of a Frame. f.setBounds( 200 , 200 , 400 , 300 ); // Setting Resizable status of frame as false f.setResizable( false ); // Setting Visible status of frame as true. f.setVisible( true ); } } |
输出:
JAVA
// Java program to show QUESTION_MESSAGE // dialog in Java. Importing different Package. import java.awt.*; import java.awt.event.*; import javax.swing.*; class Demo extends JFrame implements ActionListener { // Declaration of object of JButton class. JButton b1; // Constructor of Demo class Demo() { // Setting layout as null of JFrame. this .setLayout( null ); // Initialization of object of "JButton" class. b1 = new JButton( "Button 3" ); // Setting Bounds of a JButton. b1.setBounds( 130 , 05 , 100 , 50 ); //"this" keyword in java refers to current object. // Adding JButton on JFrame. this .add(b1); // Adding Listener toJButton. b1.addActionListener( this ); } // Override Method public void actionPerformed(ActionEvent evt) { if (evt.getSource() == b1) { // Code TO popup a QUESTION_MESSAGE Dialog. JOptionPane.showMessageDialog( this , "Do you want to quit" , "Question" , JOptionPane.QUESTION_MESSAGE); } } } class MessageDialogs3 { // Driver code public static void main(String args[]) { // Creating Object of demo class. Demo f = new Demo(); // Setting Bounds of a Frame. f.setBounds( 200 , 200 , 400 , 300 ); // Setting Resizable status of frame as false f.setResizable( false ); // Setting Visible status of frame as true. f.setVisible( true ); } } |
输出:
JAVA
// Java program to show INFORMATION_MESSAGE // dialog in Java. Importing different Package. import java.awt.*; import java.awt.event.*; import javax.swing.*; class Demo extends JFrame implements ActionListener { // Declaration of object of JButton class. JButton b1; // Constructor of Demo class Demo() { // Setting layout as null of JFrame. this .setLayout( null ); // Initialization of object of "JButton" class. b1 = new JButton( "Button 4" ); // Setting Bounds of a JButton. b1.setBounds( 130 , 05 , 100 , 50 ); //"this" keyword in java refers to current object. // Adding JButton on JFrame. this .add(b1); // Adding Listener toJButton. b1.addActionListener( this ); } // Override Method public void actionPerformed(ActionEvent evt) { if (evt.getSource() == b1) { // Code To popup an INFORMATION_MESSAGE Dialog. JOptionPane.showMessageDialog( this , "You Pressed Button FOUR" , "INFORMATION" , JOptionPane.INFORMATION_MESSAGE); } } } class MessageDialogs4 { // Driver code public static void main(String args[]) { // Creating Object of demo class. Demo f = new Demo(); // Setting Bounds of a Frame. f.setBounds( 200 , 200 , 400 , 300 ); // Setting Resizable status of frame as false f.setResizable( false ); // Setting Visible status of frame as true. f.setVisible( true ); } } |
输出:
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END