0

我正在编写一个JDesktopPane应用程序,并且在 a 中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();
    }
}
4

1 回答 1

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);
            }
        });
    }

}

这表明您正在代码中执行我们没有看到的其他操作。

于 2013-04-05T04:10:30.353 回答