Java Swing边框错误

3

我正在将Java Swing边框添加到现有代码中。我参考了网上的资料,并将得到的代码放入我的代码中,但在运行时出现错误,并且在我的Intellij中高亮显示时出现:无法解析符号“p”。我错在哪里了?

private class AnotherBorderTest extends JFrame {
    public AnotherBorderTest() {
        setTitle("Border Test");
        setSize(450, 450);

        JPanel content = (JPanel) getContentPane();
        content.setLayout(new GridLayout(6, 2, 3, 3));


        p = new JPanel();
        p.setBorder(new MatteBorder(new ImageIcon("BALL.GIF")));
        p.add(new JLabel("MatteBorder"));
        content.add(p);

        setVisible(true);
    }

    public void main(String args[]) {
        new AnotherBorderTest();
    }
}
1个回答

1
在你的代码中,变量p没有被赋任何值。使用以下代码:
JPanel p = new JPanel(); /* instead of p = new JPanel() alone */

它声明并分配了类型为JPanel,值为new JPanel()的对象变量p。


现在没有错误,但我无法看到边框。我想要的主要是让MatteBorder这些字出现,因为我打算将其用作在GUI中分组各种设置的方法。 - John
我想添加面板是最重要的,但我以前从未使用过它们,我该如何创建一个新的面板? - John
是的,抱歉,边框已经在那里了,但我需要一个新的面板,只是为了说明一些词语,比如“测试”这个词,然后是一个空面板,但只有这个词是可见的。 - John
还有一个问题,如何给单词“LOGIN”加下划线,我卡在这里了。 - John
如果这个DJLabel是一个JLabel,那么请参考https://dev59.com/HGUo5IYBdhLWcg3woAqt#15892859,其中提到您可以在您的`JLabel label = new JLabel("<html><u>this text will be underlined</u><b>this text is bolded</b> and this text appears as it is</html>");`中放置HTML代码。 - Developer Marius Žilėnas
显示剩余4条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接