协助 Java 的网格包布局?
Assistance with Java's grid bag layout?
好吧,我正在尝试将其设置为 "enter your name" JLabel 紧挨着 JTextField 左侧显示的位置,遗憾的是它显示在中间。我说的是第一个JLabel和第一个JTextArea,其余代码请无视
import java.awt.*;
import javax.swing.*;
public class practice extends JFrame {
public static void main(String[] args){
new practice();
}
public practice(){
JPanel jp = new JPanel();
jp.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
jp.add(new JLabel(""),gbc);
gbc.gridx=0;
gbc.gridy=1;
jp.add(new JLabel("Enter your name\n"),gbc);
gbc.gridx=0;
gbc.gridy=0;
jp.add(new JLabel(" "),gbc);
gbc.gridx=0;
gbc.gridy=0;
jp.add(new JLabel(" "),gbc);
gbc.insets = new Insets(30,0,0,0);
gbc.gridx=0;
gbc.gridy=1;
jp.add(new JTextArea(3,30),gbc);
gbc.insets = new Insets(10,0,0,0);
gbc.gridx=0;
gbc.gridy=2;
jp.add(new JTextArea(3,10),gbc);
gbc.insets = new Insets(10,0,0,0);
gbc.gridx=0;
gbc.gridy=3;
jp.add(new JTextArea(3,10),gbc);
gbc.gridx=0;
gbc.gridy=0;
jp.add(new JRadioButton(""),gbc);
gbc.gridx=0;
gbc.gridy=0;
jp.add(new JRadioButton (""),gbc);
gbc.gridx=0;
gbc.gridy=0;
jp.add(new JRadioButton(""),gbc);
this.add(jp);
setTitle("Card");
setSize(700,500);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
}
gridx
和 gridy
定义对象将放置在网格的哪个单元格中。它本质上是一个坐标系……就像电子表格。
您在 (0,0) 处放置了许多组件,并且在您询问的特定实例中,您同时放置了 "enter your name" 标签和 JTextArea
(不同于JTextField
) 在 (0,1) 的同一单元格中。因此,它们被放置在彼此之上。
我建议为每个要放入 GUI 的 Swing 组件创建一个 GridBagConstraints。这样,您就不必记住任何默认值,您可以为每个组件指定每个参数。
这是我使用 GridBagLayout 的 JDialog。
这是 JDialog 的代码。
package com.ggl.sudoku.solver.view;
import java.awt.Component;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class SolutionDialog {
protected static final Insets buttonInsets = new Insets(10, 10, 0, 10);
private int singleCount;
private int guessCount;
private long elapsedTime;
private JDialog dialog;
private SudokuFrame frame;
public SolutionDialog(SudokuFrame frame, int singleCount, int guessCount,
long elapsedTime) {
this.frame = frame;
this.singleCount = singleCount;
this.guessCount = guessCount;
this.elapsedTime = elapsedTime;
createPartControl();
}
private void createPartControl() {
dialog = new JDialog(frame.getFrame(), "Statistics");
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
JPanel resultsPanel = new JPanel();
resultsPanel.setLayout(new GridBagLayout());
int gridy = 0;
JLabel singleCountLabel = new JLabel("Cells with one possible number:");
addComponent(resultsPanel, singleCountLabel, 0, gridy, 1, 1,
buttonInsets, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);
String s = singleCount + " cells";
JLabel singleCountString = new JLabel(s);
addComponent(resultsPanel, singleCountString, 1, gridy++, 1, 1,
buttonInsets, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);
JLabel guessCountLabel = new JLabel("Cells where the solver guessed:");
addComponent(resultsPanel, guessCountLabel, 0, gridy, 1, 1,
buttonInsets, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);
s = guessCount + " cells";
JLabel guessCountString = new JLabel(s);
addComponent(resultsPanel, guessCountString, 1, gridy++, 1, 1,
buttonInsets, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);
JLabel elapsedTimeLabel = new JLabel("Elapsed Time:");
addComponent(resultsPanel, elapsedTimeLabel, 0, gridy, 1, 1,
buttonInsets, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);
s = elapsedTime + " milliseconds";
JLabel elapsedTimeString = new JLabel(s);
addComponent(resultsPanel, elapsedTimeString, 1, gridy++, 1, 1,
buttonInsets, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);
mainPanel.add(resultsPanel);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());
JButton okButton = new JButton("OK");
okButton.setAlignmentX(JButton.RIGHT_ALIGNMENT);
okButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
dialog.setVisible(false);
dialog.dispose();
}
});
buttonPanel.add(okButton);
mainPanel.add(buttonPanel);
dialog.add(mainPanel);
dialog.pack();
dialog.setBounds(getBounds());
dialog.setVisible(true);
}
private void addComponent(Container container, Component component,
int gridx, int gridy, int gridwidth, int gridheight, Insets insets,
int anchor, int fill) {
GridBagConstraints gbc = new GridBagConstraints(gridx, gridy,
gridwidth, gridheight, 1.0D, 1.0D, anchor, fill, insets, 0, 0);
container.add(component, gbc);
}
protected Rectangle getBounds() {
Rectangle f = frame.getBounds();
Rectangle d = dialog.getBounds();
d.x = f.x + (f.width - d.width) / 2;
d.y = f.y + (f.height - d.height) / 2;
return d;
}
}
如果你想把它放在另一个旁边,你应该尝试这样做:
gbc.gridx = 0;
gbc.gridy = 0;
Container.add(//Your Label);
gbc.gridx = 1;
gbc.gridy = 0;
Container.add(//Your TextField);
另请阅读有关锚点的内容,因为我没有看到您使用过它们,您可以使用:
gbc.anchor = GridBagConstraints.LINE_START;
用于显示从线开始开始而不是从中心开始的对象。
最后我建议您创建一个方法来将对象添加到您的 window 以避免那一大堆代码。
这是我使用的一个非常好的 DEITEL'S 示例:
import java.awt.
public Yourclass{
JPanel Container;
GridBagLayout yourlayout;
GridBagConstraints gbc;
//Initialize them in the constructor.. Ex: Container = new JPanel();
}
private void addobjects(Component object, int gridx, int gridy, int width, int heigth){
gbc.gridx = gridx;
gbc.gridy = gridy;
gbc.gridwith = width;
gbc.gridheight = height;
yourlayout.setConstraints(object, gbc);
Container.add(object);
}
所以现在你只需要这样做:
addobjects(YourJLabel, 0, 0, 1, 1);
addobjects(YourTextField, 1, 0, 1, 1);
一定要给宽度和高度赋值,否则构造函数现在不会在何处添加对象。
并且还要确保在构造函数所在的同一 class 中声明该方法。
如果我有一些语法错误,抱歉,但我为脑海中有 Nopal 感到自豪。 Jajaja.
希望这个回答对您有所帮助。
好吧,我正在尝试将其设置为 "enter your name" JLabel 紧挨着 JTextField 左侧显示的位置,遗憾的是它显示在中间。我说的是第一个JLabel和第一个JTextArea,其余代码请无视
import java.awt.*;
import javax.swing.*;
public class practice extends JFrame {
public static void main(String[] args){
new practice();
}
public practice(){
JPanel jp = new JPanel();
jp.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
jp.add(new JLabel(""),gbc);
gbc.gridx=0;
gbc.gridy=1;
jp.add(new JLabel("Enter your name\n"),gbc);
gbc.gridx=0;
gbc.gridy=0;
jp.add(new JLabel(" "),gbc);
gbc.gridx=0;
gbc.gridy=0;
jp.add(new JLabel(" "),gbc);
gbc.insets = new Insets(30,0,0,0);
gbc.gridx=0;
gbc.gridy=1;
jp.add(new JTextArea(3,30),gbc);
gbc.insets = new Insets(10,0,0,0);
gbc.gridx=0;
gbc.gridy=2;
jp.add(new JTextArea(3,10),gbc);
gbc.insets = new Insets(10,0,0,0);
gbc.gridx=0;
gbc.gridy=3;
jp.add(new JTextArea(3,10),gbc);
gbc.gridx=0;
gbc.gridy=0;
jp.add(new JRadioButton(""),gbc);
gbc.gridx=0;
gbc.gridy=0;
jp.add(new JRadioButton (""),gbc);
gbc.gridx=0;
gbc.gridy=0;
jp.add(new JRadioButton(""),gbc);
this.add(jp);
setTitle("Card");
setSize(700,500);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
}
gridx
和 gridy
定义对象将放置在网格的哪个单元格中。它本质上是一个坐标系……就像电子表格。
您在 (0,0) 处放置了许多组件,并且在您询问的特定实例中,您同时放置了 "enter your name" 标签和 JTextArea
(不同于JTextField
) 在 (0,1) 的同一单元格中。因此,它们被放置在彼此之上。
我建议为每个要放入 GUI 的 Swing 组件创建一个 GridBagConstraints。这样,您就不必记住任何默认值,您可以为每个组件指定每个参数。
这是我使用 GridBagLayout 的 JDialog。
这是 JDialog 的代码。
package com.ggl.sudoku.solver.view;
import java.awt.Component;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class SolutionDialog {
protected static final Insets buttonInsets = new Insets(10, 10, 0, 10);
private int singleCount;
private int guessCount;
private long elapsedTime;
private JDialog dialog;
private SudokuFrame frame;
public SolutionDialog(SudokuFrame frame, int singleCount, int guessCount,
long elapsedTime) {
this.frame = frame;
this.singleCount = singleCount;
this.guessCount = guessCount;
this.elapsedTime = elapsedTime;
createPartControl();
}
private void createPartControl() {
dialog = new JDialog(frame.getFrame(), "Statistics");
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
JPanel resultsPanel = new JPanel();
resultsPanel.setLayout(new GridBagLayout());
int gridy = 0;
JLabel singleCountLabel = new JLabel("Cells with one possible number:");
addComponent(resultsPanel, singleCountLabel, 0, gridy, 1, 1,
buttonInsets, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);
String s = singleCount + " cells";
JLabel singleCountString = new JLabel(s);
addComponent(resultsPanel, singleCountString, 1, gridy++, 1, 1,
buttonInsets, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);
JLabel guessCountLabel = new JLabel("Cells where the solver guessed:");
addComponent(resultsPanel, guessCountLabel, 0, gridy, 1, 1,
buttonInsets, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);
s = guessCount + " cells";
JLabel guessCountString = new JLabel(s);
addComponent(resultsPanel, guessCountString, 1, gridy++, 1, 1,
buttonInsets, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);
JLabel elapsedTimeLabel = new JLabel("Elapsed Time:");
addComponent(resultsPanel, elapsedTimeLabel, 0, gridy, 1, 1,
buttonInsets, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);
s = elapsedTime + " milliseconds";
JLabel elapsedTimeString = new JLabel(s);
addComponent(resultsPanel, elapsedTimeString, 1, gridy++, 1, 1,
buttonInsets, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);
mainPanel.add(resultsPanel);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());
JButton okButton = new JButton("OK");
okButton.setAlignmentX(JButton.RIGHT_ALIGNMENT);
okButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
dialog.setVisible(false);
dialog.dispose();
}
});
buttonPanel.add(okButton);
mainPanel.add(buttonPanel);
dialog.add(mainPanel);
dialog.pack();
dialog.setBounds(getBounds());
dialog.setVisible(true);
}
private void addComponent(Container container, Component component,
int gridx, int gridy, int gridwidth, int gridheight, Insets insets,
int anchor, int fill) {
GridBagConstraints gbc = new GridBagConstraints(gridx, gridy,
gridwidth, gridheight, 1.0D, 1.0D, anchor, fill, insets, 0, 0);
container.add(component, gbc);
}
protected Rectangle getBounds() {
Rectangle f = frame.getBounds();
Rectangle d = dialog.getBounds();
d.x = f.x + (f.width - d.width) / 2;
d.y = f.y + (f.height - d.height) / 2;
return d;
}
}
如果你想把它放在另一个旁边,你应该尝试这样做:
gbc.gridx = 0;
gbc.gridy = 0;
Container.add(//Your Label);
gbc.gridx = 1;
gbc.gridy = 0;
Container.add(//Your TextField);
另请阅读有关锚点的内容,因为我没有看到您使用过它们,您可以使用:
gbc.anchor = GridBagConstraints.LINE_START;
用于显示从线开始开始而不是从中心开始的对象。
最后我建议您创建一个方法来将对象添加到您的 window 以避免那一大堆代码。 这是我使用的一个非常好的 DEITEL'S 示例:
import java.awt.
public Yourclass{
JPanel Container;
GridBagLayout yourlayout;
GridBagConstraints gbc;
//Initialize them in the constructor.. Ex: Container = new JPanel();
}
private void addobjects(Component object, int gridx, int gridy, int width, int heigth){
gbc.gridx = gridx;
gbc.gridy = gridy;
gbc.gridwith = width;
gbc.gridheight = height;
yourlayout.setConstraints(object, gbc);
Container.add(object);
}
所以现在你只需要这样做:
addobjects(YourJLabel, 0, 0, 1, 1);
addobjects(YourTextField, 1, 0, 1, 1);
一定要给宽度和高度赋值,否则构造函数现在不会在何处添加对象。 并且还要确保在构造函数所在的同一 class 中声明该方法。
如果我有一些语法错误,抱歉,但我为脑海中有 Nopal 感到自豪。 Jajaja.
希望这个回答对您有所帮助。