首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >从JDialog中的JOptionPane获取输入

从JDialog中的JOptionPane获取输入
EN

Stack Overflow用户
提问于 2018-06-18 07:20:39
回答 1查看 143关注 0票数 -1

出于涉及外观花哨的组件的目的,我编写了一个在JDialog中创建和显示JOptionPane的函数。现在,我需要从那个JDialog获取输入,但我不能。有没有一种方法可以让我在不扩展JDialog或JOptionPane的情况下从该JDialog获取输入?(我也不能使用UIManager来改变JDialog的外观,这就是我首先遇到这个问题的原因)

代码语言:javascript
复制
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);

private static void recursivePaint(Container ct) {
    for (Component c : ct.getComponents()) {
        if (c instanceof Container) {
            c.setBackground(DARK_BLUE);
            c.setForeground(WHITE);
            recursivePaint((Container) c);
        }
    }
}

public static int showInputDialog(final Container parent) {
    int portNumber = 0;

    final JLabel label = new JLabel("Enter an Open Port: ", SwingConstants.LEFT);
    label.setOpaque(true);
    label.setBackground(DARK_BLUE);
    label.setForeground(WHITE);

    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);
        }
    };
    button.addActionListener(e -> {
        //GET THE INPUT OF JOPTIONPANE TEXTFIELD
        //portNumber = getTextOfJOptionPane();
        SwingUtilities.getWindowAncestor((Component) e.getSource()).setVisible(false);
        SwingUtilities.getWindowAncestor((Component) e.getSource()).dispose();
    });

    final JOptionPane optionPane = new JOptionPane(label, JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_OPTION, null, new JButton[] {button}, button);
    optionPane.setWantsInput(true);
    optionPane.setOpaque(true);
    optionPane.setBackground(DARK_BLUE);
    optionPane.getInputValue();
    recursivePaint(optionPane);
    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);
    return portNumber;
}

提前谢谢。

JOptionPane on Windows

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-18 08:17:08

由于JOptionPane不会公开复制它所需的所有功能,因此您必须采取更多控制措施。

“合适”的方法是提供您自己的自定义外观和感觉委托,但这似乎有很多额外的工作,以便您可以控制JTextField

相反,您可以通过message参数将JLabelJTextField都传递给OptionPane,该参数包含在单个JPanel中,例如...

代码语言:javascript
复制
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_OPTION并命名为setInputValue。调用setValue很重要,否则inputValue将不会被应用,因为它会认为您已经“取消”了对话框。

我不知道你的对话框在你的系统上是什么样子,但这就是它在我的系统上的样子……

这就是为什么覆盖像getHeightgetWidth这样的东西是不明智的

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50901214

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档