2013-03-12 50 views
4

我目前正在開發的小工具有以下GUI程序:如何這個GUI中的Java Swing

Sample GUI

現在我有一個容器(JPanel中)與在擁有一切BorderLayout的佈局地點。我有另外兩個JPanels分別放在BorderLayout.NORTH和BorderLayout.SOUTH上,每個都有GridLayout(2列1行)。桌子放在中心的主容器上。

您認爲這是最好的方法嗎?我在處理組件和框架邊界之間的間隔時很困難。

現在,我有這樣的代碼:

import java.awt.BorderLayout; 
import java.awt.Dimension; 
import java.awt.GridLayout; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JMenu; 
import javax.swing.JMenuBar; 
import javax.swing.JMenuItem; 
import javax.swing.JPanel; 
import javax.swing.JSeparator; 
import javax.swing.JTable; 
import javax.swing.SwingUtilities; 

public class GUI extends JFrame { 

    private JButton loadFileBtn = new JButton("Load File"); 
    private JButton generateReportBtn = new JButton("Generate Report"); 
    private JButton exitBtn = new JButton("Exit"); 
    private JLabel fileNameLbl = new JLabel("File Name Here"); 
    private JMenuBar menuBar = new JMenuBar(); 
    private JMenu fileMI = new JMenu("File"); 
    private JMenuItem openFileMenu = new JMenuItem("Open File"); 
    private JSeparator separator = new JSeparator(); 
    private JMenuItem exitMenu = new JMenuItem("Exit"); 
    private JMenu reportMI = new JMenu("Report"); 
    private JMenuItem generateReportMenu = new JMenuItem("Generate Report"); 
    private JMenu helpMI = new JMenu("Help"); 
    private JMenuItem aboutMenu = new JMenuItem("About"); 
    private JTable table = new JTable(5, 2); 
    private JPanel mainPanel = new JPanel(new BorderLayout(10, 10)); 
    private JPanel panel1 = new JPanel(new BorderLayout()); 
    private JPanel panel2 = new JPanel(new GridLayout(1, 2)); 
    private JPanel panel3 = new JPanel(new GridLayout(1, 2)); 

    public GUI() { 

     super("Sample GUI"); 

     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setSize(new Dimension(300, 300)); 
     setResizable(false); 

     setLayout(new BorderLayout(10, 10)); 

     fileMI.add(openFileMenu); 
     fileMI.add(separator); 
     fileMI.add(exitMenu); 

     reportMI.add(generateReportMenu); 

     helpMI.add(aboutMenu); 

     menuBar.add(fileMI); 
     menuBar.add(reportMI); 
     menuBar.add(helpMI); 

     setJMenuBar(menuBar); 

     panel1.add(table, BorderLayout.CENTER); 

     panel2.add(fileNameLbl); 
     panel2.add(loadFileBtn); 

     panel3.add(generateReportBtn); 
     panel3.add(exitBtn); 

     mainPanel.add(panel2, BorderLayout.NORTH); 
     mainPanel.add(panel1, BorderLayout.CENTER); 
     mainPanel.add(panel3, BorderLayout.SOUTH); 

     add(mainPanel); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       GUI app = new GUI(); 
       app.setVisible(true); 
      } 
     }); 
    } 
} 

你認爲會做到這一點,最好的方法?

任何幫助,將不勝感激。謝謝。


UPDATE:

現在,我有以下GUI

enter image description here

我想要的組件從空間的邊界遠均勻,就像在實體模型。

+0

您的問題究竟是什麼?您想介紹哪些空間並導致問題?你總是可以使用'BorderFactory.createEmptyBorder(int,int,int,int)' – 2013-03-12 14:19:21

+0

@Guillaume Polet不確定,請不要攻擊,你的意思是使用'EmptyBorder'來避免'setPreferredSize',或者使用'EmptyBorder'在'NestedLayout'窗體,'MigLayout'或... – mKorbel 2013-03-12 14:25:30

+0

@mKorbel中避免使用'GridBagLayout'的'SpringLayout'空白邊框是一個非常簡單的(有時候是優雅的)解決方案,可以正確定位容器中的組件,而無需切換到另一個LayoutManager。如果OP對這些LayoutManager感到滿意,空白的邊界可能是解決他的實際問題的好辦法。 – 2013-03-12 14:32:55

回答

4

2東西,你可以用它來做到這一點:

  1. 使用BorderFactory.createEmptyBorder(int, int, int, int)
  2. 使用的4參數的構造GridLayout

還有其他LayoutManager的可攜帶相同的功能(如GridBagLayout或使用嵌套BorderLayout),但如果您對目前的感到滿意的,沒有迫切需要改變爲那些。你做的方式也是可以接受的。

enter image description here

你可能會考慮包裝的表在JScrollPane,使之更好,用頭和滾動條在需要時。

小例子代碼:

import java.awt.BorderLayout; 
import java.awt.Dimension; 
import java.awt.GridLayout; 

