java怎么 对齐_java – 如何在GridBagLayout单元格左右对齐?

当使用GridBagLayout进行JLabel:JTextField的表格显示时,我喜欢使用一种方法,使我的GridBagConstraints基于x,y位置。例如:

private GridBagConstraints createGbc(int x, int y) {

GridBagConstraints gbc = new GridBagConstraints();

gbc.gridx = x;

gbc.gridy = y;

gbc.gridwidth = 1;

gbc.gridheight = 1;

gbc.anchor = (x == 0) ? GridBagConstraints.WEST : GridBagConstraints.EAST;

gbc.fill = (x == 0) ? GridBagConstraints.BOTH

: GridBagConstraints.HORIZONTAL;

gbc.insets = (x == 0) ? WEST_INSETS : EAST_INSETS;

gbc.weightx = (x == 0) ? 0.1 : 1.0;

gbc.weighty = 1.0;

return gbc;

}

以下代码使GUI如下所示:

import java.awt.GridBagConstraints;

import java.awt.GridBagLayout;

import java.awt.Insets;

import java.util.HashMap;

import java.util.Map;

import javax.swing.*;

public class GridBagEg {

private static void createAndShowGui() {

PlayerEditorPanel playerEditorPane = new PlayerEditorPanel();

int result = JOptionPane.showConfirmDialog(null, playerEditorPane,

"Edit Player", JOptionPane.OK_CANCEL_OPTION,

JOptionPane.PLAIN_MESSAGE);

if (result == JOptionPane.OK_OPTION) {

// TODO: do something with info

for (PlayerEditorPanel.FieldTitle fieldTitle :

PlayerEditorPanel.FieldTitle.values()) {

System.out.printf("%10s: %s%n", fieldTitle.getTitle(),

playerEditorPane.getFieldText(fieldTitle));

}

}

}

public static void main(String[] args) {

SwingUtilities.invokeLater(new Runnable() {

public void run() {

createAndShowGui();

}

});

}

}

@SuppressWarnings("serial")

class PlayerEditorPanel extends JPanel {

enum FieldTitle {

NAME("Name"), SPEED("Speed"), STRENGTH("Strength");

private String title;

private FieldTitle(String title) {

this.title = title;

}

public String getTitle() {

return title;

}

};

private static final Insets WEST_INSETS = new Insets(5, 0, 5, 5);

private static final Insets EAST_INSETS = new Insets(5, 5, 5, 0);

private Map fieldMap = new HashMap();

public PlayerEditorPanel() {

setLayout(new GridBagLayout());

setBorder(BorderFactory.createCompoundBorder(

BorderFactory.createTitledBorder("Player Editor"),

BorderFactory.createEmptyBorder(5, 5, 5, 5)));

GridBagConstraints gbc;

for (int i = 0; i < FieldTitle.values().length; i++) {

FieldTitle fieldTitle = FieldTitle.values()[i];

gbc = createGbc(0, i);

add(new JLabel(fieldTitle.getTitle() + ":", JLabel.LEFT), gbc);

gbc = createGbc(1, i);

JTextField textField = new JTextField(10);

add(textField, gbc);

fieldMap.put(fieldTitle, textField);

}

}

private GridBagConstraints createGbc(int x, int y) {

GridBagConstraints gbc = new GridBagConstraints();

gbc.gridx = x;

gbc.gridy = y;

gbc.gridwidth = 1;

gbc.gridheight = 1;

gbc.anchor = (x == 0) ? GridBagConstraints.WEST : GridBagConstraints.EAST;

gbc.fill = (x == 0) ? GridBagConstraints.BOTH

: GridBagConstraints.HORIZONTAL;

gbc.insets = (x == 0) ? WEST_INSETS : EAST_INSETS;

gbc.weightx = (x == 0) ? 0.1 : 1.0;

gbc.weighty = 1.0;

return gbc;

}

public String getFieldText(FieldTitle fieldTitle) {

return fieldMap.get(fieldTitle).getText();

}

}

在这个例子中,我在JOptionPane中显示了JPanel,但它可以很容易地显示在JFrame或者JApplet或JDialog或…

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值