Java - MigLayout 设置边界
所以我有一个无法解决的问题,我希望我的 GUI 应用程序分为 3 个 JPanel(左、中、右)。我希望左面板和右面板具有固定尺寸,并且中心是流动的。这意味着当 JFrame 展开时,侧面板仅垂直展开,而中心面板则水平和垂直展开。
我已将所有面板的最小尺寸设置为高度 600,但它们只是保持在最小尺寸,并且不会随着 JForm 的增加而扩展,我不知道如何设置 JFrame 边框的边界,以便它们随其扩展。
package ppe.view;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import net.miginfocom.swing.MigLayout;
public class UI_View extends JFrame
{
private JList browse = new JList();
private JScrollPane rightX = new JScrollPane();
private JButton btn1 = new JButton("Button 1");
private JButton btn2 = new JButton("Button 2");
private JButton btn3 = new JButton("Button 3");
private JButton btn4 = new JButton("Button 4");
public UI_View()
{
this.setTitle("Prototype MVC Arhitecture");
this.setMinimumSize(new Dimension(800, 600));
this.setExtendedState(this.MAXIMIZED_BOTH);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new MigLayout());
JPanel content = new JPanel(new MigLayout());
content.setBackground(Color.black);
JPanel right = new JPanel(new MigLayout());
JPanel center = new JPanel(new MigLayout());
JPanel left = new JPanel(new MigLayout());
right.setBackground(Color.red);
right.setMinimumSize(new Dimension(200, 600));
right.setMaximumSize(new Dimension(200, 37500));
center.setBackground(Color.green);
center.setMinimumSize(new Dimension(400, 600));
left.setBackground(Color.blue);
left.setMinimumSize(new Dimension(200, 600));
left.setMaximumSize(new Dimension(200, 37500));
content.add(left);
content.add(center);
content.add(right);
this.setContentPane(content);
}
public static void main(String[] args)
{
new UI_View().setVisible(true);
}
}
我尝试将它们绑定到另一个内容面板,并将该面板作为 ContentPane 添加到 JFrame,自动将其绑定到 JFrame 边框,但问题仍然基本修复。
so i have a problem i cant solve i want my GUI app to be split between 3 JPanels (left, center, right). I want left panel and right panel to be of fixed size and center to be fluid. Meaning side panels expand only vertically as JFrame is expanded and center panel expands bot horizontally and vertically.
I have set minimal size for all panels to be height of 600 but they just stay in the minimal size and dont expand as JForm increases i dont know how to set bounds to JFrame borders so they expand whit it.
package ppe.view;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import net.miginfocom.swing.MigLayout;
public class UI_View extends JFrame
{
private JList browse = new JList();
private JScrollPane rightX = new JScrollPane();
private JButton btn1 = new JButton("Button 1");
private JButton btn2 = new JButton("Button 2");
private JButton btn3 = new JButton("Button 3");
private JButton btn4 = new JButton("Button 4");
public UI_View()
{
this.setTitle("Prototype MVC Arhitecture");
this.setMinimumSize(new Dimension(800, 600));
this.setExtendedState(this.MAXIMIZED_BOTH);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new MigLayout());
JPanel content = new JPanel(new MigLayout());
content.setBackground(Color.black);
JPanel right = new JPanel(new MigLayout());
JPanel center = new JPanel(new MigLayout());
JPanel left = new JPanel(new MigLayout());
right.setBackground(Color.red);
right.setMinimumSize(new Dimension(200, 600));
right.setMaximumSize(new Dimension(200, 37500));
center.setBackground(Color.green);
center.setMinimumSize(new Dimension(400, 600));
left.setBackground(Color.blue);
left.setMinimumSize(new Dimension(200, 600));
left.setMaximumSize(new Dimension(200, 37500));
content.add(left);
content.add(center);
content.add(right);
this.setContentPane(content);
}
public static void main(String[] args)
{
new UI_View().setVisible(true);
}
}
I have tryed bounding them to another content panel and adding that panel as ContentPane to JFrame that automatically bounds it to JFrame border but the thing is still pretty much fixed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用
MiGLayout
为什么不在添加组件时配置约束?例如,这可能会有所帮助(尽管我也是
MiGLayout
初学者):在某些情况下,我还需要添加
pushx
但我不确定何时这是需要的。请参阅文档以获取更多信息。编辑:似乎您总是必须为导致列/行增长的组件添加
push
。否则,单独的“增长”将使组件与它们所在的列/行一样大,而这些组件又由该列/行中最大的组件定义。如果有更多可用空间,则如果没有push
关键字,可用列/行将不会增长以填充它。从文档中:
If you're using
MiGLayout
why don't you configure the constraints when adding the components?This, for example, might help (although I'm a
MiGLayout
beginner, too):In some cases I needed to also add a
pushx
but I'm not sure when this is needed. Please refer to the documentation for further information.Edit: it seems like you always have to add
push
for components that should cause the column/row to grow. Otherwisegrow
alone would make the components as big as the column/row they are in which in turn is defined by the largest component in that column/row. If there is more space available columns/rows won't grow to fill it without thepush
keyword.From the documentation: