简体   繁体   English

为什么我的Jpanel borderlayout不按预期工作?

[英]Why doesn't my Jpanel borderlayout work as expected?

I am adding 2 elements to a JPanel, a JLabel and a JButton. 我在JPanel,JLabel和JButton中添加了2个元素。 I would like them to appear one on top of each other, so I added them using BorderLayout.NORTH and SOUTH. 我希望它们彼此叠加,所以我使用BorderLayout.NORTH和SOUTH添加它们。

The problem I have is that the JLabel JButton and are sitting next to each other horizontally side to side, not on top of each other as expected. 我遇到的问题是JLabel JButton并排坐在彼此旁边,而不是像预期的那样彼此叠加。 The issue may be related to the fact that I am also adding to other panels to the frame using borderlayout as shown below. 这个问题可能与我使用borderlayout将其他面板添加到框架的事实有关,如下所示。

Here is an SSCE to demonstrate my problem. 这是一个SSCE来演示我的问题。 You will notice that if you expand the window you will see the JButton to the left of the JLabel. 您会注意到,如果展开窗口,您将看到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);
    }

}

Where do you set the layout of the thePanel JPanel? 你在哪里设置thePanel JPanel的布局? Answer: you don't and so it uses FlowLayout by default. 答:你没有,所以它默认使用FlowLayout。 Solution: set the layout to the one you want via setLayout(...) 解决方案:通过setLayout(...)将布局设置为您想要的布局

eg, 例如,

thePanel.setLayout(new BorderLayout());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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