teelyf28
teelyf28

Reputation: 21

Add method using panels as subcontainers

running the follow code it opens only an empty window without any buttons and sub panels . Could anyone help me ?

public class TestPanels extends JFrame{
    public TestPanels(){
        //Create first panel
        JPanel panel1= new JPanel();
        panel1.setLayout(new GridLayout(4, 3));

    for(int i=1; i<=9; i++){
            panel1.add(new JButton( ""+i));
            }

        // Create Second panel 
        JPanel panel2=new JPanel(new BorderLayout());       
        panel2.add(new JTextField("Time to Display Here"), BorderLayout.NORTH);
        panel2.add(panel1, BorderLayout.CENTER);

    }

    public static void main(String[] args){
        TestPanels test= new TestPanels();

        test.setTitle("Test Panel");

    }
}

Upvotes: 1

Views: 69

Answers (1)

GhostCat
GhostCat

Reputation: 140457

The real issue here is: learning about Swing is "different" than your first steps with Java.

What I mean is: in order to get a JFrame on the screen, certain methods need to be called. And then, in order to get "something" into that JFrame, more things need to happen.

Meaning: learning Swing UIs by "trial and error" won't work. In contrast to things you might have studied earlier, you better start with tutorials, like the one from Oracle and follow that. And only then, when things are really working - then you look into enhancing them with your own code!

So my answer does not contain the multipe lines of code that you need to get to your result, but the recommendation to look out for resources that explain these things in more detail! Because whatever you want to do afterwards, might again only work when you study the required context carefully before getting into "action mode".

Upvotes: 1

Related Questions