简体   繁体   中英

JPanel doesn't show in BorderLayout

I have a border layout, and a JPanel on the right. The JPanel has absolute layout, and it doesn't contain anything because I'm using it to draw graphics (overriding paintComponent(Graphics g))

The problem is that the panel doesn't show (it's there but like 1 pixel wide). I tried to setSize on the panel.. but that doesn't work..

How do I do it?

frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
JPanel panelCenter = new JPanel();
panelRight = new ActorDrawer();
frame.getContentPane().add(panelRight, BorderLayout.EAST);
panelRight.setSize(200, 400);
panelRight.setLayout(null);
frame.getContentPane().add(panelCenter, BorderLayout.CENTER);
frame.getContentPane().add(panelBottom, BorderLayout.SOUTH);
table = new JTable(new MyTableModel());
JScrollPane scrollPane = new JScrollPane(table);
table.setFillsViewportHeight(true);
panelCenter.add(scrollPane);

Look into the JPanel.setPreferredSize() method. Also, I think the JFrame.setBounds() call is not what you are looking for. Here's an example:

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Dimension;
import java.awt.BorderLayout;

public class Sandbox extends JFrame
{
    class MyPanel extends JPanel
    {
        public void paintComponent(Graphics g)
        {
            g.setColor(Color.RED);
            g.fillRect(0, 0, this.getWidth(), this.getHeight());
        }
    }

    public static void main(String[] args)
    {
        Sandbox s = new Sandbox();
    }

    public Sandbox()
    {
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(new BorderLayout());

        MyPanel panelRight = new MyPanel();
        panelRight.setPreferredSize(new Dimension(150, 300));
        this.getContentPane().add(panelRight, BorderLayout.EAST);

        JPanel panelCenter = new JPanel();
        this.getContentPane().add(panelCenter, BorderLayout.CENTER);

        JScrollPane scrollPane = new JScrollPane(new JTable());
        panelCenter.add(scrollPane);

        JPanel panelBottom = new JPanel();
        this.getContentPane().add(panelBottom, BorderLayout.SOUTH);

        this.setSize(400, 400);
        this.setVisible(true);
    }
}

BorderLayout honors the preferred size of the east component, and stretches its height if necessary. Have you tried setting the preferred size of the panel (or overriding the getPreferredSize method to return the size you prefer)?

here comes the solution!!! I was doing the same stuff.

JPanel panel = new JPanel();          
JLabel strech_label = new JLabel("As long as you need ,  Dummy Text");
strech_label.setPreferredSize( new Dimension( 111, 0 ) );

panel.setLayout(new BorderLayout());

panel.add(new MyDraw(), BorderLayout.CENTER);
panel.add(strech_label, BorderLayout.SOUTH);

    frame1.setLayout(new BorderLayout());
    frame1.add(panel,BorderLayout.WEST);

So basically the JLabel is either located in the South or North and it is going to strech the MyDraw forcefully.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM