3

I am adding a java swing border into my existing code. I have referred online and put in my code which I got from a site, but on running it I get an error, and when highlighting in my intellij this pops up : cannot resolve symbol 'p'. Where am I going wrong ?

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();
    }
}
John
  • 33
  • 4

1 Answers1

1

In your code you didn't have any value assigned to variable p. Use code

JPanel p = new JPanel(); /* instead of p = new JPanel() alone */

as it declares and assigns the variable p of type JPanel with a value of object new JPanel().

  • it has no errors now, but I am not able to see the border. The main thing I want is the words MatteBorder to appear as I intend to use it as a method of grouping various settings in the GUI. – John Sep 01 '15 at 06:07
  • I suppose adding the panel is most important, but I have never worked with them before, how would I create a new one ? – John Sep 01 '15 at 06:07
  • Can you add and see any other border? Do _p.setBorder([BorderFactory](https://docs.oracle.com/javase/tutorial/uiswing/components/border.html).createLineBorder(Color.black));_, does it produce a black border around your panel? – Developer Marius Žilėnas Sep 01 '15 at 06:12
  • yes sorry , the border is there, but i need a new panel, just to illustrate some words, like the word Testing for example, and then an empty panel, but just the word is visible – John Sep 01 '15 at 06:17
  • and there is no new panel created, upon running the above code – John Sep 01 '15 at 06:18
  • In case you want multiple panels on the frame you could create multiple panels (see http://stackoverflow.com/a/15694375/1737819) and put them to the mainPanel (so called _container panel_) then do `mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.X_AXIS)`. – Developer Marius Žilėnas Sep 01 '15 at 06:37
  • Managed to get it using this code : formBuilder.add(new DJLabel(LOGIN, login, KeyEvent.VK_N), cc.xy(1, 6)); – John Sep 01 '15 at 06:42
  • One more question though, how do I underline the word LOGIN, I m getting stuck with it – John Sep 01 '15 at 06:45
  • If this DJLabel is a JLabel then see http://stackoverflow.com/a/15892859/1737819 where it says that you can put HTML code in your `JLabel label = new JLabel("this text will be underlinedthis text is bolded and this text appears as it is");` – Developer Marius Žilėnas Sep 01 '15 at 06:53