Domanda

i have 3 panels, panel1 has 3 components(ADD, Delete, Edit) JButtons, panel2(scrollPane) has jtable and panel3 again as (5 JTextFields, 5JLabels). when I add all panels into frame panel3 components shows paritialy only means (4JTextFields) only visible within frame. for panels I used BorderLayout for frame.add(panel2,BorderLayout.SOUTH) , frame.add(panel3,BorderLayout.CENTER); panel1, panel2 shows all of components, but panel3 shows partially only. could you please suggest me precisely what layout should I use, for panels, and how to setsize for my frame to accomodate all panels(components)? clearly?

`

          frame.add(topPanel, BorderLayout.NORTH);
        frame.add(updatePanel, BorderLayout.SOUTH);
        frame.add(middlePanel, BorderLayout.CENTER);

        frame.setVisible(true); 
        frame.setSize(850, 500);

`

È stato utile?

Soluzione

Try using Window#pack

Causes this Window to be sized to fit the preferred size and layouts of its subcomponents. The resulting width and height of the window are automatically enlarged if either of dimensions is less than the minimum size as specified by the previous call to the setMinimumSize method.

If the window and/or its owner are not displayable yet, both of them are made displayable before calculating the preferred size. The Window is validated after its size is being calculated.

Updated with example

Without a SSCCE (working example), it's impossible to fully diagnose your problem. Let me demonstrate...

So, based on your description, I can create this without any issues...

enter image description here

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;

public class TestPack {

    public static void main(String[] args) {
        new TestPack();
    }

    public TestPack() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TopPanel(), BorderLayout.NORTH);
                frame.add(new MiddlePanel(), BorderLayout.CENTER);
                frame.add(new UpdatePanel(), BorderLayout.SOUTH);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TopPanel extends JPanel {

        public TopPanel() {
            setBackground(Color.red);
            add(new JLabel("I'm on top"));
        }

    }

    public class MiddlePanel extends JPanel {

        public MiddlePanel() {
            setLayout(new BorderLayout());
            add(new JScrollPane(new JTable(new DefaultTableModel(new Object[]{"A", "B", "C"}, 5))));
        }

    }

    public class UpdatePanel extends JPanel {

        public UpdatePanel() {
            for (int index = 0; index < 5; index++) {
                add(new JLabel(Integer.toString(index)));
                add(new JTextField(5));
            }
            add(new JButton("Button"));
        }            
    }        
}

But obviously, you're doing something different. Take the time to update your question with a working example, otherwise there's nothing more we can possible do

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top