2013-06-14 51 views
7

Khi tôi cố gắng căn chỉnh thành phần của mình, nó sẽ sang bên trái hoặc bên phải.Làm cách nào để căn chỉnh các thành phần trung tâm trong JPanel bằng GridBagLayout?

Vì vậy, tôi chỉ muốn giải pháp để loại bỏ vấn đề này và cũng cho tôi biết cách đặt kích thước của bảng điều khiển là 400 x 350 pixel.

enter image description here

Đây là mã của tôi .... titleLabelResultLabel nên được sắp xếp ở trung tâm

public TimeGui() { 

    layout = new GridBagLayout(); 
    setSize(400, 350); //**Its not working** 
    setBackground(Color.LIGHT_GRAY); 
    setBorder(BorderFactory.createLineBorder(Color.BLACK)); 
    setBorder(new TitledBorder(new EtchedBorder(), "Time Conversion")); 

    setLayout(layout); 
    layoutConstraints = new GridBagConstraints();  
    textField1 = new JTextField(10); 
    textField2 = new JTextField(10); 

    String[] names1 = {"Seconds", "Minutes", "Hours", "Days", "Weeks"}; 


    comboBox1 = new JComboBox<>(names1); 
    comboBox2 = new JComboBox<>(names1); 

    titleLabel = new JLabel("Time Conversion Unit", JLabel.CENTER); 
    resultLabel = new JLabel("Result Label"); 
    equalLabel = new JLabel("="); 

    convertButton = new JButton("Convert"); 


    layoutConstraints.fill = GridBagConstraints.HORIZONTAL; 
    Insets inset = new Insets(10, 10, 10, 10); 
    layoutConstraints.anchor = GridBagConstraints.CENTER; 

    addComponent(titleLabel, 0, 0, 2, 2, inset); // I tried (0,1,2,2) 



    addComponent(comboBox1, 3, 0, 2, 3, inset); 

    addComponent(comboBox2, 3, 2, 2, 3, inset); 

    addComponent(textField1, 6, 0, 1, 2, inset); 

    addComponent(equalLabel, 6, 1, 1, 2, inset); 

    addComponent(textField2, 6, 2, 1, 2, inset); 

    addComponent(resultLabel, 8, 1, 2, 1, inset); 

    addComponent(convertButton, 10, 0, 2, 2, inset); 

} 

private void addComponent(Component component, int row, 
     int column, int width, int height, Insets inset1) { 
    layoutConstraints.gridx = column; 
    layoutConstraints.gridy = row; 
    layoutConstraints.gridwidth = width; 
    layoutConstraints.gridheight = height; 
    layoutConstraints.insets = inset1; 
    layout.setConstraints(component, layoutConstraints); 
    add(component); 
} 
} 
+0

Trong trường hợp không có hình ảnh, nó sẽ là khôn ngoan, nếu ít nhất bạn có thể cung cấp một [___SSCCE___] (http://sscce.org/), để người ta có thể atleast chạy nó mà không có bất kỳ loại bổ sung. Chỉ cần tải hình ảnh của bạn lên bất kỳ trang web nào, như [** imgur **] (http://imgur.com/), [** dropbox **] (https://www.dropbox.com/home) hoặc [* * 4shared **] (http://www.4shared.com/). Bất kỳ ai có đặc quyền cao hơn sẽ thêm điều đó vào câu hỏi của bạn –

+0

ok chỉ cần cho tôi biết cách đặt kích thước của bảng điều khiển. – user1874936

Trả lời

4

Vấn đề là với gridwidthfill tài sản của bạn của bạn ...

enter image description hereenter image description here

Về cơ bản tất cả những gì tôi đã thay đổi là ...

addComponent(titleLabel, 0, 0, GridBagConstraints.REMAINDER, 2, inset); // I tried (0,1,2,2) 
addComponent(comboBox1, 3, 0, 1, 3, inset); 
addComponent(comboBox2, 3, 2, 1, 3, inset); 
addComponent(textField1, 6, 0, 1, 2, inset); 
addComponent(equalLabel, 6, 1, 1, 2, inset); 
addComponent(textField2, 6, 2, 1, 2, inset); 
layoutConstraints.fill = GridBagConstraints.NONE; 
addComponent(resultLabel, 8, 0, GridBagConstraints.REMAINDER, 1, inset); 
addComponent(convertButton, 10, 0, GridBagConstraints.REMAINDER, 2, inset); 

Bạn có thể chơi chung với một vài người khác.

Như để xác định kích thước thực tế của bảng điều khiển, là tốt nhất bạn có thể làm là ghi đè lên các phương pháp getPreferredSize của TimeGui ...

@Override 
public Dimension getPreferredSize() { 
    return new Dimension(400, 350); 
} 

Mà sẽ "gợi ý" để container cha mẹ những gì kích thước bạn muốn được đặt ra. Chỉ cần nhớ, đây là một giá trị tùy chọn và quản lý bố cục cũng nằm trong đó quyền bỏ qua nó.

+0

Great ... thanx rất nhiều – user1874936

+0

Làm thế nào để increse kích thước thành phần. Vì vậy, nó trông rõ ràng hơn – user1874936

+0

Điều đó phụ thuộc vào trình quản lý bố cục bạn đang sử dụng để hiển thị nó. Bắt đầu bằng cách ghi đè 'getPreferredSize' như được đề cập ở dưới cùng của câu trả lời của tôi – MadProgrammer

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