Message dialogs provide information to the user. Message dialogs are created with the JOptionPane.showMessageDialog() method.
We call the static showMessageDialog() method of the JOptionPane class to create a message dialog. We provide the dialog’s parent, message text, title, and message type. The message type is one of the following constants :
- ERROR_MESSAGE
- WARNING_MESSAGE
- QUESTION_MESSAGE
- INFORMATION_MESSAGE
Methods Used :
- setLayout(…): method helps us to set the layout of the container, often a JPanel, to say FlowLayout, BorderLayout, GridLayout, null layout, or whatever layout we want to add on container.
- setBounds(…): method is used to set the location and size of components like JButton, and is only useful if null layout is used in JFrame.
- setVisible(…): method is used to set the Visibility status of JFrame.
- setVisible(true) will set JFrame visible to user.
- setVisible(false) will set JFrame not visible to user.
- getSource(): An event object contains a reference to the component that generated the event. To extract that reference from the event object we use getSource() Method.
- add(): It is used to add components like JButton etc, to the container of JFrame.
Below is the implementation of above discussed method to show Message Dialogs :
Tóm Tắt
Java
import
java.awt.*;
import
java.awt.event.*;
import
javax.swing.*;
class
Demo
extends
JFrame
implements
ActionListener
{
JButton b1;
Demo()
{
this
.setLayout(
null
);
b1 =
new
JButton(
"Button 1"
);
b1.setBounds(
130
,
05
,
100
,
50
);
this
.add(b1);
b1.addActionListener(
this
);
}
public
void
actionPerformed(ActionEvent evt)
{
if
(evt.getSource() == b1)
{
JOptionPane.showMessageDialog(
this
,
"Enter a valid Number"
,
"ERROR"
, JOptionPane.ERROR_MESSAGE);
}
}
}
class
MessageDialogs1 {
public
static
void
main(String args[])
{
Demo f =
new
Demo();
f.setBounds(
200
,
200
,
400
,
300
);
f.setResizable(
false
);
f.setVisible(
true
);
}
}
Output :
Java
import
java.awt.*;
import
java.awt.event.*;
import
javax.swing.*;
class
Demo
extends
JFrame
implements
ActionListener
{
JButton b1;
Demo()
{
this
.setLayout(
null
);
b1 =
new
JButton(
"Button 2"
);
b1.setBounds(
130
,
05
,
100
,
50
);
this
.add(b1);
b1.addActionListener(
this
);
}
public
void
actionPerformed(ActionEvent evt)
{
if
(evt.getSource() == b1) {
JOptionPane.showMessageDialog(
this
,
"Enter a valid String"
,
"WARNING"
, JOptionPane.WARNING_MESSAGE);
}
}
}
class
MessageDialogs2 {
public
static
void
main(String args[])
{
Demo f =
new
Demo();
f.setBounds(
200
,
200
,
400
,
300
);
f.setResizable(
false
);
f.setVisible(
true
);
}
}
Output :
Java
import
java.awt.*;
import
java.awt.event.*;
import
javax.swing.*;
class
Demo
extends
JFrame
implements
ActionListener
{
JButton b1;
Demo()
{
this
.setLayout(
null
);
b1 =
new
JButton(
"Button 3"
);
b1.setBounds(
130
,
05
,
100
,
50
);
this
.add(b1);
b1.addActionListener(
this
);
}
public
void
actionPerformed(ActionEvent evt)
{
if
(evt.getSource() == b1)
{
JOptionPane.showMessageDialog(
this
,
"Do you want to quit"
,
"Question"
, JOptionPane.QUESTION_MESSAGE);
}
}
}
class
MessageDialogs3 {
public
static
void
main(String args[])
{
Demo f =
new
Demo();
f.setBounds(
200
,
200
,
400
,
300
);
f.setResizable(
false
);
f.setVisible(
true
);
}
}
Output :
Java
import
java.awt.*;
import
java.awt.event.*;
import
javax.swing.*;
class
Demo
extends
JFrame
implements
ActionListener
{
JButton b1;
Demo()
{
this
.setLayout(
null
);
b1 =
new
JButton(
"Button 4"
);
b1.setBounds(
130
,
05
,
100
,
50
);
this
.add(b1);
b1.addActionListener(
this
);
}
public
void
actionPerformed(ActionEvent evt)
{
if
(evt.getSource() == b1)
{
JOptionPane.showMessageDialog(
this
,
"You Pressed Button FOUR"
,
"INFORMATION"
,
JOptionPane.INFORMATION_MESSAGE);
}
}
}
class
MessageDialogs4 {
public
static
void
main(String args[])
{
Demo f =
new
Demo();
f.setBounds(
200
,
200
,
400
,
300
);
f.setResizable(
false
);
f.setVisible(
true
);
}
}
Output :
My Personal Notes
arrow_drop_up