gpt4 book ai didi

Java Swing : Vertical Layout with fixed width and variable height

转载 作者:行者123 更新时间:2023-12-02 12:57:15 29 4
gpt4 key购买 nike

我正在寻找一个布局管理器(或一组布局管理器)来帮助我垂直排列我的组件。所有组件(按钮)应具有相同的宽度,以底层对话框客户区宽度为边界。这些按钮中的每一个都能够根据连接的 JLabel 扩展其高度(一旦用户单击相关按钮,标签就会显示) - 简而言之:所有组件都应拉伸(stretch)到相同的固定宽度,但具有变量高度取决于其内容。如果需要,整个对话框内容应放置在 JScrollPane 中以支持垂直滚动条。

我现在堆叠了很多不同的布局管理器(主要是用于水平拉伸(stretch)的 borderlayout 和用于垂直排列的 boxlayout)来实现我的目标行为。但它既不是令人满意的代码,也没有完全按照我想要的方式工作。

为了这个问题我花了很多时间上网,发现垂直布局是Java中的一个常见问题。很多人回答说 GridBagLayout 将是完成这项工作的最佳布局,但我没有让它按照我想要的方式工作。

我希望这些信息足以为我解决该问题提供一些帮助。

最诚挚的问候迈克尔

编辑:-不再需要-

编辑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);
}
}

最佳答案

参见http://docs.oracle.com/javase/tutorial/uiswing/layout/box.html

setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));

Example

关于Java Swing : Vertical Layout with fixed width and variable height,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13476274/

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