我相信你们中的一些人知道,我正在尝试制作一个开源的 Tiled 替代工具。我之前问过我应该使用什么布局,有人建议我使用 MiGLayout,我真的很喜欢它,但根本不明白。我也希望能从中学到一些东西。 我希望有人向我解释我做错了什么,以及我需要做什么来纠正这个问题。
首先让我说明什么在我眼中是完美的,但实际上可能并非如此。
- JFrame
- 菜单和菜单项
现在让我说说我不喜欢什么,什么不屈服于我的意志。
- JToolBar(有我不想要的间隙,它们用红色圈出)
- 两个 JPanel(宽度都很完美,但它们没有填充到高度)
我的问题是我该怎么做才能解决这个问题以及如何调整miglayout,以便在移动工具栏时不会使布局崩溃?
这是我的代码:
package main;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import javax.swing.KeyStroke;
import net.miginfocom.swing.MigLayout;
public class GUI extends JFrame {
// Window Vars //
String title;
int width;
int height;
// Mid Level componets //
JMenuBar menuBar;
JMenu file;
JToolBar toolBar;
JPanel map;
JPanel sideBar;
// Low Level componets //
JMenuItem exit;
JButton select;
public GUI(String title, int width, int height) {
this.title = title;
this.width = width;
this.height = height;
this.makeInterface();
}
public void makeInterface() {
// Setup JFrame
this.setTitle(title);
this.setSize(width, height);
this.setLocationRelativeTo(null);
this.setMinimumSize(new Dimension(700, 500));
this.setVisible(true);
this.setLayout(new MigLayout(
"debug, fillx, gap unrel rel", // Layout
"[grow, fill][fill]", // Column
"[fill][fill]")); // Row
this.makeMenu();
this.addToolBars();
this.makePanels();
this.setupActionListeners();
}
public void makeMenu() {
this.menuBar = new JMenuBar();
this.file = new JMenu("File");
this.file.setMnemonic(KeyEvent.VK_F);
this.menuBar.add(file);
this.exit = new JMenuItem("Exit", KeyEvent.VK_E);
this.exit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E, ActionEvent.ALT_MASK));
this.file.add(exit);
this.setJMenuBar(this.menuBar);
}
public void addToolBars() {
this.toolBar = new JToolBar("Draggable");
this.addToolBarButtons();
this.add(toolBar, "span, height 20:35:50, wrap");
}
public void addToolBarButtons() {
this.select = new JButton("Select");
this.toolBar.add(select);
}
public void makePanels() {
this.map = new JPanel();
this.sideBar = new JPanel();
this.add(map, "width 400:600:, flowy, growy");
this.add(sideBar, "width 250:300:350, flowy, growy");
}
public void setupActionListeners() {
this.exit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
}
}
最佳答案
需要使用停靠功能在区域中设置侧边栏和 map 区域。
例如我做了如下操作:
this.setLayout(new MigLayout(
"fill", // Layout
"", // Column
"")); // Row
this.add(map, "width 400:600:, dock center, growy");
this.add(sideBar, "width 250:300:350, dock east, growy");
这消除了间隙并根据需要扩展了所有内容。
关于java - MiGLayout 不会向下展开 JPanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39558461/