【问题标题】:Why doesn't my Jpanel borderlayout work as expected?为什么我的 Jpanel 边框布局没有按预期工作?
【发布时间】:2015-05-24 11:28:50
【问题描述】:

我正在向 JPanel、JLabel 和 JButton 添加 2 个元素。我希望它们彼此重叠出现,所以我使用 BorderLayout.NORTH 和 SOUTH 添加它们。

我遇到的问题是 JLabel JButton 和 JLabel JButton 水平并排坐在彼此旁边,而不是像预期的那样彼此重叠。该问题可能与我还使用边框布局将其他面板添加到框架中的事实有关,如下所示。

这是一个 SSCE 来演示我的问题。您会注意到,如果您展开窗口,您会看到 JLabel 左侧的 JButton。

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

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Question {
    /**
     * @param args
     */
    public static void main(String[] args) {
        gui();
    }

    public static void gui() {

        final JFrame theFrame = new JFrame();
        theFrame.setSize(550, 290);

        theFrame.setLocationRelativeTo(null);

        // options for the JComboBox
        String[] options = { ("1"), ("2"), ("3") };

        final JPanel thePanel = new JPanel();
        JLabel aMessage = new JLabel(
                "<html>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus tempus<br> lacus eu ante vestibulum tincidunt. Donec venenatis rhoncus justo sit amet <br>gravida. Pellentesque habitant morbi tristique senectus et netus et malesuada<br>est ac mattis. Donec lobortis rhoncus quam. In at vulputate ipsum<br>to fix. Phasellus tempus lacus eu ante vestibulum tincidunt. Donec venenatis rhoncus<br>turpis quam sagittis arcu, non pulvinar purus leo ege.<br><br><br></html>");
        aMessage.setVisible(true);

        JButton theButton = new JButton();
        theButton
                .setText("<HTML><FONT color=\"#000099\"><U>Click here</U></FONT>"
                        + " if you would like to view some file.</HTML>");

        theButton.setBorderPainted(false);
        theButton.setOpaque(false);
        theButton.setBackground(Color.WHITE);
        theButton.setVisible(true);

        thePanel.add(aMessage, BorderLayout.NORTH);
        thePanel.add(theButton, BorderLayout.SOUTH);

        // create second panel
        final JPanel secondPanel = new JPanel();
        JLabel bMessage = new JLabel("Here's another Jlabel");
        final JComboBox cBox = new JComboBox(options);
        // would be nice to get bmessage + cBox left aligned
        secondPanel.add(bMessage);
        secondPanel.add(cBox);

        // create third panel
        final JPanel thirdPanel = new JPanel();
        JLabel cMessage = new JLabel("Here's a third message");
        String newString = ("Hello");
        JTextField stringInput = new JTextField(newString);
        stringInput.setPreferredSize(new Dimension(250, 20));
        thirdPanel.add(cMessage);
        thirdPanel.add(stringInput);


        JButton lastButton = new JButton("A Button");

        thirdPanel.add(cMessage);
        thirdPanel.add(stringInput);
        thirdPanel.add(lastButton);

        theFrame.add(thePanel, BorderLayout.NORTH);
        theFrame.add(thirdPanel, BorderLayout.SOUTH);
        theFrame.add(secondPanel, BorderLayout.CENTER);

        theFrame.setVisible(true);
    }

}

【问题讨论】:

    标签: java swing jpanel layout-manager border-layout


    【解决方案1】:

    thePanelJPanel 的布局在哪里设置?答:你没有,所以它默认使用 FlowLayout。解决方案:通过setLayout(...)将布局设置为您想要的布局

    例如,

    thePanel.setLayout(new BorderLayout());
    

    【讨论】:

    • 或...new JPanel(new BorderLayout()); :)
    猜你喜欢
    • 1970-01-01
    • 2016-08-17
    • 1970-01-01
    • 2020-02-29
    • 2019-08-28
    • 2014-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多