2014-06-18 19 views
5

tôi đang cố gắng để tạo ra các giao diện sau:GridBagLayout gridding không làm việc

enter image description here

nhưng GUI mà tôi mắc phải là:

enter image description here

gì lưới của tôi trông giống như:

image: THE GRIDLAYOUT FOR THIS

Tôi không hiểu tại sao tôi nhận được kết quả này vì tôi đã vẽ một sơ đồ để hỗ trợ mã và dường như nó hoạt động.

Phương thức addComp bổ sung thêm thành phần đầu vào vào bảng nhập ở vị trí (x, y) nhất định và ở chiều rộng và chiều cao thành phần nhất định.

Code:

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

public class GUIError extends JFrame { 

    //initialise all the components 
    JPanel mainPanel = new JPanel(); 
    JTextField txtDisplay = new JTextField(); 
    JButton btnA = new JButton("A"); 
    JButton btnB = new JButton("B"); 
    JButton btnC = new JButton("C"); 
    JButton btnD = new JButton("D"); 
    JButton btnE = new JButton("E"); 
    JButton btnF = new JButton("F"); 
    JButton btnWA = new JButton("WA"); 
    JButton btnWB = new JButton("WB"); 
    JButton btnWC = new JButton("WC"); 
    JButton btnWD = new JButton("WD"); 

    private void addComp(JPanel panel, JComponent comp, int xPos, int yPos, int compWidth, int compHeight) { 

     GridBagConstraints gridConstraints = new GridBagConstraints(); 

     gridConstraints.gridx = xPos; 
     gridConstraints.gridy = yPos; 
     gridConstraints.gridwidth = compWidth; 
     gridConstraints.gridheight = compHeight; 
     gridConstraints.weightx = 0.5; 
     gridConstraints.weighty = 0.5; 
     gridConstraints.insets = new Insets(5, 5, 5, 5); 
     gridConstraints.anchor = GridBagConstraints.CENTER; 
     gridConstraints.fill = GridBagConstraints.BOTH; 

     panel.add(comp, gridConstraints); 

    } 


    public static void main(String[] args) { 
     try { 
      for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) { 
       if ("Nimbus".equals(info.getName())) { 
        UIManager.setLookAndFeel(info.getClassName()); 
        break; 
       } 
      } 
     } 
     catch (Exception e) { } 

     SwingUtilities.invokeLater(new Runnable() { 

      public void run() { 

       // create frame 
       JFrame frame = new JFrame("Calculator"); 
       frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
       frame.setLocationRelativeTo(null); 
       Container c = frame.getContentPane(); 

       // create GUI within frame    
       new GUIError(c); 

       // finish frame definition 
       frame.pack(); 
       frame.setResizable(false); 
       frame.setVisible(true); 

      } 

     }); 
    } 

    public GUIError(Container cont) { 

     cont.setPreferredSize(new Dimension(610, 250)); 

     // parent panel containes every other panel 
     mainPanel.setLayout(new GridBagLayout()); 

     // text display 
     txtDisplay.setEditable(false); 
     addComp(mainPanel, txtDisplay, 0, 0, 12, 2); // width 16, height 2 


     addComp(mainPanel, btnA, 0, 2, 2, 1); 
     addComp(mainPanel, btnB, 2, 2, 2, 1); 
     addComp(mainPanel, btnC, 4, 2, 2, 1); 
     addComp(mainPanel, btnD, 6, 2, 2, 1); 
     addComp(mainPanel, btnE, 8, 2, 2, 1); 
     addComp(mainPanel, btnF, 10, 2, 2, 1); 
     addComp(mainPanel, btnWA, 0, 3, 3, 1); 
     addComp(mainPanel, btnWB, 3, 3, 3, 1); 
     addComp(mainPanel, btnWC, 6, 3, 3, 1); 
     addComp(mainPanel, btnWD, 9, 3, 3, 1); 

     cont.add(mainPanel); 
    } 
} 
+1

GBC dựa trên cột, +1 cho câu hỏi và SSCCE/MCVE, lý do tại sao MigLayout (e.i.) được phát triển – mKorbel

+0

