2012-07-04 127 views
2

正如我相信你們中的一些人知道的,我正在嘗試爲開源的Tiled製作一個合適的工具。我問之前我應該​​使用什麼樣的佈局,並且我建議我真的很喜歡MiGLayout,但是根本不理解。我也希望從中學到一些東西。 我想要的是有人向我解釋我做錯了什麼,顯然,我需要做些什麼來糾正這一點。MiGLayout不會擴展JPanel

讓我先說明什麼在我眼中完美,但可能不是真的。

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

Program in its current state

+0

我想出了一個問題,我需要設置行約束來填充和增長下第二行。仍然不確定如何處理差距問題並移動工具欄問題。 – Zeveso

回答

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

這擺脫了差距,並根據需要擴展的一切。