我创建了一个JToolbar组件并将其添加到Frame中。 工具栏使用BorderLayout。
我向工具栏添加了三个按钮,它们显示得非常好,但我希望它们被添加到工具栏的右侧。 右对齐。
然后,每当我向工具栏添加其他按钮时,我希望它们添加到左侧。
如何做到这一点?
我尝试了以下方法,但结果是按钮彼此上下堆叠:右侧的三个按钮都在彼此上方,而左侧的两个按钮也在彼此上方。
我向工具栏添加了三个按钮,它们显示得非常好,但我希望它们被添加到工具栏的右侧。 右对齐。
然后,每当我向工具栏添加其他按钮时,我希望它们添加到左侧。
如何做到这一点?
我尝试了以下方法,但结果是按钮彼此上下堆叠:右侧的三个按钮都在彼此上方,而左侧的两个按钮也在彼此上方。
public class Toolbar extends JToolBar {
private JToggleButton Screenshot = null;
private JToggleButton UserKeyInput = null;
private JToggleButton UserMouseInput = null;
private CardPanel cardPanel = null;
public Toolbar() {
setFloatable(false);
setRollover(true);
setLayout(new BorderLayout());
//I want to add these three to the right side of my toolbar.. Right align them :l
Screenshot = new JToggleButton(new ImageIcon());
UserKeyInput = new JToggleButton(new ImageIcon());
UserMouseInput = new JToggleButton(new ImageIcon());
cardPanel = new CardPanel();
add(Screenshot, BorderLayout.EAST);
add(UserKeyInput, BorderLayout.EAST);
add(UserMouseInput, BorderLayout.EAST);
addListeners();
}
public void addButtonLeft() {
JButton Tab = new JButton("Game");
Tab.setFocusable(false);
Tab.setSize(50, 25);
Tab.setActionCommand(String.valueOf(Global.getApplet().getCanvas().getClass().hashCode()));
Tab.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
cardPanel.jumpTo(Integer.valueOf(e.getActionCommand()));
}
});
add(Tab, BorderLayout.WEST);
}
}