首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >BoxLayout左对齐

BoxLayout左对齐
EN

Stack Overflow用户
提问于 2015-07-09 15:57:58
回答 1查看 1.1K关注 0票数 2

我正试着向左对齐类和中性按钮,这样它们就与最左边的卡按钮保持一致。由于某些原因,setAlignmentX只将按钮移到一半。这是密码。有其他的按钮对齐吗?

代码语言:javascript
复制
private String[] listEntries = {"a","a","a","a","a"};
private JButton remove = new JButton("Remove");
private JList list;
private JButton b1 = new JButton("Class");
private JButton b2 = new JButton("Neutral");
private JPanel page = new JPanel(new CardLayout());
private DefaultListModel listModel = new DefaultListModel();

public Main () {

    JPanel leftPanel = new JPanel();
    JPanel rightPanel = new JPanel();
    rightPanel.setLayout(new BoxLayout(rightPanel,BoxLayout.PAGE_AXIS));
    leftPanel.setLayout(new BoxLayout(leftPanel,BoxLayout.PAGE_AXIS));
    rightPanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
    leftPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
    JLabel title  = new JLabel("Deck Constructor", SwingConstants.CENTER);
    title.setBorder(BorderFactory.createEmptyBorder(10,0,0,0));

    //Set up Deck List 
    list = new JList(listModel);
    list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
    list.setLayoutOrientation(JList.VERTICAL);
    list.setVisibleRowCount(-1);

    JScrollPane listScroller = new JScrollPane(list);
    listScroller.setPreferredSize(new Dimension(150, 80));

    JLabel listTitle = new JLabel("List");
    listTitle.setLabelFor(list);
    listScroller.setAlignmentX(LEFT_ALIGNMENT);


    rightPanel.add(listTitle);
    rightPanel.add(listScroller);
    rightPanel.add(remove);

    //Set up Card Selection
    JPanel buttonPanel = new JPanel();
    buttonPanel.setAlignmentX(Component.RIGHT_ALIGNMENT);

    b1.setActionCommand("Class");
    b2.setActionCommand("Neutral");
    b1.addActionListener(this);
    b2.addActionListener(this);
    buttonPanel.add(b1);
    buttonPanel.add(b2);

    JPanel classCards = new JPanel(new GridLayout(2,3, 10, 10));
    JButton card1 = new JButton("Card 1");
    card1.addActionListener(this);
    card1.setActionCommand("addCard");
    JButton card2 = new JButton("Card 2");
    JButton card3 = new JButton("Card 3");
    JButton card4 = new JButton("Card 4");
    JButton card5 = new JButton("Card 5");
    JButton card6 = new JButton("Card 6");
    classCards.add(card1);
    classCards.add(card2);
    classCards.add(card3);
    classCards.add(card4);
    classCards.add(card5);
    classCards.add(card6);


    JPanel neutral = new JPanel();
    neutral.setBackground(Color.BLUE);
    page.add(classCards, "Class");
    page.add(neutral, "Neutral");

    leftPanel.add(buttonPanel);
    leftPanel.add(page);


    setPreferredSize(new Dimension(640,640/12*9));
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    getContentPane().add(leftPanel,BorderLayout.CENTER);
    getContentPane().add(rightPanel,BorderLayout.EAST);
    getContentPane().add(title,BorderLayout.NORTH);
    pack();
    setVisible(true);
}
EN

回答 1

Stack Overflow用户

发布于 2015-07-09 18:31:46

它不是完美的解决方案,但您可以使用例如:

  1. 如果要保持按钮的默认大小: JPanel buttonPanel =新JPanel(新FlowLayout(FlowLayout.LEADING,1,2)); 删除: buttonPanel.setAlignmentX(Component.RIGHT_ALIGNMENT);
  2. 如果要用按钮填充buttonPanel: JPanel buttonPanel =新JPanel(1,2,2,2,2);buttonPanel.setBorder(2,1,2,1);

(FlowLayout.LEADING,1,2)EmptyBorder(2,1,2,1))中,添加1,2值以匹配buttonPanelclassCard、hgap和vgap。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31322590

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档