Đừng nghĩ rằng bạn có thể đạt được điều này chỉ với 1 bảng điều khiển. 'gridwidth' không thể chỉ định" nửa lưới "theo như tôi biết. Tôi nghĩ lựa chọn tốt nhất là sử dụng bố cục khác hoặc tách các thành phần của bạn thành các bảng khác nhau (bảng chính chứa một trường ở trên cùng và 2 ô bên dưới (cạnh nhau). Mỗi bảng con có 2 bảng: một bảng cho 3 các nút nhỏ hơn, một với 2 nút lớn hơn bên dưới) –

+0

Tôi chưa chỉ định một nửa lưới bằng cách sử dụng lưới thông rộng. Dòng đầu tiên của các nút là gridwidth = 2 và dòng thứ ba của các nút là gridwidth = 3. Tôi không hiểu ý bạn là gì bởi 3 nút nhỏ hơn và 2 lớn hơn ở phía dưới? Lưới mà tôi cũng đã liên kết hiển thị lưới mà tôi đã sử dụng trong tình huống này. Hàng thứ hai của các nút có cùng kích thước, hàng thứ ba của các nút có cùng kích thước. Tôi không hiểu tại sao điều này sẽ không hoạt động với GridBagLayout. – user3749872

Trả lời

5

Nó có thể dễ dàng đạt được bằng cách sử dụng GridBagLayout như đã thấy trong ví dụ này trình bày dưới đây. Bạn chỉ cần thực hiện hai số JPanel mỗi cái cho mỗi ROW. Cả hai sử dụng GridBagLayout, và thêm các thành phần vào hàng đầu tiên, với weightx = 0.16 vì mỗi JButton là giả sử để đưa khu vực này nhiều, và cho hàng thứ hai weightx = 0.25 từ đó, đây là khu vực bị chiếm đóng bởi mỗi JButton cùng hướng X

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

public class GridBagExample { 
    private static final int SIZE = 10; 
    private JButton[] buttons; 
    private GridBagConstraints gbc; 

    public GridBagExample() { 
     buttons = new JButton[SIZE]; 
     gbc = new GridBagConstraints(); 
     gbc.insets = new Insets(5, 5, 5, 5); 
    } 

    private void createAndDisplayGUI() { 
     JFrame frame = new JFrame("Grid Game"); 
     frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 

     JPanel contentPane = new JPanel(); 
     contentPane.setLayout(new GridLayout(2, 1, 5, 5)); 
     contentPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); 

     JPanel topPanel = new JPanel(); 
     JTextField textField = new JTextField(10); 
     //topPanel.add(textField); 

     JPanel buttonPanel = new JPanel(new GridLayout(2, 1, 5, 5)); 
     JPanel headerPanel = new JPanel(new GridBagLayout()); 
     buttons[0] = new JButton("A"); 
     addComp(headerPanel, buttons[0], 0, 0, 0.16, 1.0, 1, 1, GridBagConstraints.BOTH);   
     buttons[1] = new JButton("B"); 
     addComp(headerPanel, buttons[1], 1, 0, 0.16, 1.0, 1, 1, GridBagConstraints.BOTH); 
     buttons[2] = new JButton("C"); 
     addComp(headerPanel, buttons[2], 2, 0, 0.16, 1.0, 1, 1, GridBagConstraints.BOTH); 
     buttons[3] = new JButton("D"); 
     addComp(headerPanel, buttons[3], 3, 0, 0.16, 1.0, 1, 1, GridBagConstraints.BOTH); 
     buttons[4] = new JButton("E"); 
     addComp(headerPanel, buttons[4], 4, 0, 0.16, 1.0, 1, 1, GridBagConstraints.BOTH); 
     buttons[5] = new JButton("F"); 
     addComp(headerPanel, buttons[5], 5, 0, 0.16, 1.0, 1, 1, GridBagConstraints.BOTH); 

     JPanel footerPanel = new JPanel(new GridBagLayout()); 
     buttons[6] = new JButton("WA"); 
     addComp(footerPanel, buttons[6], 0, 0, 0.25, 1.0, 1, 1, GridBagConstraints.BOTH); 
     buttons[7] = new JButton("WB"); 
     addComp(footerPanel, buttons[7], 1, 0, 0.25, 1.0, 1, 1, GridBagConstraints.BOTH); 
     buttons[8] = new JButton("WC"); 
     addComp(footerPanel, buttons[8], 2, 0, 0.25, 1.0, 1, 1, GridBagConstraints.BOTH); 
     buttons[9] = new JButton("WD"); 
     addComp(footerPanel, buttons[9], 3, 0, 0.25, 1.0, 1, 1, GridBagConstraints.BOTH); 
     buttonPanel.add(headerPanel); 
     buttonPanel.add(footerPanel); 

