我是Java Swing的新手,我不熟悉不同的布局管理器。
我想创建一个框架,其中有3个面板,顶部有一个菜单栏,如下所示:
我曾尝试过边框布局,但只有在没有菜单栏的情况下才有效。我应该使用什么布局管理器?
答案 0 :(得分:0)
您应该使用GridLayout,您可以指定行数和列数并向其中添加元素,例如,如果您说要2行3列并开始向其添加元素,则首先将3行添加到第1行和第3行你将基本上创建一个表并从左到右填充单元格从上到下(默认情况下),所以在你的情况下,你需要3行和1列,这里有一个例子可以帮助你
public class GridLayoutTut {
private JFrame mainFrame;
public static void main(String[] args) {
GridLayoutTut swingLayoutDemo = new GridLayoutTut();
swingLayoutDemo.showStuff();
}
private void showStuff() {
JMenuBar menuBar = new JMenuBar();
JMenu menu;
menu = new JMenu("My menu");
menuBar.add(menu);
mainFrame = new JFrame("Java SWING Examples");
mainFrame.setSize(400, 400);
JPanel comp = new JPanel();
comp.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.BLACK), "Pane 1"));
JPanel comp2 = new JPanel();
comp2.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.BLACK), "Pane 2"));
JPanel comp3 = new JPanel();
comp3.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.BLACK), "Pane 3"));
mainFrame.setLayout(new GridLayout(3, 1)); // 3 rows 1 column
mainFrame.add(comp);
mainFrame.add(comp2);
mainFrame.add(comp3);
mainFrame.add(menuBar);
mainFrame.setJMenuBar(menuBar);
mainFrame.setVisible(true);
}
}
答案 1 :(得分:0)
我建议创建一个自定义LayoutManager。
这是一个基本的,可以满足您的要求:
import java.awt.*;
import java.util.ArrayList;
public class MyLayoutManager implements LayoutManager {
private final int TOOLBAR_HEIGHT = 30;
private final int WINDOW_WIDTH = 800;
private final int WINDOW_HEIGHT = 600;
// CONSTRAINTS //
public static final String TOOLBAR = "TOOLBAR";
public static final String BODY = "BODY";
private ArrayList<Component> toolbarComponents = new ArrayList<>();
private ArrayList<Component> bodyComponents = new ArrayList<>();
@Override // Called when JFrame.add() is used //
public void addLayoutComponent(String constraints, Component component) {
switch(constraints) {
case "TOOLBAR": toolbarComponents.add(component); break;
case "BODY": bodyComponents.add(component); break;
}
}
@Override // Sets the bounds for each component //
public void layoutContainer(Container parent) {
// Toolbar //
int currentWidth = 0;
for(Component c : toolbarComponents) {
int width = c.getPreferredSize().width;
c.setBounds(currentWidth, 0, width, TOOLBAR_HEIGHT);
currentWidth += width;
}
// Body //
int currentHeight = TOOLBAR_HEIGHT;
for(Component c : bodyComponents) {
int height = (parent.getHeight()-TOOLBAR_HEIGHT)/bodyComponents.size();
c.setBounds(0, currentHeight, parent.getWidth(), height);
currentHeight += height;
}
}
@Override
public Dimension minimumLayoutSize(Container parent) {
return preferredLayoutSize(parent);
}
@Override // Called when JFrame.pack() is called //
public Dimension preferredLayoutSize(Container parent) {
return new Dimension(WINDOW_WIDTH,WINDOW_HEIGHT);
}
@Override
public void removeLayoutComponent(Component comp) {}
}
然后像这样实现它:
JFrame frame = new JFrame("Interface");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new MyLayoutManager());
frame.add(new JButton("Menu"), MyLayoutManager.TOOLBAR);
frame.add(new JButton("File"), MyLayoutManager.TOOLBAR);
frame.add(new JButton("Settings"), MyLayoutManager.TOOLBAR);
JTextArea area1 = new JTextArea();
area1.setBorder(new TitledBorder("title 1"));
JTextArea area2 = new JTextArea();
area2.setBorder(new TitledBorder("title 2"));
frame.add(area1, MyLayoutManager.BODY);
frame.add(area2, MyLayoutManager.BODY);
frame.pack();
frame.setVisible(true);
答案 2 :(得分:0)
JFrame
类有一个setJMenuBar(JMenuBar)
方法,
以便JMenuBar
自动显示在JFrame
的顶部。
对于面板的布局,我建议使用GridBagLayout
。
通过添加3个不同GridBagConstraints
的面板
你可以调整JFrame
中的尺寸/位置。
有关这些内容的含义,您需要阅读GridBagConstraints
的javadoc。
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar menuBar = new JMenuBar();
JMenu menu1 = new JMenu("Menu1");
JMenu menu2 = new JMenu("Menu2");
JMenu menu3 = new JMenu("Menu3");
menuBar.add(menu1);
menuBar.add(menu2);
menuBar.add(menu3);
frame.add(menuBar);
frame.setJMenuBar(menuBar);
JPanel panel1 = new JPanel();
panel1.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.BLACK), "Panel 1"));
JPanel panel2 = new JPanel();
panel2.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.BLACK), "Panel 2"));
JPanel panel3 = new JPanel();
panel3.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.BLACK), "Panel 3"));
frame.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1;
gbc.gridy = 0;
gbc.weighty = 0.1;
frame.add(panel1, gbc);
gbc.gridy = 1;
gbc.weighty = 0.3;
frame.add(panel2, gbc);
gbc.gridy = 2;
gbc.weighty = 0.2;
frame.add(panel3, gbc);
frame.setSize(400, 400);
frame.setVisible(true);