Java 从JDialog中的JOptionPane获取输入
为了使用外观奇特的组件,我编写了一个函数,在JDialog中创建并显示JOptionPane。现在,我需要从JDialog获取输入,但我不能。有没有一种方法可以让我在不扩展JDialog或JOptionPane的情况下从JDialog获取输入?(我也不能使用UIManager来改变JDialog的外观,这就是我首先遇到问题的原因) 提前谢谢Java 从JDialog中的JOptionPane获取输入,java,swing,user-interface,Java,Swing,User Interface,为了使用外观奇特的组件,我编写了一个函数,在JDialog中创建并显示JOptionPane。现在,我需要从JDialog获取输入,但我不能。有没有一种方法可以让我在不扩展JDialog或JOptionPane的情况下从JDialog获取输入?(我也不能使用UIManager来改变JDialog的外观,这就是我首先遇到问题的原因) 提前谢谢 由于JOptionPane没有公开复制此功能所需的所有功能,因此您将不得不采取更多控制措施 “合适的”路径是提供您自己的定制外观委托,但这似乎需要做很多额外
由于
JOptionPane
没有公开复制此功能所需的所有功能,因此您将不得不采取更多控制措施
“合适的”路径是提供您自己的定制外观委托,但这似乎需要做很多额外的工作,以便您可以控制JTextField
相反,您可以通过包含在单个JPanel
中的message
参数将JLabel
和JTextField
传递到OptionPane
,例如
public static final Color WHITE = Color.WHITE;
public static final Color RED = Color.RED;
public static final Color LIGHTER_RED = new Color(255, 0, 0, 100);
public static final Color LIGHT_RED = new Color(255, 0, 0, 160);
public static final Color DARK_BLUE = new Color(22, 44, 66);
public static final Font GEORGIA_BOLD_12 = new Font("Georgia", Font.BOLD, 12);
public static final BasicStroke STROKE_0 = new BasicStroke(0);
public static int showInputDialog(final Container parent) {
int portNumber = 0;
final JLabel label = new JLabel("Enter an Open Port: ", SwingConstants.LEFT);
label.setForeground(WHITE);
JPanel panel = new JPanel(new GridLayout(2, 1));
panel.setOpaque(true);
panel.setBackground(DARK_BLUE);
JTextField inputField = new JTextField(10);
panel.add(label);
panel.add(inputField);
final JButton button = new JButton("OK") {
private static final long serialVersionUID = -4808194362293478299L;
@Override
public int getWidth() {
return 51;
}
@Override
public int getHeight() {
return 26;
}
@Override
public void paintComponent(final Graphics g) {
final Graphics2D g2d = (Graphics2D) g;
g2d.clearRect(0, 0, getWidth(), getHeight());
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g2d.setStroke(STROKE_0);
g2d.setColor(LIGHTER_RED);
if (this.getModel().isRollover()) {
g2d.setColor(LIGHT_RED);
}
if (this.getModel().isPressed()) {
g2d.setColor(RED);
}
g2d.fillRect(0, 0, getWidth(), getHeight());
g2d.setColor(RED);
g2d.drawRect(0, 0, getWidth(), getHeight());
g2d.setColor(WHITE);
g2d.setFont(GEORGIA_BOLD_12);
g2d.drawString("CONFIRM", 10, 18);
}
};
final JOptionPane optionPane = new JOptionPane(panel, JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_OPTION, null, new JButton[]{button}, button);
button.addActionListener(e -> {
//GET THE INPUT OF JOPTIONPANE TEXTFIELD
optionPane.setInputValue(inputField.getText());
optionPane.setValue(JOptionPane.OK_OPTION);
});
optionPane.setOpaque(true);
optionPane.setBackground(DARK_BLUE);
final JDialog d = optionPane.createDialog(parent, "Open port required!");
d.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
d.setContentPane(optionPane);
d.pack();
d.setLocationRelativeTo(parent);
d.setVisible(true);
System.out.println(optionPane.getValue());
System.out.println(optionPane.getInputValue());
return portNumber;
}
所以。此外,我还做了一些额外的更改。在ActionListener
中,我将操作的“值”设置为OK\u选项
,并调用了setInputValue
。调用setValue
非常重要,否则不会应用inputValue
,因为它认为您已“取消”对话框
我不知道你的对话框在你的系统上是什么样子的,但这就是我的系统上的样子
仔细看一看,尤其是“直接使用:”示例。基本上,在对话框关闭后,您将像往常一样从
JOptionPane
中获取值,但您需要“手动”而不是“谢谢”!。我刚刚做了并且有了一个想法,但有一行我不明白,“pane.set.Xxxx(…);//Configure”是什么意思?JOptionPane没有这样的方法。您能描述一下如何“配置”JOptionPaneWait吗?这仅仅是指设置方法吗?我很困惑:(是的,这意味着您需要自行配置该对话框。特别是setwantInput
,setSelectionValues
,setInitialSelectionValue
,您可以通过getInputValue
获取输入值。查看作业窗格的源代码。showInputDialog
,它将演示实际上,iLog是由类自己构建的——长话短说,您必须手动设置它,包括在“确认”时提供JTextField
按下按钮,您将需要从JTextField
获取值并调用setInputValue
-为什么,因为JOptionPane
没有公开用于基类中输入的JTextField
,这似乎是由look-and-feel-delegated应用的。该死,它在Windows操作系统上看起来很好。想不到会这样uld在其他操作系统上看起来是扭曲的。关于如何在不覆盖getHeight和getWidth的情况下正确创建自定义外观按钮,有什么建议吗?我现在真的可以使用一些建议。是的,API按照预期或编写自己的API:PI已经在我的答案中添加了一个imgur链接。这是JOptionPane的图像。当使用recursivePaint函数时d(你没有这样做,因为我忘了添加它,但现在它被添加了)它看起来不错。但我相信有更好的方法可以创建自定义按钮。因此,如果你有其他方法,我肯定会听到它们。再次感谢你的帮助。程序终于完成了。:)
public static final Color WHITE = Color.WHITE;
public static final Color RED = Color.RED;
public static final Color LIGHTER_RED = new Color(255, 0, 0, 100);
public static final Color LIGHT_RED = new Color(255, 0, 0, 160);
public static final Color DARK_BLUE = new Color(22, 44, 66);
public static final Font GEORGIA_BOLD_12 = new Font("Georgia", Font.BOLD, 12);
public static final BasicStroke STROKE_0 = new BasicStroke(0);
public static int showInputDialog(final Container parent) {
int portNumber = 0;
final JLabel label = new JLabel("Enter an Open Port: ", SwingConstants.LEFT);
label.setForeground(WHITE);
JPanel panel = new JPanel(new GridLayout(2, 1));
panel.setOpaque(true);
panel.setBackground(DARK_BLUE);
JTextField inputField = new JTextField(10);
panel.add(label);
panel.add(inputField);
final JButton button = new JButton("OK") {
private static final long serialVersionUID = -4808194362293478299L;
@Override
public int getWidth() {
return 51;
}
@Override
public int getHeight() {
return 26;
}
@Override
public void paintComponent(final Graphics g) {
final Graphics2D g2d = (Graphics2D) g;
g2d.clearRect(0, 0, getWidth(), getHeight());
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g2d.setStroke(STROKE_0);
g2d.setColor(LIGHTER_RED);
if (this.getModel().isRollover()) {
g2d.setColor(LIGHT_RED);
}
if (this.getModel().isPressed()) {
g2d.setColor(RED);
}
g2d.fillRect(0, 0, getWidth(), getHeight());
g2d.setColor(RED);
g2d.drawRect(0, 0, getWidth(), getHeight());
g2d.setColor(WHITE);
g2d.setFont(GEORGIA_BOLD_12);
g2d.drawString("CONFIRM", 10, 18);
}
};
final JOptionPane optionPane = new JOptionPane(panel, JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_OPTION, null, new JButton[]{button}, button);
button.addActionListener(e -> {
//GET THE INPUT OF JOPTIONPANE TEXTFIELD
optionPane.setInputValue(inputField.getText());
optionPane.setValue(JOptionPane.OK_OPTION);
});
optionPane.setOpaque(true);
optionPane.setBackground(DARK_BLUE);
final JDialog d = optionPane.createDialog(parent, "Open port required!");
d.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
d.setContentPane(optionPane);
d.pack();
d.setLocationRelativeTo(parent);
d.setVisible(true);
System.out.println(optionPane.getValue());
System.out.println(optionPane.getInputValue());
return portNumber;
}