gpt4 book ai didi

java - 为什么我的 Jpanel borderlayout 没有按预期工作?

转载 作者:搜寻专家 更新时间:2023-11-01 01:51:17 25 4
gpt4 key购买 nike

我正在向 JPanel、JLabel 和 JButton 添加 2 个元素。我希望它们彼此重叠,所以我使用 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);
}

}

最佳答案

在哪里设置thePanel JPanel 的布局?答:你不需要,所以它默认使用 FlowLayout。解决方案:通过 setLayout(...)

将布局设​​置为您想要的布局

例如,

thePanel.setLayout(new BorderLayout());

关于java - 为什么我的 Jpanel borderlayout 没有按预期工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30423001/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com