2016-07-01 61 views
6

我想用Java Swing創建以下GUI。如何在Java Swing中創建以下GUI?

GUI I want to have

因爲我不跟的Java Swing足夠的經驗,我不知道如何準確地重新創建GUI。

我使用網格佈局,看起來像這樣嘗試:

GridLayout

我試過其他佈局管理,但由於缺乏經驗,我,我不能得到任何東西,甚至遠程類似我想要的GUI實現。

我可能不得不使用GridBagLayout,但我已經嘗試過了,根本無法完成任何事情。 我不確定如何正確使用GridBagLayout,特別是因爲所需的柱子數量有差異(2,2和3)。

下面是用於創建第二GUI代碼:

import java.awt.*; 
import javax.swing.*; 

public class GUITest extends JFrame { 

public GUITest() { 
    super("Testing Title"); 
    Container pane = getContentPane(); 

    pane.setLayout(new GridLayout(3,1)); 

    pane.add(getHeader()); 
    pane.add(getTextArea()); 
    pane.add(getButtonPanel()); 

} 

public JComponent getHeader() { 
    JPanel labelPanel = new JPanel(); 
    labelPanel.setLayout(new GridLayout(1,2)); 
    labelPanel.setSize(getPreferredSize()); 

    JLabel labelLocal = new JLabel("Left value: ", JLabel.CENTER); 
    JLabel labelDB = new JLabel("Right value: ", JLabel.CENTER); 

    labelPanel.add(labelLocal); 
    labelPanel.add(labelDB); 

    return labelPanel; 
} 

public JComponent getTextArea() { 
    JPanel textPanel = new JPanel(); 
    textPanel.setLayout(new GridLayout(1,2,5,0)); 

    JTextArea testTextArea = new JTextArea(); 
    testTextArea.setEditable(false); 
    JScrollPane sp1 = new JScrollPane(testTextArea); 

    JTextArea testTextArea2 = new JTextArea(); 
    JScrollPane sp2 = new JScrollPane(testTextArea2); 
    testTextArea2.setEditable(false); 

    testTextArea.setText("Hello Hello Hello\nTesting!\ntesterino\ntesteroni"); 
    testTextArea2.setText("Hello Hello Hello\nTesting!\ntest\nABC123\ncdef123\nhijk123"); 

    textPanel.add(sp1); 
    textPanel.add(sp2); 
    return textPanel; 
} 

public JComponent getButtonPanel() { 
    JPanel inner = new JPanel(); 
    inner.setLayout(new FlowLayout((FlowLayout.CENTER),0,100)); 
    inner.add(new JButton("Do something")); 
    inner.add(new JButton("Do something different")); 
    inner.add(new JButton("Do something even more different")); 
    return inner; 
} 

public static void main(String[] args) { 
    GUITest e = new GUITest(); 
    e.setSize(700, 500); 
    e.setVisible(true); 
    e.setResizable(false); 
    e.setDefaultCloseOperation(EXIT_ON_CLOSE); 
    e.setLocationRelativeTo(null); 
} 
} 

我很感激任何形式的支持!

+0

你可以使用'MigLayout'。這很容易 –

+1

NetBeans IDE的GUI編輯器是我的方法。因爲與業務邏輯相比,所有代碼都不是很有趣。此外,屬性和這樣的嘗試。 –

+2

@JoopEggen我不明白你想說什麼....這就像有兩件事情,但你專注於一個 – taclight

回答

5

這裏是你的代碼只是一些小的變化:)

 import java.awt.*; 
    import javax.swing.*; 

    public class GUITest extends JFrame { 

     public GUITest() { 

       super("Testing Title"); 
       Container pane = getContentPane(); 
       pane.setLayout(new BorderLayout());//Modified Layout to BorderLayout 
       pane.add(getHeader(),BorderLayout.NORTH); //BorderLayout.NORTH 
       pane.add(getTextArea(),BorderLayout.CENTER);//BorderLayout.CENTER 
       pane.add(getButtonPanel(),BorderLayout.SOUTH);//BorderLayout.SOUTH 

     } 

     public JComponent getHeader() { 

      JPanel labelPanel = new JPanel(); 
      labelPanel.setLayout(new GridLayout(1,2)); 
      labelPanel.setSize(getPreferredSize());  
      JLabel labelLocal = new JLabel("Left value: ", JLabel.CENTER); 
      JLabel labelDB = new JLabel("Right value: ", JLabel.CENTER); 
      labelPanel.add(labelLocal); 
      labelPanel.add(labelDB); 
      return labelPanel; 

     } 

    public JComponent getTextArea() { 

      JPanel textPanel = new JPanel();  
      textPanel.setLayout(new GridLayout(1,2,5,0)); 
      JTextArea testTextArea = new JTextArea(); 
      testTextArea.setEditable(false); 
      JScrollPane sp1 = new JScrollPane(testTextArea); 
      JTextArea testTextArea2 = new JTextArea(); 
      JScrollPane sp2 = new JScrollPane(testTextArea2); 
      testTextArea2.setEditable(false); 
      testTextArea.setText("Hello Hello Hello\nTesting!\ntesterino\ntesteroni"); 
      testTextArea2.setText("Hello Hello Hello\nTesting!\ntest\nABC123\ncdef123\nhijk123"); 
      textPanel.add(sp1); 
      textPanel.add(sp2); 
      return textPanel; 
    } 

    public JComponent getButtonPanel() { 

      JPanel inner = new JPanel(); 
      inner.setLayout(new FlowLayout());//Modified to standard FlowLayout 
      inner.add(new JButton("Do something")); 
      inner.add(new JButton("Do something different")); 
      inner.add(new JButton("Do something even more different")); 
      return inner; 

    } 

    public static void main(String[] args) { 

      GUITest e = new GUITest(); 
      e.pack(); //Modified setSize(700,500) to pack() 
      e.setVisible(true); 
      e.setResizable(false); 
      e.setDefaultCloseOperation(EXIT_ON_CLOSE); 
      e.setLocationRelativeTo(null); 
    } 
} 
+1

