So I'm trying to get the same result, according to this picture:
I'm trying to make the bottom 2 look like the upper 2.
So the first problem is that I don't get the java icon in the title.
The second problem is that "Some text:" isn't lined with the input box.
Here is my code:
public static void main(String[] args) {
String input = JOptionPane.showInputDialog(null, "Some Text:", "Dialog",
JOptionPane.PLAIN_MESSAGE);
if(input != null)
JOptionPane.showMessageDialog(null, "Value entered: " + input, "Message box", JOptionPane.INFORMATION_MESSAGE);
else
System.exit(0);
}
we can add Swing Component to JOptionPane
. So why not creating a custom panel containing a JLabel
and JTextFeild
with layout ie, FlowLayout
and add that panel to JOptionPane using
JOptionPane.showConfirmDialog
(
frame, // main window frame
customPanel, // custom panel containing the label and textFeild
"My Panel with Text Feild", // Title
JOptionPane.OK_CANCEL_OPTION, // with OK and CANCEL button
JOptionPane.PLAIN_MESSAGE
);
A minimal working example:
import java.awt.event.*;
import javax.swing.*;
class CustomPanel extends JPanel
{
JLabel lab;
JTextField txtField;
public CustomPanel() {
lab = new JLabel("Some Text: ");
txtField = new JTextField(20);
add(lab);
add(txtField);
}
public String getText()
{
return txtField.getText();
}
}
public class JOptionPaneDemo {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
CustomPanel inputPane = new CustomPanel();
int value = JOptionPane.showConfirmDialog(null, inputPane, "Demo" ,JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
if(value == JOptionPane.OK_OPTION)
{
JOptionPane.showMessageDialog(null, "Value Entered: "+inputPane.getText(), "Demo", JOptionPane.INFORMATION_MESSAGE);
}
}
});
}
}
Tutorial resource: How to make Dialogue
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.