@
@
@atambalasi2019-10-22 18:55:43
Java
@atambalasi, 2019-10-22 18:55:43

How to add two Jpanels to a JFrame?

How to add two JPanels to one frame? I do so

import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;

public class ColumnOne extends JFrame {

    public ColumnOne(){
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setExtendedState(MAXIMIZED_BOTH);
        setTitle("Rails");
        week();
        setVisible(true);

    }

    public void week(){
        Object[] headers = {"1", "Lesson"};
        Object[][] monday = {{"1", "English"}, {"2", "Algebra"}};
        Object[][] two    = {{"1", "Physic"}, {"2", "Kazkah"}};

        JTable jtMonday = new JTable(monday, headers);
        JTable jtTwo = new JTable(two, headers);

        JPanel weekClassOne = new JPanel();
        weekClassOne.setBounds(0,20, 400, 650);
        weekClassOne.setBackground(Color.DARK_GRAY);
        weekClassOne.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "1", TitledBorder.CENTER, TitledBorder.TOP));
        weekClassOne.add(new JScrollPane(jtMonday));
        weekClassOne.add(new JScrollPane(jtTwo));

        getContentPane().add(weekClassOne);

        JTable jtMondayCls2 = new JTable(monday, headers);
        JTable jtTwoCls2 = new JTable(two, headers);

        JPanel weekClassTwo = new JPanel();
        weekClassTwo.setBounds(0,20, 400, 650);
        weekClassTwo.setBackground(Color.PINK);
        weekClassTwo.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "2", TitledBorder.CENTER, TitledBorder.TOP));
        weekClassTwo.add(new JScrollPane(jtMondayCls2));
        weekClassTwo.add(new JScrollPane(jtTwoCls2));

        getContentPane().add(weekClassTwo);



    }
}

But I draw like this weekClassOne inside weekClassTwo. Why is this happening, what is wrong?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question