正確縮進您的代碼。 – user1803551

4

GridLayout將所有單元格大小設置爲相同,即,具有3行和1列的外部佈局可製作3個大小相同的單元格。

而是使用BorderLayout的你外容器內,並限制BorderLayout.NORTH,BorderLayout.CENTER和BorderLayout.SOUTH加頂,中,下圖分別

7

你可以嘗試這樣的事:

import javax.swing.*; 
import javax.swing.border.EmptyBorder; 
import java.awt.*; 

public class Example { 

    public static void main(String[] args) { 

     JFrame jFrame = new JFrame(); 
     jFrame.setTitle("Testing Title"); 
     jFrame.setLocationRelativeTo(null); 

     JPanel mainPanel = new JPanel(new BorderLayout()); 
     mainPanel.setBorder(new EmptyBorder(5, 5, 5, 5)); 

     JPanel listPanel = new JPanel(new GridLayout(0, 2, 10, 0)); 

     JPanel leftListPanel = new JPanel(new BorderLayout(0, 10)); 
     JLabel leftLabel = new JLabel("Left value:"); 
     JTextArea leftTextArea = new JTextArea("Hello Hello Hello\nTesting!\ntest"); 
     JScrollPane leftScrollPane = new JScrollPane(leftTextArea); 
     leftListPanel.add(leftLabel, BorderLayout.NORTH); 
     leftListPanel.add(leftScrollPane, BorderLayout.CENTER); 

     JPanel rightListPanel = new JPanel(new BorderLayout(0, 10)); 
     JLabel rightLabel = new JLabel("Right value:"); 
     JTextArea rightTextArea = new JTextArea("Hello Hello Hello\nTesting!\ntest"); 
     JScrollPane rightScrollPane = new JScrollPane(rightTextArea); 
     rightListPanel.add(rightLabel, BorderLayout.NORTH); 
     rightListPanel.add(rightScrollPane, BorderLayout.CENTER); 

     listPanel.add(leftListPanel); 
     listPanel.add(rightListPanel); 
     mainPanel.add(listPanel, BorderLayout.CENTER); 

     JPanel buttonsPanel = new JPanel(new BorderLayout()); 
     buttonsPanel.setBorder(new EmptyBorder(10, 10, 10, 10)); 
     buttonsPanel.add(new JButton("Do something"), BorderLayout.WEST); 
     buttonsPanel.add(new JButton("Do something different"), BorderLayout.CENTER); 
     buttonsPanel.add(new JButton("Do something even more different"), BorderLayout.EAST); 
     mainPanel.add(buttonsPanel, BorderLayout.SOUTH); 

     jFrame.setContentPane(mainPanel); 
     jFrame.pack(); 
     jFrame.setVisible(true); 
    } 
} 

說明:

首先我創建了一個主JPanelBorderLayout。這JPanel將水平分割,CENTRE組件將是另一個JPanel包含文本區域和標籤,並且SOUTH組件將是JPanel包含按鈕。

包含文本區域的JPanel被賦予了GridLayout,以便它可以很容易地垂直分割,並且也給出了一個10hgap添加一些間距。

放在那裏的左側和右側JPanels都是一樣的。他們有一個BorderLayoutvgap增加間距。 NORTH組件是JLabel,而CENTRE組件是JScrollPane,其中包含JTextArea

最後,主JPanelSOUTH組件是另一個JPanel,其再次被給予BorderLayout。三個JButton s分別加上WEST,CENTREEAST相應的屬性。

總的結果是這樣的:

enter image description here

+0

將您的文本區域添加到滾動窗格。 – user1803551

+0

@ user1803551謝謝,更新 – explv