[英]Align to right with GridBagLayout
I'm creating an easy GUI using GridBagLayout and almost everything goes right, but there is this detail: having two columns, the left one for labels and the right one for other widgets like JTextArea, I want the first column to be aligned to right. 我正在使用GridBagLayout创建一个简单的GUI,几乎一切顺利,但有这个细节:有两列,左边一个用于标签,另一个用于其他小部件,如JTextArea,我希望第一列对齐到右边。
So I tried with anchor atribute set to GridBagConstraints.NORTHEAST or .EAST, and none of them work. 所以我尝试使用锚点属性设置为GridBagConstraints.NORTHEAST或.EAST,并且它们都不起作用。
My code is the following: 我的代码如下:
private void createDataPanel() {
data = new JPanel();
data.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
data.setBorder(new TitledBorder("NOTAM proposal parameters"));
c.gridx = 0;
c.gridy = 0;
c.insets = new Insets(5, 10, 5, 10);
c.fill = GridBagConstraints.HORIZONTAL;
data.add(new JLabel("Start time (DD-MM-YYYY HH:MM:SS):"), c);
c.gridx++;
long today = System.currentTimeMillis();
startTimePicker = new JXDatePicker(new Date(today));
startTimePicker.setFormats(dateFormatter);
data.add(startTimePicker);
c.gridx = 0;
c.gridy++;
c.anchor = GridBagConstraints.EAST;
data.add(new JLabel("End time (DD-MM-YYYY HH:MM:SS):"), c);
c.gridx++;
c.anchor = GridBagConstraints.CENTER;
long tomorrow = today + 86400000;
endTimePicker = new JXDatePicker(new Date(tomorrow));
endTimePicker.setFormats(dateFormatter);
data.add(endTimePicker, c);
c.gridx = 0;
c.gridy++;
c.anchor = GridBagConstraints.NORTHEAST;
JPanel itemEPanel = new JPanel();
itemEPanel.setLayout(new BoxLayout(itemEPanel, BoxLayout.Y_AXIS));
itemEPanel.add(new JLabel("Item E:"));
remainingChars = new JLabel(Integer.toString(availableChars) + " characters remaining");
itemEPanel.add(remainingChars);
data.add(itemEPanel, c);
c.gridx++;
c.anchor = GridBagConstraints.CENTER;
itemEText = new JTextArea(8, 1);
JScrollPane itemEScroll = new JScrollPane(itemEText);
itemEText.setLineWrap(true);
itemEText.setWrapStyleWord(true);
itemEText.setBackground(Color.WHITE);
itemEText.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void removeUpdate(DocumentEvent e) {
updateRemainingChars(e.getDocument());
}
@Override
public void insertUpdate(DocumentEvent e) {
updateRemainingChars(e.getDocument());
}
@Override
public void changedUpdate(DocumentEvent e) {
updateRemainingChars(e.getDocument());
}
public void updateRemainingChars(Document doc) {
availableChars = maxItemECharacters - doc.getLength();
if(availableChars>=0){
remainingChars.setText(Integer.toString(availableChars) + " characters remaining");
remainingChars.setForeground(Color.BLACK);
if(!okButton.isEnabled()) okButton.setEnabled(true);
}
else{
remainingChars.setText("Message too long: " + Integer.toString(availableChars));
remainingChars.setForeground(Color.RED);
if(okButton.isEnabled()) okButton.setEnabled(false);
}
}
});
data.add(itemEScroll, c);
c.gridx = 0;
c.gridy++;
JPanel itemDPanel = new JPanel(new FlowLayout());
applicable = new JCheckBox("Applicable");
applicable.setSelected(true);
applicable.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
itemDText.setEnabled(applicable.isSelected());
}
});
itemDPanel.add(applicable);
itemDPanel.add(new JLabel("Item D:"));
data.add(itemDPanel, c);
c.gridx++;
itemDText = new JTextField();
data.add(itemDText, c);
}
Nevertheless, it currently looks like this: 不过,它目前看起来像这样:
How can it align to right then? 它怎么能正确对齐呢?
设置GridBagConstraints.NORTHEAST或.EAST但使用fill = NONE
Do you mean you want your JLabel
's text to be on the RIGHT side. 你的意思是你希望你的JLabel
文本在正确的一面。 If yes try to initialize your JLabel
s like this 如果是,请尝试像这样初始化您的JLabel
JLabel someLabel = new JLabel("My Text will align towards RIGHT", JLabel.RIGHT);
JLabel.setHorizontalTextPosition(JLabel.RIGHT);
The problem is that you use this constraint: 问题是你使用这个约束:
c.fill = GridBagConstraints.HORIZONTAL;
therefore all components are stretched horizontally, leaving the alignement to them instead of the GridBagLayout. 因此,所有组件都是水平拉伸,而是将对齐而不是GridBagLayout。
in my case, it's 在我的情况下,它是
constraints.anchor = GridBagConstraints.NORTHEAST;
rather than .fill
attribute 而不是.fill
属性
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.