Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么';我的Jpanel borderlayout工作是否如预期?_Java_Swing_Jpanel_Layout Manager_Border Layout - Fatal编程技术网

Java 为什么';我的Jpanel borderlayout工作是否如预期?

Java 为什么';我的Jpanel borderlayout工作是否如预期?,java,swing,jpanel,layout-manager,border-layout,Java,Swing,Jpanel,Layout Manager,Border Layout,我向一个JPanel、一个JLabel和一个JButton添加了两个元素。我希望它们彼此重叠,所以我使用BorderLayout.NORTH和SOUTH添加了它们 我遇到的问题是,JLabel JButton和JLabel JButton在水平方向上并排放置,而不是像预期的那样彼此重叠。这个问题可能与以下事实有关:我还使用borderlayout将其他面板添加到框架中,如下所示 这里有一个SSCE来演示我的问题。您会注意到,如果展开窗口,您将看到JLabel左侧的JButton import j

我向一个JPanel、一个JLabel和一个JButton添加了两个元素。我希望它们彼此重叠,所以我使用BorderLayout.NORTH和SOUTH添加了它们

我遇到的问题是,JLabel JButton和JLabel JButton在水平方向上并排放置,而不是像预期的那样彼此重叠。这个问题可能与以下事实有关:我还使用borderlayout将其他面板添加到框架中,如下所示

这里有一个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.awt.BorderLayout;
导入java.awt.Color;
导入java.awt.Dimension;
导入javax.swing.JButton;
导入javax.swing.JComboBox;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
导入javax.swing.JTextField;
公开课问题{
/**
*@param args
*/
公共静态void main(字符串[]args){
gui();
}
公共静态void gui(){
最终JFrame theFrame=新JFrame();
框架设置尺寸(550290);
frame.setLocationRelativeTo(空);
//JComboBox的选项
字符串[]选项={(“1”),(“2”),(“3”)};
final JPanel thePanel=新JPanel();
JLabel aMessage=新JLabel(
“Lorem ipsum door sit amet,concertetur adipising elite.Phasellus tempus
前庭间隙。Donec venenatis rhoncus just to sit amet
孕妇。Pellentesque居住者morbi tristique sentectus et netus et malesuada
est ac mattis。Donec lobortis rhoncus quam.In In In In vulputte ipsum
修复。Phasellus tempus eu ante罗非鱼托叶
弓形弓状体,非枕叶purus leo ege.


”; aMessage.setVisible(true); JButton theButton=新JButton(); 按钮 .setText(“单击此处” +“如果您想查看某些文件。”); 按钮。已绘制(假); 按钮。设置不透明(假); 按钮。背景(颜色。白色); 按钮。设置可见(真); 面板添加(A消息,BorderLayout.NORTH); 面板添加(按钮,BorderLayout.SOUTH); //创建第二个面板 最终JPanel secondPanel=新JPanel(); JLabel bMessage=newjlabel(“这里是另一个JLabel”); 最终JCOMBOX cBox=新JCOMBOX(选项); //最好让bmessage+cBox左对齐 添加(b消息); 第二个面板。添加(cBox); //创建第三个面板 最终JPanel thirdPanel=新JPanel(); JLabel cMessage=newjlabel(“这是第三条消息”); 字符串newString=(“Hello”); JTextField stringInput=新的JTextField(newString); stringInput.setPreferredSize(新维度(250,20)); 第三面板添加(cMessage); thirdPanel.add(stringInput); JButton lastButton=新JButton(“按钮”); 第三面板添加(cMessage); thirdPanel.add(stringInput); 第三面板添加(lastButton); 添加(面板,BorderLayout.NORTH); 添加(第三面板,边界布局。南); frame.add(第二个面板,BorderLayout.CENTER); frame.setVisible(true); } }
您在哪里设置
面板的布局?答:您不需要,因此默认情况下它使用FlowLayout。解决方案:通过
setLayout(…)

e、 g

或者<代码>新建JPanel(新建BorderLayout()):)
thePanel.setLayout(new BorderLayout());