JPanel 不显示在 BorderLayout 中

发布于 2024-12-09 10:15:46 字数 716 浏览 0 评论 0原文

我有一个边框布局,右侧有一个 JPanel。 JPanel 具有绝对布局,并且它不包含任何内容,因为我使用它来绘制图形(覆盖 PaintComponent(Graphics g))。

问题是面板不显示(它在那里,但宽度约为 1 个像素)。我尝试在面板上设置大小..但这不起作用..

我该怎么做?

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);

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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

长发绾君心 2024-12-16 10:15:46

查看 JPanel.setPreferredSize() 方法。另外,我认为 JFrame.setBounds() 调用不是您正在寻找的。这是一个例子:

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);
    }
}

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);
    }
}
南渊 2024-12-16 10:15:46

BorderLayout 遵循东部组件的首选大小,并在必要时拉伸其高度。您是否尝试过设置面板的首选大小(或重写 getPreferredSize 方法以返回您喜欢的大小)?

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)?

痴骨ら 2024-12-16 10:15:46

解决方案来了!!!我也在做同样的事情。

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);

所以基本上 JLabel 要么位于南方,要么位于北方,它将有力地拉伸 MyDraw。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文