2

Since, I get used to code in javascript, I want to create objects with properties.

Here is my code:

jPanel2.add( new JPanel(){ this.add(new JButton("Add")); });

Do you have any suggestions ?

Yunus Eren Güzel
  • 3,018
  • 11
  • 36
  • 63

1 Answers1

6

You can always use this syntax:

container.add(new JPanel() {{ this.add(new JButton("Add")); }});

Full example:

public static void main(String[] args) throws Exception {

    JFrame frame = new JFrame("Test");

    frame.add(new JPanel() {{ this.add(new JButton("Add")); }});

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
}
dacwe
  • 43,066
  • 12
  • 116
  • 140