import javax.swing.BorderFactory; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JMenu; 
import javax.swing.JMenuBar; 
import javax.swing.JMenuItem; 
import javax.swing.JPanel; 
import javax.swing.JSeparator; 
import javax.swing.JTable; 
import javax.swing.SwingUtilities; 

public class GUI extends JFrame { 

    private JButton loadFileBtn = new JButton("Load File"); 
    private JButton generateReportBtn = new JButton("Generate Report"); 
    private JButton exitBtn = new JButton("Exit"); 
    private JLabel fileNameLbl = new JLabel("File Name Here"); 
    private JMenuBar menuBar = new JMenuBar(); 
    private JMenu fileMI = new JMenu("File"); 
    private JMenuItem openFileMenu = new JMenuItem("Open File"); 
    private JSeparator separator = new JSeparator(); 
    private JMenuItem exitMenu = new JMenuItem("Exit"); 
    private JMenu reportMI = new JMenu("Report"); 
    private JMenuItem generateReportMenu = new JMenuItem("Generate Report"); 
    private JMenu helpMI = new JMenu("Help"); 
    private JMenuItem aboutMenu = new JMenuItem("About"); 
    private JTable table = new JTable(5, 2); 
    private JPanel mainPanel = new JPanel(new BorderLayout(10, 10)); 
    private JPanel panel1 = new JPanel(new BorderLayout()); 
    private JPanel panel2 = new JPanel(new GridLayout(1, 2, 10, 10)); 
    private JPanel panel3 = new JPanel(new GridLayout(1, 2, 10, 10)); 

    public GUI() { 

     super("Sample GUI"); 

     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setSize(new Dimension(300, 300)); 
     setResizable(false); 

     setLayout(new BorderLayout(10, 10)); 

     fileMI.add(openFileMenu); 
     fileMI.add(separator); 
     fileMI.add(exitMenu); 

     reportMI.add(generateReportMenu); 

     helpMI.add(aboutMenu); 

     menuBar.add(fileMI); 
     menuBar.add(reportMI); 
     menuBar.add(helpMI); 

     setJMenuBar(menuBar); 

     panel1.add(table, BorderLayout.CENTER); 

     panel2.add(fileNameLbl); 
     panel2.add(loadFileBtn); 

     panel3.add(generateReportBtn); 
     panel3.add(exitBtn); 

     mainPanel.add(panel2, BorderLayout.NORTH); 
     mainPanel.add(panel1, BorderLayout.CENTER); 
     mainPanel.add(panel3, BorderLayout.SOUTH); 
     mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); 
     add(mainPanel); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       GUI app = new GUI(); 
       app.setVisible(true); 
      } 
     }); 
    } 
} 
+0

這正是我想要的。謝謝@Guillaume Polet。 我也試過GridBagLayout,但我似乎無法理解這個概念。你會推薦一些好的導師嗎?對一個初學者來說,一個人的網頁並不容易。 – DaveQuinn 2013-03-12 14:40:09

+0

hmmmm你知道空JPanels添加到W,E,S,N區域到JFrame自動創建這個空白,沒有理由爲這個想法添加EmptyBorders :-) – mKorbel 2013-03-12 14:41:16

+0

@mKorbel你是否建議添加空的'JPanel '強制首選大小的JFrame的西部,東部,北部和南部? – 2013-03-12 14:43:33

4

在過去,我已經發現,使用MigLayout,解決了我的所有問題

+1

是的,'MigLayout'幾乎總是我走的路。非常容易學習,並且非常易於使用。 – wchargin 2013-03-12 14:38:24

+0

不知道這一點。 Thaks,會檢查出來。 – DaveQuinn 2013-03-12 14:42:58

2

的最好方法是創建一個GUI是一種方式,你和其他人會明白的代碼。看看Guide to Layout Managers。您可以爲不同的組件使用不同的佈局管理器。這將幫助您設置正確的間距。

如果您的問題只是組件的間距,請設置其Insets

+0

您對這種設計推薦哪種佈局管理器?這是我的問題背後的動機。 – DaveQuinn 2013-03-12 14:31:14

+0

GridBagLayout在我看來是最靈活的。但也許我會混合一些佈局經理。使用BorderLayout將面板放置在頂部和底部,並將桌面放在中間。底部的面板我會使用FlowLayout,面板int頂部可能是FlowLayout或GridBagLayout。或者用GridBagLayout來做所有事情(很多編碼)。 – 2013-03-12 14:38:13

3

下面是我將如何安排您的GUI。這只是一種方式。這不是唯一的方法。

  • JTable進入JScrollPane的內部。

  • Button2 and Button3走進按鈕JPanelFlowLayout

  • Label and Button1走進標籤JPanel的內部FlowLayout

  • 文件,報告和幫助文件JMenuItem s在JMenuBar上。

  • JPanel有一個BoxLayout與Y_AXIS方向。

  • 將標籤JPanelJScrollPane和按鈕JPanel添加到主JPanel

+0

試試@Gilbert Le Blanc。感謝您的輸入。非常感激。 – DaveQuinn 2013-03-12 14:39:22