我正在努力完成以下任务:
我一直在测试各种Swing布局以实现这一点,但到目前为止还没有运气。目前我正在按照教程尝试GridBagConstraints;这是现在的样子:
你可以看到它不起作用,我不知道为什么。
这是我目前的代码:
public class MessageFooter extends JPanel {
private static final int BORDER_WIDTH = 5;
private final JLabel _recentMessage = new JLabel();
private int _footerHeight;
/*package*/ MessageFooter() {
// Set the size of the footer
_footerHeight = FlowFramePrefs.getUserPrefFontSize() + (BORDER_WIDTH * 2);
this.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
this.setMaximumSize(new java.awt.Dimension(java.lang.Integer.MAX_VALUE, _footerHeight));
this.setMinimumSize(new java.awt.Dimension(0, _footerHeight));
this.setPreferredSize(new Dimension(_footerHeight, _footerHeight));
this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
// JLabel
JPanel labelPanel = new JPanel();
_recentMessage.setText(" Blahhhhh");
labelPanel.add(_recentMessage);//, BorderLayout.CENTER);
labelPanel.setBorder(new BevelBorder(BevelBorder.LOWERED));
//_recentMessage.setAlignmentX(Component.LEFT_ALIGNMENT);
// Buttons
JPanel buttonPanel = new JPanel();
buttonPanel.setBorder(new EmptyBorder(0, 0, 0, 0));
//buttonPanel.setAlignmentX(Component.RIGHT_ALIGNMENT);
// Create the buttons
JButton upprB = new JButton("U");
upprB.setMargin(new Insets(0, 0, 0, 0));
upprB.setBorder(new EmptyBorder(0, 0, 0, 0));
JButton downB = new JButton("D");
downB.setMargin(new Insets(0, 0, 0, 0));
downB.setBorder(new EmptyBorder(0, 0, 0, 0));
JButton openB = new JButton("O");
openB.setMargin(new Insets(0, 0, 0, 0));
openB.setBorder(new EmptyBorder(0, 0, 0, 0));
buttonPanel.add(upprB);
buttonPanel.add(downB);
buttonPanel.add(openB);
// Add both
c.weightx = 1.0;
c.gridx = 0;
c.gridy = 0;
this.add(labelPanel, c);
c.weightx = 0.0;
c.gridx = 1;
c.gridy = 0;
this.add(buttonPanel, c);
}
...
}
答案 0 :(得分:3)
"页脚"面板应为for i in a:
i.replace('\t',' ')
print i
。
然后你创建一个"按钮"面板并将按钮添加到该面板。然后,此面板将添加到" BorderLayout.LINE_END"页脚面板。这意味着按钮将全部以其首选尺寸显示。
然后将标签添加到" BorderLayout.CENTER"页脚面板。标签现在将占用所有可用的剩余空间。
类似的东西:
BorderLayout
答案 1 :(得分:2)
基于@camickr的回答,我创建了一个例子:
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;
public class MessageFooter extends JFrame {
public MessageFooter() {
setLayout(new BorderLayout());
JPanel footer = new JPanel();
footer.setLayout(new BorderLayout());
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(1,3));
JPanel labelPanel = new JPanel();
labelPanel.setBorder(new BevelBorder(BevelBorder.LOWERED));
JButton u = new JButton("U");
u.setMargin(new Insets(0,0,0,0));
JButton d = new JButton("D");
d.setMargin(new Insets(0,0,0,0));
JButton o = new JButton("O");
o.setMargin(new Insets(0,0,0,0));
JLabel label = new JLabel("Sample Text");
buttonPanel.add(u);
buttonPanel.add(d);
buttonPanel.add(o);
labelPanel.add(label);
footer.add(labelPanel, BorderLayout.CENTER);
footer.add(buttonPanel, BorderLayout.LINE_END);
add(footer, BorderLayout.SOUTH);
setSize(500,500);
setVisible(true);
}
public static void main(String[] args) {
new MessageFooter();
}
}
答案 2 :(得分:-1)
Try using absolute layout i.e this.setLayout(null)
now you can give specified amount space to each of the components you are using.
Supposing that the MessageFooter
is of Dimensions - 1000(width), 50(height)
Now that the layout of MessageFooter
is null, we can set the positions of the buttons or as well as the buttonPanel
this.setLayout(null);
this.setSize(new Dimension(1000,50));
labelPanel.setBounds(0,0,800,50);
buttonPanel.setBounds(800,0,200,50);
for further details you can go through this documentation