     contentPane.add(textField); 
     contentPane.add(buttonPanel); 

     frame.setContentPane(contentPane); 
     frame.pack(); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 
    } 

    private void addComp(JPanel panel, JComponent comp, int x, int y, 
            double wx, double wy, int gw, int gh, int fill) { 
     gbc.gridx = x; 
     gbc.gridy = y; 
     gbc.weightx = wx; 
     gbc.weighty = wy; 
     gbc.gridwidth = gw; 
     gbc.gridheight = gh; 
     gbc.fill = fill; 

     panel.add(comp, gbc); 
    } 

    public static void main(String... args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       new GridBagExample().createAndDisplayGUI(); 
      } 
     }); 
    } 
} 

OUTPUT: GridBagLayout:

gridbagexample

Kết quả tương tự cũng có thể đạt được bằng GridLayout, nếu GridBagLayout không phải là một điều cần thiết trong trường hợp này, như được trích dẫn trong các ví dụ dưới đây:

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

public class GridBagExample { 
    private static final int SIZE = 10; 
    private JButton[] buttons; 

    public GridBagExample() { 
     buttons = new JButton[SIZE]; 
    } 

    private void createAndDisplayGUI() { 
     JFrame frame = new JFrame("Grid Game"); 
     frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 

     JPanel contentPane = new JPanel(); 
     contentPane.setLayout(new GridLayout(2, 1, 5, 5)); 
     contentPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); 

     JPanel topPanel = new JPanel(); 
     JTextArea tArea = new JTextArea(5, 30); 
     JScrollPane scroller = new JScrollPane(); 
     scroller.setViewportView(tArea); 
     topPanel.add(scroller); 

     JPanel buttonPanel = new JPanel(new GridLayout(2, 1, 5, 5)); 
     JPanel headerPanel = new JPanel(new GridLayout(1, 0, 5, 5)); 
     buttons[0] = new JButton("A"); 
     headerPanel.add(buttons[0]); 
     buttons[1] = new JButton("B"); 
     headerPanel.add(buttons[1]); 
     buttons[2] = new JButton("C"); 
     headerPanel.add(buttons[2]); 
     buttons[3] = new JButton("D"); 
     headerPanel.add(buttons[3]); 
     buttons[4] = new JButton("E"); 
     headerPanel.add(buttons[4]); 
     buttons[5] = new JButton("F"); 
     headerPanel.add(buttons[5]); 
     JPanel footerPanel = new JPanel(new GridLayout(1, 0, 5, 5)); 
     buttons[6] = new JButton("WA"); 
     footerPanel.add(buttons[6]); 
     buttons[7] = new JButton("WB"); 
     footerPanel.add(buttons[7]); 
     buttons[8] = new JButton("WC"); 
     footerPanel.add(buttons[8]); 
     buttons[9] = new JButton("WD"); 
     footerPanel.add(buttons[9]); 
     buttonPanel.add(headerPanel); 
     buttonPanel.add(footerPanel); 

     contentPane.add(topPanel); 
     contentPane.add(buttonPanel); 

     frame.setContentPane(contentPane); 
     frame.pack(); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 
    } 

    public static void main(String... args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       new GridBagExample().createAndDisplayGUI(); 
      } 
     }); 
    } 
} 

OUTPUT: GridLayout: GridBagExample

5

thường Tôi muốn tạo các bảng riêng biệt, nhưng hey, ai không thích thử thách?

Vấn đề là một số cột không được xác định rõ ràng cho GBL (không có thành phần nào xác định chiều rộng của nó), vì vậy nút WA không biết cách bao xa một nửa nút B, v.v.

Vì vậy, tôi đã thêm một số miếng đệm có chiều rộng bằng không trong các cột tạo nên các phần cần thiết.

Tất cả trong một bảng điều khiển:

enter image description here

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

