MiGLayout不会扩展JPanel [英] MiGLayout will not expand JPanel down
问题描述
我相信你们当中有些人都知道,我正试图为Tiled制作一个开源的工具。我之前问过我应该使用什么布局,我建议我真正喜欢的MiGLayout,但是根本不理解。我也希望从中学到一些东西。 我想要的是有人向我解释我做错了什么,显然,以及我需要做些什么来纠正这个问题。
As I am sure some of you are aware, I am attempting to make an alernative tool to Tiled that is open source. I asked before what layout I should use and I was suggest the MiGLayout which I really do love, but don't understand that well at all. I also hope to learn something from this as well. What I would like is someone to explain to me what I did wrong, obviously, and what I need to do to correct this.
让我首先说明了什么在我眼中是完美的,但可能不是真的。
Let me first state what works perfect in my eyes, but may not really.
- JFrame
- 菜单&安培;菜单项
现在让我说出我不喜欢的内容,并且不会违背我的意愿。
Now let me state what I don't like and is not bending to my will.
- JToolBar(有我不想要的空白,它们用红色圈出)
- 两个JPanels(它的宽度都很完美) ,但他们没有填补高度)
我的问题是我该怎么做才能解决这个问题和如何调整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);
}
});
}
}
推荐答案
需要设置侧边栏并使用停靠功能在该区域中映射区域。
Needed to set the sidebar and map area in the area by using the docking feature.
例如,我按如下方式执行:
For example I did as follows:
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");
这消除了差距并根据需要扩展了所有内容。
This got rid of gaps and expanded everything as needed.
这篇关于MiGLayout不会扩展JPanel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!