2016-07-01 24 views
6

Tôi muốn tạo GUI sau bằng Java Swing.Làm cách nào để tạo GUI sau trong Java Swing?

GUI I want to have

Vì tôi không đủ kinh nghiệm với Java Swing, tôi không chắc chắn làm thế nào để chính xác tái tạo GUI đó.

Tôi đã cố gắng sử dụng GridLayout mà trông như thế này:

GridLayout

Tôi đã thử LayoutManagers khác nhưng do thiếu kinh nghiệm của tôi, tôi không thể có được bất cứ điều gì, ngay cả từ xa giống như GUI Tôi muốn Hoàn thành.

Tôi có thể phải sử dụng GridBagLayout nhưng tôi đã thử nó và chỉ đơn giản là không thể làm được gì. Tôi không chắc chắn làm thế nào để sử dụng chính xác GridBagLayout, đặc biệt là vì có một phương sai của số lượng colums cần thiết (2, 2 và sau đó 3).

Đây là đoạn mã được sử dụng để tạo ra các giao diện thứ hai:

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

Tôi biết ơn đối với bất kỳ loại hỗ trợ!

+0

Bạn có thể sử dụng 'MigLayout'. Thật dễ dàng –

+1

Trình soạn thảo GUI của NetBeans IDE sẽ là cách tiếp cận của tôi. Như tất cả các mã đó không thực sự thú vị so với logic kinh doanh. Ngoài ra các tài sản và như vậy được trình bày để thử. –

+2

@ JoopEggen Tôi không hiểu những gì bạn đang cố gắng nói .... nó giống như khi có hai điều nhưng bạn tập trung vào một – taclight

Trả lời

5

Đây là mã của bạn chỉ với một số thay đổi nhỏ :)

 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

Thụt lề mã của bạn đúng cách. – user1803551

4

GridLayout kích thước tất cả các ô giống nhau, tức là bố cục bên ngoài của bạn với 3 hàng và 1 cột làm cho 3 ô có cùng kích thước.

Thay vào đó, sử dụng BorderLayout cho thùng chứa bên ngoài của bạn và thêm đầu, giữa và tấm thấp hơn với các ràng buộc BorderLayout.NORTH, BorderLayout.CENTER và BorderLayout.SOUTH tương ứng

7

Bạn có thể thử một cái gì đó như thế này:

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

Giải thích:

Trước hết tôi đã tạo chính JPanel với BorderLayout. JPanel này sẽ được chia theo chiều ngang, thành phần CENTRE sẽ là JPanel chứa khu vực văn bản và nhãn khác và thành phần SOUTH sẽ là JPanel chứa các nút.

Các JPanel có chứa các lĩnh vực văn bản được đưa ra một GridLayout để nó có thể dễ dàng chia theo chiều dọc, và cũng có thể được đưa ra một hgap của 10 thêm một số khoảng cách.

Bên trái và bên phải JPanels được đặt vào cả hai đều giống nhau. Họ có một số BorderLayout với số vgap để thêm khoảng trắng. Thành phần NORTHJLabel và thành phần CENTREJScrollPane có chứa JTextArea.

Cuối cùng, thành phần SOUTH của chính JPanel là một JPanel khác được cấp lại BorderLayout. Ba số JButton s được thêm với các thuộc tính WEST, CENTREEAST được phân bổ tương ứng.

Kết quả tổng thể trông giống như:

enter image description here

+0

Thêm các vùng văn bản của bạn vào các ô cuộn. – user1803551

+0

@ user1803551 cảm ơn, đã cập nhật – explv

Các vấn đề liên quan