public class GridBagDemo3 implements Runnable 
{ 
    private JPanel panel; 

    public static void main(String[] args) 
    { 
    SwingUtilities.invokeLater(new GridBagDemo3()); 
    } 

    public void run() 
    { 
    JTextArea text = new JTextArea(10,10); 
    JButton btnA = new JButton("A"); 
    JButton btnB = new JButton("B"); 
    JButton btnC = new JButton("C"); 
    JButton btnD = new JButton("D"); 
    JButton btnE = new JButton("E"); 
    JButton btnF = new JButton("F"); 
    JButton btnWA = new JButton("WA"); 
    JButton btnWB = new JButton("WB"); 
    JButton btnWC = new JButton("WC"); 
    JButton btnWD = new JButton("WD"); 

    panel = new JPanel(new GridBagLayout()); 

    add(text, 0,0, 12,1); 
    add(btnA, 0,1, 2,1); 
    add(btnB, 2,1, 2,1); 
    add(btnC, 4,1, 2,1); 
    add(btnD, 6,1, 2,1); 
    add(btnE, 8,1, 2,1); 
    add(btnF, 10,1, 2,1); 
    add(btnWA, 0,2, 3,1); 
    add(btnWB, 3,2, 3,1); 
    add(btnWC, 6,2, 3,1); 
    add(btnWD, 9,2, 3,1); 

    // SPACERS: define the 2 columns that button B spans 
    //   so that WA and WB can split B, and same for button E 
    add(null, 2,2, 1,1); 
    add(null, 3,2, 1,1); 
    add(null, 8,2, 1,1); 
    add(null, 9,2, 1,1); 

    JFrame frame = new JFrame("Grig Bag"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.getContentPane().add(panel); 
    frame.pack(); 
    frame.setVisible(true); 
    } 

    private void add(Component comp, int x, int y, int colspan, int rowspan) 
    { 
    GridBagConstraints gbc = new GridBagConstraints(); 
    gbc.gridx = x; 
    gbc.gridy = y; 
    gbc.gridwidth = colspan; 
    gbc.gridheight = rowspan; 
    gbc.weighty = .1; 
    gbc.anchor = GridBagConstraints.CENTER; 
    gbc.fill = GridBagConstraints.BOTH; 
    gbc.insets = new Insets(5,5,5,5); 

    if (comp != null) 
    { 
     gbc.weightx = 1; 
    } 
    else 
    { 
     comp = Box.createHorizontalGlue(); 
     gbc.fill = GridBagConstraints.NONE; 
     gbc.weightx = 0.1; 
     gbc.weighty = 0; 
    } 

    panel.add(comp, gbc); 
    } 
} 
3

nếu bạn vẫn muốn tìm hiểu lỗi của bạn: lưới' bố trí không phải là FlowLayout, bạn không thể chỉ cần thêm thành phần và nó sẽ vẽ đối tượng cùng một lúc chương trình đọc nó. Trong trường hợp lưới, máy ảo đầu tiên đọc tất cả các đối tượng và hơn lưới xây dựng, số lượng cột được xác định trong cơ sở của những hàng có số lượng cột lớn nhất. Vì vậy, bạn có ý tưởng tốt để thích 12 cột (do đó bạn tránh tạo ra "Panel" mới và tài nguyên hệ thống dự phòng), nhưng để làm điều đó đúng bạn chỉ cần có thêm 12 khoảng trống (không chỉ trống mà còn với kích thước bằng không) trong một trong những hàng

// text display 
    txtDisplay.setEditable(false); 
    addComp(mainPanel, txtDisplay, 0, 0, 12, 2); // width 16, height 2 



    //now go with empty spaces 
    for(int i=0;i<12;i++){ 
     GridBagConstraints gridConstraints = new GridBagConstraints(); 
     gridConstraints.gridx = i; //12 times "i" 
     gridConstraints.gridy = 1; //you left y=1 empty, so why don't use it 
     gridConstraints.gridwidth = 1; //one column = one space in grid 
     gridConstraints.gridheight = 0; //we don't want it to occupied any space 
     gridConstraints.weightx = 0.5; 
     gridConstraints.weighty = 0.5; 
     gridConstraints.insets = new Insets(0, 0, 0, 0); //same reason, we don't want it to occupied any space 
     gridConstraints.anchor = GridBagConstraints.CENTER; 
     gridConstraints.fill = GridBagConstraints.BOTH ; 

     mainPanel.add(Box.createHorizontalGlue(), gridConstraints); 
    } 


