2

ご存知の方もいらっしゃるかと思いますが、私はオープン ソースの Tiled に代わるツールを作ろうとしています。以前、どのレイアウトを使用すべきかを尋ねたところ、私が本当に気に入っている MiGLayout を提案されましたが、それがまったくよくわかりません。私もこのことから何かを学びたいと思っています。私が望むのは、明らかに私が間違っていたことと、これを修正するために何をする必要があるかを説明してくれる人です。

最初に、私の目には完璧に機能するものを述べさせてください。

  • JFrame
  • メニューとメニュー項目

ここで、私が嫌いで、自分の意志に屈していないことを述べさせてください。

  • 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);
            }
        });
    }
}

現在の状態のプログラム

4

1 に答える 1

3

ドッキング機能を使用して、エリアにサイドバーとマップエリアを設定する必要があります。

たとえば、私は次のようにしました。

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");

これによりギャップがなくなり、必要に応じてすべてが拡張されました。

于 2012-07-06T22:24:38.783 に答える