[英]How do I add the java icon and lineup text with input?
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. 我试图使底部2看起来像上部2。
So the first problem is that I don't get the java icon in the title. 因此,第一个问题是标题中没有显示Java图标。
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
. 我们可以将Swing Component添加到
JOptionPane
。 So why not creating a custom panel containing a JLabel
and JTextFeild
with layout ie, FlowLayout
and add that panel to JOptionPane using 那么,为什么不创建一个包含
JLabel
和JTextFeild
及其布局的自定义面板,即FlowLayout
然后使用以下方式将该面板添加到JOptionPane中:
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 教程资源: 如何进行对话
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.