    addComp(mainPanel, btnA, 0, 2, 2, 1); 
    addComp(mainPanel, btnB, 2, 2, 2, 1); 
    addComp(mainPanel, btnC, 4, 2, 2, 1); 
    addComp(mainPanel, btnD, 6, 2, 2, 1); 
    addComp(mainPanel, btnE, 8, 2, 2, 1); 
    addComp(mainPanel, btnF, 10, 2, 2, 1); 
    addComp(mainPanel, btnWA, 0, 3, 3, 1); 
    addComp(mainPanel, btnWB, 3, 3, 3, 1); 
    addComp(mainPanel, btnWC, 6, 3, 3, 1); 
    addComp(mainPanel, btnWD, 9, 3, 3, 1); 
4

@Matthieu viết -

could you elaborate on GBC are column based? It sounds like an explanation of a problem I noticed...

tôi nghĩ rằng cách này là GridBagLayout thiết kế, tôi luôn sử dụng các JLabels (không có biên giới :-) như một ma trận .. .

enter image description here

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

public class CopyTextNorthPanel extends JPanel { 

    private static final long serialVersionUID = 1L; 
    private JLabel hidelLabel; 
    private JLabel firstLabel; 
    private JTextField firstText; 

    public CopyTextNorthPanel() { 

     setLayout(new GridBagLayout()); 
     GridBagConstraints gbc = new GridBagConstraints(); 
     setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT); 
     for (int k = 0; k < 50; k++) { 
      hidelLabel = new JLabel("  "); 
      hidelLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1)); 
      gbc.fill = GridBagConstraints.HORIZONTAL; 
      gbc.weightx = 0.5; 
      gbc.weighty = 0.5; 
      gbc.gridx = k; 
      gbc.gridy = 0; 
      add(hidelLabel, gbc); 
     } 
     for (int k = 0; k < 5; k++) { 
      firstLabel = new JLabel("Testing Label : ", SwingConstants.RIGHT); 
      firstLabel.setFont(new Font("Serif", Font.BOLD, 20)); 
      firstLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1)); 
      gbc.fill = GridBagConstraints.HORIZONTAL; 
      gbc.insets = new Insets(0, 0, 5, 0); 
      gbc.gridx = 0; 
      gbc.gridwidth = 8; 
      gbc.gridy = k + 1; 
      add(firstLabel, gbc); 
     } 
     for (int k = 0; k < 5; k++) { 
      firstText = new JTextField("Testing TextField"); 
      firstText.setFont(new Font("Serif", Font.BOLD, 20)); 
      firstText.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1)); 
      gbc.fill = GridBagConstraints.HORIZONTAL; 
      gbc.insets = new Insets(0, 0, 5, 0); 
      gbc.gridx = 9; 
      gbc.gridwidth = k + 8; 
      gbc.gridy = k + 1; 
      add(firstText, gbc); 
     } 
     for (int k = 0; k < 5; k++) { 
      firstLabel = new JLabel("Testing Label : ", SwingConstants.RIGHT); 
      firstLabel.setFont(new Font("Serif", Font.BOLD, 20)); 
      firstLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1)); 
      gbc.fill = GridBagConstraints.HORIZONTAL; 
      gbc.insets = new Insets(0, 0, 5, 0); 
      gbc.gridx = 20 + k; 
      gbc.gridwidth = 8; 
      gbc.gridy = k + 1; 
      add(firstLabel, gbc); 
     } 
     for (int k = 0; k < 5; k++) { 
      firstText = new JTextField("Testing TextField"); 
      firstText.setFont(new Font("Serif", Font.BOLD, 20)); 
      firstText.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1)); 
      gbc.fill = GridBagConstraints.HORIZONTAL; 
      gbc.insets = new Insets(0, 0, 5, 0); 
      gbc.gridx = 29 + k; 
      gbc.gridwidth = 21 - k; 
      gbc.gridy = k + 1; 
      add(firstText, gbc); 
     } 
    } 
} 
Các vấn đề liên quan