- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在寻找一个布局管理器(或一组布局管理器)来帮助我垂直排列我的组件。所有组件(按钮)应具有相同的宽度,以底层对话框客户区宽度为边界。这些按钮中的每一个都能够根据连接的 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));
关于Java Swing : Vertical Layout with fixed width and variable height,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13476274/
我想了解这两者的区别 谢谢 最大宽度:100px 宽度:100px body{ max-width: 1080px; width: 1080px; backgrou
jQuery 的 .width()、jQuery 的 .css('width') 和 .style 返回的值有什么区别.width 节点上的属性? 我很好奇,因为在尝试将两个表格单元格的宽度设置为相等
我可以在 wordpress 的 header php 中添加两个视口(viewport): 和 我希望能够使用 viewport width 1024 显示everything,除非您是低于 x
好的,所以我得到了 ImageBackground 的以下组件(使用 typescript ,但应该是不言自明的)。 (这是 react 原生的) import * as React from "re
我正在尝试根据 div 中包含的图像的宽度调整其大小,但没有任何 JS 方法可以一致地工作来获取图像的宽度。 label here img { width: auto
我有一个表,我试图找到表中每个 td 的宽度。我尝试了各种变体:$("td")[0].width(),但它们都不起作用,收到错误:"Uncaught TypeError: $ (...)[0].wid
我在使 rem 中的 min-width 与百分比宽度共存时遇到了一些问题。 When resizing this jsfiddle the outer right block is going un
当我在 chrome(以及 Edge/Brave/Opera)的设备模式下测试页面时,设备宽度属性似乎减少了,但宽度属性没有 重新创建: html {
我想依次显示 div #botone、#bottwo 和 #bottree。我还希望在 #botone div 上按 100% 宽度和比例高度调整背景图像。 现在最后两个 Div 显示在 #boton
我是第一次学习 JavaScript 的学生,我有一个学校问题,我们需要指出 width 和 style.width 接受的数据类型。 我知道数据类型有数字、字符串、空值、未定义、 bool 值和对象
所以看起来在移动设备中媒体查询使用 screen.width 根本没有改变。但是在我的桌面上,当我更改窗口大小时,screen.width 保持不变,$(window).width() 发生变化,并且
http://jsfiddle.net/3BFGU/27/ 有谁知道为什么包含文本“ABC”的跨度的宽度返回 0。 1) 仅在 Firefox 中发生。2)如果我删除两者之间的跨度,它工作正常。 (h
我知道下面的代码清除了 Canvas canvas.width = canvas.width 但这在内部是如何工作的呢? 赋值运算符只是将一些值赋给变量,但是上面的代码如何清除 Canvas ? 根据
当使用 max-width 时,为什么它不会“破坏”超过允许长度的单词,我该如何让它工作? JSFiddle function input() { var inputText = document
在移动网站上,在利用 width=device-width 视口(viewport)设置的同时调整文本大小的最佳方式是什么? 最佳答案 样式百分比如何?比如 100% 或 90%。 关于html -
对于我的 WPF Toolkit DataGrid,我使用以下自定义列标题样式:
过去,我看到下一个 css,我在想两者之间是否存在一些实际差异 min-width: 90px; max-width: 90px; 和 width: 90px; 最佳答案 使用 width 将简单地在
我有一个 R Markdown,我从 RStudio 中将其编织为 html。我可以使用 fig.width 和 fig.height 来调整我的绘图的分辨率,但我似乎无法加宽页面输出,以便文本输出不
此处的示例代码:http://pastebin.com/95z3pftQ 我正在尝试构建一个带有固定标题和“内容”部分的移动页面,该部分将填充外部(经过清理,但在其他方面是任意的)HTML。我需要使用
这里是 dp 单位的定义:(如果它是正确的...) 密度无关像素 - 一种基于屏幕物理密度的抽象单位。这些单位是相对于 160 dpi 屏幕而言的,因此 1 dp 是 160 dpi 屏幕上的一个像素
我是一名优秀的程序员,十分优秀!