Tôi cố gắng để triển khai mô hình này sử dụng bố trí GRIDBAG trong javaĐiều chỉnh Gridbag bố trí
public static void addComponentsToPane(Container pane) {
if (RIGHT_TO_LEFT) {
pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
}
JLabel label1,label2,label3,result,title;
JButton calculate_btn;
JTextField side1,side2,side3;
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
if (shouldFill) {
//natural height, maximum width
c.fill = GridBagConstraints.HORIZONTAL;
}
title = new JLabel("Area of Triangle");
if (shouldWeightX) {
c.weightx = 0.5;
}
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 2;
c.gridy = -1;
pane.add(title, c);
label1 = new JLabel("Side 1: ");
if (shouldWeightX) {
c.weightx = 0.5;
}
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 20;
c.gridx = 1;
c.gridy = 1;
pane.add(label1, c);
label2 = new JLabel("Side 2: ");
if (shouldWeightX) {
c.weightx = 0.5;
}
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 20;
c.gridx = 1;
c.gridy = 2;
pane.add(label2, c);
label3 = new JLabel("Side 3: ");
if (shouldWeightX) {
c.weightx = 0.5;
}
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 20;
c.gridx = 1;
c.gridy = 3;
pane.add(label3, c);
side1 = new JTextField(" ");
if (shouldWeightX) {
c.weightx = 0.5;
}
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 20;
c.gridx = 2;
c.gridy = 1;
pane.add(side1, c);
side2 = new JTextField("Side 3: ");
if (shouldWeightX) {
c.weightx = 0.5;
}
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 20;
c.gridx = 2;
c.gridy = 2;
pane.add(side2, c);
side3 = new JTextField("Side 3: ");
if (shouldWeightX) {
c.weightx = 0.5;
}
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 20;
c.gridx = 2;
c.gridy = 3;
pane.add(side3, c);
calculate_btn = new JButton("Calculate");
//c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 30; //make this component tall
c.weightx = 0.5;
c.gridwidth = 3;
c.gridx = 0;
c.gridy = 5;
pane.add(calculate_btn, c);
result = new JLabel("Result displayed here");
if (shouldWeightX) {
c.weightx = 0.5;
}
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 20;
c.gridx = 2;
c.gridy = 7;
pane.add(result, c);
}
Vì vậy, các mã trên là về cơ bản chỉ là thành phần mà sẽ được thêm vào một giao diện, nhưng tôi không hoàn toàn nhận được những gì tôi muốn, đây là những gì tôi đang cố gắng để đạt được
Nhưng đây là những gì tôi nhận được với các mã trên
Vì vậy, khi tôi biên dịch trên là những gì tôi endup với, còn nếu có thể, tôi không muốn người dùng thay đổi kích thước cửa sổ, tôi đoán một số boolean với một trong các tính chất cửa sổ ..
'pane.setComponentOrientation (ComponentOrientation.RIGHT_TO_LEFT);' dòng mã này là bởi 'Darryl Burke' hoặc' camickr', đặc biệt là thứ hai của Swing Gurus đã đề cập một vài ví dụ về GBC trên diễn đàn này, – mKorbel
GBC chỉ hoạt động (chính xác) trong trường hợp tất cả các cột được điền (sử dụng JComponents vô hình), sau đó bạn có thể sử dụng bất kỳ cột nào cho bất kỳ hàng nào :-), – mKorbel
+1 có một đi trong tay đặt ra 'GridBagLayout' – MadProgrammer