- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使 JPanel
(“父面板”)的内容从上到下添加,并使每个组件的宽度与父面板的宽度相匹配同时让每个组件的高度包裹内容。内容将动态生成。
这是我想要的样子:
这在 Android 中相当容易做到。我研究了 Swing LayoutManagers,但似乎没有一个是显而易见的选择。我尝试过使用 Boxlayout
(垂直),除了面板没有填满父面板的宽度(这使它看起来非常糟糕)之外它可以工作。
感谢任何建议!
编辑:
感谢 camickr 的回答,我明白了。为了防止将来对任何人有所帮助,我提供了我的代码和屏幕截图。
解题代码:
(注意GridBagLayout、BorderLayout的使用和GridBagConstraints的参数)
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Test extends JFrame{
private final Color darkGreen = Color.decode("#388E3C");
private final Color green = Color.decode("#4CAF50");
private final Color lightGreen = Color.decode("#C8E6C9");
public static void main(String[] args){
Test test = new Test();
test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
int noOfTitles = 4;
int noOfSubTitles = 3;
Random random = new Random();
public Test(){
JPanel parentPanel = new JPanel();
this.setSize(new Dimension(300,800));
this.setContentPane(parentPanel);
parentPanel.setBackground(Color.white);
//Set layout of parent panel to Gridlayout
parentPanel.setLayout(new GridBagLayout());
//Panel for our titles
JPanel titlesPanel = new JPanel();
titlesPanel.setLayout(new BoxLayout(titlesPanel, BoxLayout.Y_AXIS));
for(int i = 0;i<noOfTitles;i++){
//Panel for the subtitles of each title
JPanel subTitlesPanel = new JPanel();
subTitlesPanel.setLayout(new BoxLayout(subTitlesPanel, BoxLayout.Y_AXIS));
for(int j = 0;j<noOfSubTitles;j++){
//Get a panel with a title header, a collapse/uncollapse button and some sample content
subTitlesPanel.add(getCollapsiblePanel(getSampleContent(),"Subtitle"));
subTitlesPanel.setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 0));
}
//Get a panel with a title header, a collapse/uncollapse button and the subtitles as content
titlesPanel.add(getCollapsiblePanel(subTitlesPanel,"Title"));
}
//The constraints for the
GridBagConstraints constraints = new GridBagConstraints();
constraints.anchor = GridBagConstraints.PAGE_START;
constraints.fill = GridBagConstraints.HORIZONTAL; //Fill the panels horizontally. A weightx is needed for this to work.
constraints.gridx = 1;
constraints.gridy = 0;
constraints.weightx = 1; //Actually makes it fill
parentPanel.add(titlesPanel,constraints);
//To actually bump the rest of the other stuff to the top,
//we need something below with a weighty > 0
constraints.gridy = 1;
constraints.weighty = 1;
constraints.anchor = GridBagConstraints.PAGE_END;
parentPanel.add(new JLabel(""),constraints);
this.setVisible(true);
}
/*
* Gets a title for the supplied content-panel.
* The title includes the titleText and a button for collapsing/uncollapsing the content.
*/
private JPanel getCollapsiblePanel(JPanel content,String titleText){
JPanel titlePanel = new JPanel(); //Top container for the title
JPanel title = new JPanel(); //collapse/uncollapse button and title text
title.setLayout(new BoxLayout(title,BoxLayout.X_AXIS));
title.add(getToggleVisibilityIcon(content));
title.add(new JLabel(" "+titleText));
//A border layout is needed here for the fill of the parent panel to work
// (I tried with the box layout but it didn't work)
titlePanel.setLayout(new BorderLayout());
titlePanel.add(title,BorderLayout.PAGE_START);
titlePanel.add(content,BorderLayout.CENTER);
//Vanity
title.setBackground(green);
title.setBorder(BorderFactory.createEmptyBorder(1,1,2,1));
return titlePanel;
}
/*
* Not important, just generates a panel with some "sample" strings.
*/
private JPanel getSampleContent(){
JPanel content = new JPanel();
content.setLayout(new BoxLayout(content,BoxLayout.Y_AXIS));
for(int i = 0; i<1+random.nextInt(3); i++){
String sampleContent = "";
for(int j = 0;j<1 + random.nextInt(5);j++){
sampleContent += "sample ";
}
JLabel sample = new JLabel(sampleContent);
sample.setForeground(Color.decode("#111111"));
content.add(sample);
}
content.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
content.setBackground(lightGreen);
return content;
}
/*
* Method that returns a JLabel that will toggle the visibility of the
* supplied content panel upon clicking.
*/
private JLabel getToggleVisibilityIcon(JPanel content){
JLabel icon = new JLabel(" V ");
icon.setBackground(green);
icon.setBorder(BorderFactory.createLineBorder(darkGreen,2));
icon.setOpaque(true);
icon.addMouseListener(new MouseListener(){
private JPanel content;
private JLabel icon;
MouseListener init(JPanel content,JLabel icon){
this.content = content;
this.icon = icon;
return this;
}
@Override
public void mouseClicked(MouseEvent e) {
if(content.isVisible()){
content.setVisible(false);
icon.setText(" > ");
}else{
content.setVisible(true);
icon.setText(" V ");
}
}
@Override
public void mousePressed(MouseEvent e) {}
@Override
public void mouseReleased(MouseEvent e) {}
@Override
public void mouseEntered(MouseEvent e) {}
@Override
public void mouseExited(MouseEvent e) {}
}.init(content,icon));
return icon;
}
}
正在运行的应用程序的屏幕截图:
最佳答案
不是很理解你的问题。您通常没有随机换行的文本、按钮和组合框。通常面板有布局,因此组件在逻辑上组织。也就是说,您不希望标签带有“名字”之类的文本,后跟一个随机换行到下一行的文本字段。
在我看来,您有一个隐藏或显示的“详细信息面板”?
好吧,您可以使用 BorderLayout 来实现它。因此隐藏/显示按钮将放置在 BorderLayout.PAGE_START 中。然后详细信息面板将位于 BorderLayout.CENTER 中。然后根据需要设置详细信息面板的可见性。
然后顶层容器可以是 GridBagLayout
。您可以使用约束让每一行填充单元格的宽度。每个子面板都可以使用 Border
使其在左侧略微缩进。
阅读Swing tutorial .有以下部分:
关于java - 具有与父级宽度匹配并包裹内容高度的子级的 JPanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33919591/
我的理解是 width: 100% 让元素的宽度与其父元素的宽度相同,而 width: inherit 只有在明确指定父元素的宽度时才这样做.这种理解是否正确? 如果是这样,在我看来,当 width:
并设置“高度”为全屏的 1/2。 这是我的代码: div{ background:red; } 最佳答案 我会结合使用 css 和 javascript(使
编辑 2: 问题似乎出在规则的“bigTable”元素上。显然,在布局模板上使用时继承了错误的最小宽度。我仍在调查此事。 不过,我将再尝试一次 div。一个大问题是使用固定导航和动态内容,但我已经为此
我的网站需要显示宽表。在它上面是标题,它应该和整个页面一样宽(在这种情况下,和表格一样宽)。但是,它的宽度与视口(viewport)(屏幕尺寸)一样宽,因此显示时看起来还不错,但是一旦用户滚动到侧面,
我有一个小问题。我总是使用 float 来安排我的元素。我正在转向 flexbox,我做了一些例子,一切都很好,但我正在做一个事情进展不顺利的例子。 我有一个包含 1 到 12 种产品的容器,每行 4
例如,它们在自动边距方面会导致完全不同的行为。 看看这个 fiddle :https://jsfiddle.net/L1rk46xy/ .fixed { display:fixed;
我尝试在帖子中将段落的宽度设置为 75%,并将图像的响应宽度设置为 100%。然而,总是在 默认。 Some texts Some texts Some texts Some texts 目前,我只
HTML 元素可以有 width/height 属性,也可以有 CSS width/height 属性: HTML 属性和 CSS 属性有什么区别,它们应该具有相同的效果吗? 最佳答案 有关该主题的
我有一个流动的 table ,现在需要一个固定的 thead。问题是当你固定 thead 时,th-s 不尊重 tbody 的 td-s 的宽度。列的大小都由 BootStrap 处理。我已经阅读了很
我想像这样布置一个区域: ---- ---- |A | |B | | | | | ---- ---- --------- |C | --------- 三个盒子中的每一个都是 .盒子
我遇到了很多问题。 1) 我正在使用 Bootstrap-Select 来获得具有搜索功能的现代选择框,但无论我尝试什么,我似乎都无法获得填充 col-span 的选择。 2) 我已将该行拆分为 2
http://jsfiddle.net/95EtZ/1/ 问题在行动中解决了一半。 现在它是用 javascript 中硬编码的容器宽度设置的。 我需要 js 来获取容器 div 的宽度——使用窗口滚
我想要两个宽度和高度均为 100% 的 div。我知道子 div 不会工作,因为父 div 没有特定的高度,但有没有办法解决这个问题? HTML: CSS: body
我需要使用 jQuery 更改 的高度和宽度 我尝试了以下代码 jQuery('#chart_popup').css('height','600px'); jQuery('#chart_popup')
在自定义 WPF 控件中,我想将控件的宽度设置为高度的函数。例如:Width = Height/3 * x; 实现此目的的最佳方法是什么,以便控件正确且流畅地调整大小(和初始大小)? 最佳答案 您可以
我正在使用igraph在R中绘制图形,执行plot(mygraph, vertex.color = "green")之类的操作。 有没有办法改变顶点边界的颜色和/或宽度? 最佳答案 查看下面的代码;
有没有办法使用jquery设置图像的高度和宽度?以下是我的代码 var img = new Image(); // Create image $(img).load(function(){
这个问题类似于 how-to-find-the-actual-width-of-grid-component-with-scrollbar-in-delphi 但我无法获取 CalcDrawInfo(
这里是 HTML/CSS 新手。 试图将我在 Codeacademy 上学到的知识付诸实践,但我遇到了一个问题,即我设置为 width:100% 的 header 最终离开了页面。我相信这是因为边框,
这里是 HTML/CSS 新手。 试图将我在 Codeacademy 上学到的知识付诸实践,但我遇到了一个问题,即我设置为 width:100% 的 header 最终离开了页面。我相信这是因为边框,
我是一名优秀的程序员,十分优秀!