Я пытаюсь написать то, что, по моему мнению, должно быть простым в задании диалоговым окном, у меня есть несколько меток и несколько текстовых областей напротив них, а затем кнопка ОК и отмена.В середине у меня есть широкий компонент (например, я использовал метку)
|label| [combo box]
|label| [txtfield]
|label| [txtfield]
| long label here |
[btn1] [btn2]
Я пытаюсь использовать GridBagLayout, он не делает то, что я хочу, и я не уверен, что понимаю, почему,По сути, я хочу, чтобы кнопки были прикреплены к нижней части диалогового окна.
Код ниже показывает, как я его использую:
package gui;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
public class GridBagLayoutExample2 extends JFrame {
private static final long serialVersionUID = -1972347726217162551L;
final private JLabel lbl1 = new JLabel("LABEL1: ");
final private JLabel lbl2 = new JLabel("LABEL2: ");
final private JLabel lbl3 = new JLabel("LABEL3: ");
final private JTextArea txt1 = new JTextArea(" ");
final private JComboBox cmb1 = new JComboBox();
final private JTextArea txt2 = new JTextArea("");
final private JLabel lblLine = new JLabel("a compenent on all the line");
final private JButton btnOK = new JButton("OK");
final private JButton btnCancel = new JButton("Cancel");
public GridBagLayoutExample2() {
GridBagLayout bl = new GridBagLayout();
Container pane = getContentPane();
pane.setLayout(bl);
GridBagConstraints c = new GridBagConstraints();
int r = 0;
placeComponentInGridBagLayout(lbl1, pane, bl, c, 0, r, null, null, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, null, null, null, null);
placeComponentInGridBagLayout(cmb1, pane, bl, c, 1, r++, null, null, GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL, null, null, null, null);
placeComponentInGridBagLayout(lbl2, pane, bl, c, 0, r, null, null, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, null, null, null, null);
placeComponentInGridBagLayout(txt2, pane, bl, c, 1, r++, null, null, GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL, null, null, null, null);
placeComponentInGridBagLayout(lbl3, pane, bl, c, 0, r, null, null, GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL, null, null, null, null);
placeComponentInGridBagLayout(txt1, pane, bl, c, 1, r++, null, null, GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL, null, null, null, null);
placeComponentInGridBagLayout(lblLine, pane, bl, c, 0, r++, null, null, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, 2, null, null, null);
placeComponentInGridBagLayout(btnOK, pane, bl, c, 0, null, null, null, GridBagConstraints.PAGE_END, GridBagConstraints.HORIZONTAL, null, null, null, null);
placeComponentInGridBagLayout(btnCancel, pane, bl, c, 1, null, null, null, GridBagConstraints.PAGE_END, GridBagConstraints.HORIZONTAL, null, null, null, null);
setSize(new Dimension(850, 350));
pack();
setVisible(true);
}
public static void placeComponentInGridBagLayout(Component component, Container container, GridBagLayout bagLayout, GridBagConstraints c, Integer gridX, Integer gridY, Double weightX, Double weightY, Integer anchor, Integer fill, Integer ipadx, Integer ipady,
Integer gridWidth, Integer gridHeight) {
if (c == null) {
c = new GridBagConstraints();
}
if (gridX != null) {
c.gridx = gridX;
}
if (gridY != null) {
c.gridy = gridY;
}
if (weightX != null) {
c.weightx = weightX;
}
if (weightY != null) {
c.weighty = weightY;
}
if (fill != null) {
c.fill = fill;
}
if (anchor != null) {
c.anchor = anchor;
}
if (ipadx != null) {
c.ipadx = ipadx;
}
if (ipady != null) {
c.ipady = ipady;
}
if (gridWidth != null) {
c.gridwidth = gridWidth;
}
if (gridHeight != null) {
c.gridheight = gridHeight;
}
bagLayout.setConstraints(component, c);
container.add(component);
}
}
Есть идеи, что я делаю неправильно?Также есть ли более современный способ достижения того же самого в Swing?
спасибо