MigLayout:相对于屏幕尺寸调整组件的大小 [英] MigLayout: Resize components relatively to a screen size
问题描述
我正在使用 MigLayout
来对齐 Swing 应用程序中的组件.我有 2 个 JPanel
组件和一个工具栏.我想将工具栏放在顶部,然后将 3 个面板并排放置,但是它们的大小应以百分比 (%) 定义并且相对于屏幕大小.我知道如何根据固定大小 (px) 对齐所有这些组件,但是如何切换到相对于屏幕大小的 %?
I am using MigLayout
to align components in the Swing application. I have 2 JPanel
components and a toolbar. I want to put toolbar on top and then to put 3 panels next to each other, however their size should be defined in percentages (%) and be relative to a screen size.
I know how to align all these components based on fixed size (px), however how can I switch to % relative to screen size?
JPanel contentPanel = new JPanel();
contentPanel.setBorder(BorderFactory.createEmptyBorder(0, 4, 4, 4));
contentPanel.setLayout(new MigLayout("fillx,insets 1"));
JScrollPane westPanel = new JScrollPane(createParametersPanel());
JScrollPane eastPanel = new JScrollPane(createPanel());
JEditorPane editor = new JEditorPane("text/plain", "Hello World");
contentPanel.add(toolbar,"wrap");
contentPanel.add(westPanel,"width :200:");
contentPanel.add(editor,"width :200:");
contentPanel.add(eastPanel,"width :400:");
setContentPane(contentPanel);
推荐答案
可以在 MigLayout
中直接使用 % width 试试这个例子:
It's possible to use directly % width inside MigLayout
try this example :
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import net.miginfocom.swing.MigLayout;
public class Main
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(100, 100, 500, 500);
JPanel contentPanel = new JPanel();
contentPanel.setLayout(new MigLayout("insets 4 4 4 4",
"[fill,30%][fill,40%][fill,30%]", "[fill,grow]"));
JMenuBar menubar = new JMenuBar();
JMenu menu = new JMenu("File");
JMenuItem item = new JMenuItem("Open");
menu.add(item);
menubar.add(menu);
frame.setJMenuBar(menubar);
contentPanel.add(new JScrollPane());
contentPanel.add(new JScrollPane(new JEditorPane("text/plain", "Hello World")));
contentPanel.add(new JScrollPane());
frame.setContentPane(contentPanel);
frame.setVisible(true);
}
}
要完全理解如何使用 MigLayout
,请按照此 白皮书.
To fully undestand how to use MigLayout
follow this Whitepaper.
这篇关于MigLayout:相对于屏幕尺寸调整组件的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!