【发布时间】:2013-05-17 11:20:50
【问题描述】:
我希望各种组件展开并填满整个窗口。
您尝试过其他方法吗? 是的,我尝试了GridLayout,但按钮看起来很大。我还尝试了pack(),它使窗口变小了。窗口应该是 750x750 :)
我正在尝试的是:
JPanels 的滚动窗格将包含所有视频转换任务。这样占用空间最大JPanel 包含JProgressBar 作为细条。 但是好像有什么地方搞砸了。 请帮我解决这个问题
SSCCE
import java.awt.*;
import javax.swing.*;
import com.explodingpixels.macwidgets.*;
public class HudTest {
public static void main(String[] args) {
HudWindow hud = new HudWindow("Window");
hud.getJDialog().setSize(750, 750);
hud.getJDialog().setLocationRelativeTo(null);
hud.getJDialog().setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel buttonPanel = new JPanel(new FlowLayout());
JButton addVideo = HudWidgetFactory.createHudButton("Add New Video");
JButton removeVideo = HudWidgetFactory.createHudButton("Remove Video");
JButton startAll = HudWidgetFactory.createHudButton("Start All Tasks");
JButton stopAll = HudWidgetFactory.createHudButton("Stop All Tasks");
buttonPanel.add(addVideo);
buttonPanel.add(startAll);
buttonPanel.add(removeVideo);
buttonPanel.add(stopAll);
JPanel taskPanel = new JPanel(new GridLayout(0,1));
JScrollPane taskScrollPane = new JScrollPane(taskPanel);
IAppWidgetFactory.makeIAppScrollPane(taskScrollPane);
for(int i=0;i<10;i++){
ColorPanel c = new ColorPanel();
c.setPreferredSize(new Dimension(750,100));
taskPanel.add(c);
}
JPanel progressBarPanel = new JPanel();
JComponent component = (JComponent) hud.getContentPane();
component.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
Insets in = new Insets(2,2,2,2);
gbc.insets = in;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 10;
gbc.gridheight = 1;
gbc.fill = GridBagConstraints.BOTH;
component.add(buttonPanel,gbc);
gbc.gridy += 1;
gbc.gridheight = 17;
component.add(taskScrollPane,gbc);
gbc.gridy += 17;
gbc.gridheight = 2;
component.add(progressBarPanel,gbc);
hud.getJDialog().setVisible(true);
}
}
【问题讨论】:
-
com.explodingpixels.macwidgets.*;SSCCE 应该没有外部依赖项。没有第 3 方 API。 :( 你能用标准的JComponent来说明问题吗? -
:-) 很好,请问(这个工具栏)可以贴在哪里
-
@Little Child 我不会这样,将逻辑拆分为具有相同或不同 LayoutManager 的容器(JPanel),那么这项工作将尽可能容易....
-
JComboBox, JTable, JScrollPane 无法在内部正确返回其 PreferredSize,如果是则需要覆盖此值,那么是否存在 setPreferredSize 或 setPreferredScrollableViewportSize 或 setPrototypeDisplayValue 或 setPreferredSize 无关紧要,因为其余的都是关于不良做法的,
-
是的,因为 getPreferredSize 和 setPreferredSize(对于标准 LayoutManagers)没有区别,最好使用 getPreferredSize()
标签: java swing jpanel grid-layout gridbaglayout