什么布局管理器可以制作面板图形用户界面?
What Layout Manager can make a paneled GUI?
我想做这样的申请:
------------------------------
| Toolbar |
------------------------------
| | |
| | |
| |-------------|
| | |
| | |
------------------------------
哪个布局管理器适合屏幕link这个?
我推荐GroupLayout
。一旦您习惯了它,就没有什么能像 GroupLayout
那样为您提供调整组件大小的控制类型。你可以使用这样的东西:
import java.awt.EventQueue;
import javax.swing.*;
import javax.swing.border.BevelBorder;
public class LayoutFrame extends JFrame {
private JPanel contentPane;
private JMenuBar menuBar;
public LayoutFrame() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
// Create the toolbar
menuBar = new JMenuBar();
menuBar.add(new JMenu("File"));
setJMenuBar(menuBar);
setSize(500, 500);
contentPane = new JPanel();
GroupLayout layout = new GroupLayout(contentPane);
contentPane.setLayout(layout);
setContentPane(contentPane);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
// Create the panels for the different regions
JPanel leftPanel = new JPanel();
leftPanel.setBorder(new BevelBorder(BevelBorder.LOWERED));
JPanel topRightPanel = new JPanel();
topRightPanel.setBorder(new BevelBorder(BevelBorder.LOWERED));
JPanel bottomRightPanel = new JPanel();
bottomRightPanel.setBorder(new BevelBorder(BevelBorder.LOWERED));
// Create a sequential group for the horizontal axis.
GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();
hGroup.addComponent(leftPanel);
hGroup.addGroup(layout.createParallelGroup().
addComponent(topRightPanel).addComponent(bottomRightPanel));
layout.setHorizontalGroup(hGroup);
// Create a parallel group for the vertical axis.
GroupLayout.ParallelGroup vGroup = layout.createParallelGroup();
vGroup.addComponent(leftPanel);
vGroup.addGroup(layout.createSequentialGroup().
addComponent(topRightPanel).addComponent(bottomRightPanel));
layout.setVerticalGroup(vGroup);
}
public static void main(String[] args) {
final LayoutFrame frame = new LayoutFrame();
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
frame.setVisible(true);
}
});
}
}
这将创建此框架 (Ubuntu Unity)。您应该能够自己弄清楚如何调整间隙。
还有自动间隙:
我想做这样的申请:
------------------------------
| Toolbar |
------------------------------
| | |
| | |
| |-------------|
| | |
| | |
------------------------------
哪个布局管理器适合屏幕link这个?
我推荐GroupLayout
。一旦您习惯了它,就没有什么能像 GroupLayout
那样为您提供调整组件大小的控制类型。你可以使用这样的东西:
import java.awt.EventQueue;
import javax.swing.*;
import javax.swing.border.BevelBorder;
public class LayoutFrame extends JFrame {
private JPanel contentPane;
private JMenuBar menuBar;
public LayoutFrame() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
// Create the toolbar
menuBar = new JMenuBar();
menuBar.add(new JMenu("File"));
setJMenuBar(menuBar);
setSize(500, 500);
contentPane = new JPanel();
GroupLayout layout = new GroupLayout(contentPane);
contentPane.setLayout(layout);
setContentPane(contentPane);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
// Create the panels for the different regions
JPanel leftPanel = new JPanel();
leftPanel.setBorder(new BevelBorder(BevelBorder.LOWERED));
JPanel topRightPanel = new JPanel();
topRightPanel.setBorder(new BevelBorder(BevelBorder.LOWERED));
JPanel bottomRightPanel = new JPanel();
bottomRightPanel.setBorder(new BevelBorder(BevelBorder.LOWERED));
// Create a sequential group for the horizontal axis.
GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();
hGroup.addComponent(leftPanel);
hGroup.addGroup(layout.createParallelGroup().
addComponent(topRightPanel).addComponent(bottomRightPanel));
layout.setHorizontalGroup(hGroup);
// Create a parallel group for the vertical axis.
GroupLayout.ParallelGroup vGroup = layout.createParallelGroup();
vGroup.addComponent(leftPanel);
vGroup.addGroup(layout.createSequentialGroup().
addComponent(topRightPanel).addComponent(bottomRightPanel));
layout.setVerticalGroup(vGroup);
}
public static void main(String[] args) {
final LayoutFrame frame = new LayoutFrame();
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
frame.setVisible(true);
}
});
}
}
这将创建此框架 (Ubuntu Unity)。您应该能够自己弄清楚如何调整间隙。
还有自动间隙: