我正在寻找一种布局管理器(或一组布局管理器),可以帮助我垂直排列我的组件。所有组件(按钮)都应具有相同的宽度,由底层对话框客户端区域的宽度边界限定。每个按钮都能根据连接的JLabel(标签将在用户单击相关按钮后显示)扩展其高度 - 简而言之:所有组件应该被拉伸到相同的固定宽度,但高度因其内容而异。整个对话框内容应放置在一个JScrollPane中,以支持需要时的垂直滚动条。
编辑3:
我认为我的主要问题是JScrollPane与包含JLabel的html组合,我需要类似于setMaximumSize(...)的东西,但它并没有按照预期工作: http://www.pic-upload.de/view-16988005/example-image3.png.html 我需要一个固定的宽度,但我找不到设置它的方法。
setPreferredSize(...)可以工作,但我不知道JLabel的高度,因为它包含HTML文本。
编辑4:
我的按钮:
我已经尝试了很多不同的布局管理器(大部分是BorderLayout用于水平拉伸和BoxLayout用于垂直排列),以实现我想要的行为。但代码既不令人满意,也不能完全按照我的意愿工作。
我花了很多时间在互联网上搜索这个问题,并发现在Java中,垂直布局是一个常见的问题。很多人回复说GridBagLayout是最好的布局来完成这项工作,但我没有让它按照我想要的方式工作。
希望这些信息足够为我解决这个问题提供一些帮助。
最好的问候 Michael
编辑:-不再需要-
编辑2:
这是我当前尝试的图片: http://www.pic-upload.de/view-16978954/example-image.png.html 这几乎是我想要的结果,但在调整大小时有奇怪的行为。编辑3:
我认为我的主要问题是JScrollPane与包含JLabel的html组合,我需要类似于setMaximumSize(...)的东西,但它并没有按照预期工作: http://www.pic-upload.de/view-16988005/example-image3.png.html 我需要一个固定的宽度,但我找不到设置它的方法。
setPreferredSize(...)可以工作,但我不知道JLabel的高度,因为它包含HTML文本。
编辑4:
我的按钮:
public class Expander extends JPanel implements ActionListener {
private static class HtmlViewer extends JLabel {
private static final long serialVersionUID = 8787130155299083869L;
public HtmlViewer(String doc) {
super(doc);
this.setBackground(Color.WHITE);
this.setOpaque(true);
}
}
private static final long serialVersionUID = 4480221797736558685L;
JToggleButton button;
JComponent component;
public Expander(String text, String doc) {
this(text, new HtmlViewer(doc));
}
public Expander(String text, JComponent expandableComponent) {
this.setLayout(new BorderLayout());
button = new JToggleButton(text) {
private static final long serialVersionUID = -3330376265192275758L;
@Override
public void paint(Graphics g) {
super.paint(g);
if (this.isSelected()) {
g.drawString("▼", this.getWidth() - 20, 15);
} else {
g.drawString("■", this.getWidth() - 20, 15);
}
}
};
button.setFocusPainted(false);
button.addActionListener(this);
component = expandableComponent;
component.setBorder(new EmptyBorder(5, 5, 5, 5));
this.add(button, BorderLayout.PAGE_START);
}
@Override
public void actionPerformed(ActionEvent e) {
if (button.isSelected()) {
this.add(component);
} else {
this.remove(component);
}
this.getTopLevelAncestor().validate();
this.getTopLevelAncestor().repaint();
this.setMaximumSize(this.getPreferredSize());
}
}
框架:
public class grid2 {
public static void main(String[] args) {
final JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p = new JPanel();
JScrollPane scroll = new JScrollPane(p, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
addStuff(p);
frame.add(scroll);
frame.pack();
frame.setVisible(true);
}
public static void addStuff(Container container) {
container.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 1;
c.weighty = 0;
Expander e = new Expander("test", "<html>fgb fdh fgfhg ifghiufdshfidsghfiufdsghiudsfhdsfiu dshiufhds if dhf idsufhdsiufhiufhiuds hfdshfiudshfuidsifudshfiudshf ufdhfiushdfiudshiufhdsiufhdsiuf udshfiudshfiudshfudshf iuhfiudshfiudshfiudshf</html>"); // long html text example
e.setMaximumSize(new Dimension(500, 10000));
c.gridx = 0;
c.gridy = 0;
c.gridwidth = GridBagConstraints.REMAINDER;
container.add(e, c);
JButton button2 = new JButton("Button 2");
c.gridy = 1;
c.gridwidth = GridBagConstraints.REMAINDER;
container.add(button2, c);
c.gridy = 2;
c.weighty = 1;
c.gridwidth = GridBagConstraints.REMAINDER;
container.add(new JLabel(), c);
}
}
VerticalLayout
。 - undefined<code>
之类的标签,而是选择您的代码并在编辑器工具栏中使用按钮{}
。 - undefined