JOptionPane.showInternalInputDialog无法编辑

时间:2013-04-05 03:54:45

标签: java swing joptionpane

我正在撰写一个JDesktopPane应用程序,并且在JInternalFrame中,我有一个JEditorPane打开了一个网页(是的,我知道它的糟糕能力JEditorPane有网,不要责骂)。

我可以让用户输入他们想要访问的页面,但是当我调用JOptionPane.showInternalInputDialog(this, "What page would you like to visit?")时,文本字段不可编辑。在Java 6和Java 7中都出现了这个问题。

编辑: 这是我班级的构造函数

public Internet() {
    super("Internet", true, true, true, true);
    setSize(500, 400);
    try {
        pane = new JEditorPane(new URL("http://www.vetrustech.tk"));
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    setContentPane(pane);
    bar = new JMenuBar();
    page = new JMenu("Page");
    enterPage = new JMenuItem("Enter a page");
    bar.add(page);
    page.add(enterPage);

    setJMenuBar(bar);

    enterPage.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            loadPage();
        }
    });

这是加载页面的方法

private void loadPage() {
    String s = JOptionPane.showInternalInputDialog(this,
            "What page are you visiting?");
    if (s == null) {
        return;
    }
    if (s.equals("")) {
        return;
    }
    try {
        URL u = new URL(s);
        pane.setPage(u);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

1 个答案:

答案 0 :(得分:1)

以下是合适的SSCCE如此重要的原因......

这有效....

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Internet {

    public static void main(String[] args) {
        new Internet();
    }

    public Internet() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JDesktopPane dp = new JDesktopPane();
                final JInternalFrame inf = new JInternalFrame("Help", true, true, true, true);
                inf.setSize(200, 200);
                inf.setVisible(true);
                dp.add(inf);

                JButton btn = new JButton("Click");
                btn.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        JOptionPane.showInternalInputDialog(inf, "Hit me");
                    }
                });
                inf.add(btn);

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(dp);
                frame.setSize(400, 400);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

}

这表明您在代码中正在做一些我们没有看到的其他内容。