gpt4 book ai didi

Java按钮布局右对齐

转载 作者:行者123 更新时间:2023-11-30 04:03:37 25 4
gpt4 key购买 nike

请有人对我的代码提供一些见解。我想让平衡选项卡上的按钮与左上角对齐。我尝试过不同的布局管理器,但似乎都导致按钮位于中心的相同或相似的结果

public class DefaultView extends JFrame {

public DefaultView() {
JButton SendBalInc = new JButton();
SendBalInc.setText("Balance");
GridBagConstraints c = new GridBagConstraints();
GridBagLayout gridbag = new GridBagLayout();
c.fill = GridBagConstraints.BOTH;
// Bal.fill = GridBagConstraints.NONE;
JPanel window = new JPanel();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(800, 600);
// this.setUndecorated(true);
window.setBackground(Color.WHITE);
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.setBackground(Color.WHITE);
JComponent panel1 = makeTextPanel("Panel #1");
tabbedPane.addTab("PIN ", panel1);
JPanel panel2 = new JPanel();
Box box = Box.createHorizontalBox();
box.add(Box.createHorizontalGlue());
box.add(SendBalInc);
panel2.setBackground(Color.WHITE);
panel2.add(box);
tabbedPane.addTab("Balance", panel2);
JComponent panel3 = makeTextPanel("Panel #3");
tabbedPane.addTab("Dep", panel3);
JComponent panel4 = makeTextPanel("Panel #4");
tabbedPane.addTab("Bill", panel4);
window.setLayout(new GridLayout(1, 2));
window.add(tabbedPane);
this.add(window);
}

protected JComponent makeTextPanel(String text) {
JPanel panel = new JPanel();
panel.setBackground(Color.WHITE);
return panel;
}
}

最佳答案

我更喜欢 GridBagLayout:

    final JPanel panel2 = new JPanel();
panel2.setLayout(new GridBagLayout());
panel2.setBackground(Color.WHITE);

final GridBagConstraints cons = new GridBagConstraints();
cons.weightx = 1D;
cons.weighty = 1D;
cons.anchor = GridBagConstraints.NORTHWEST;
panel2.add(box, cons);

tabbedPane.addTab("Balance", panel2);

注意:不需要盒子

关于Java按钮布局右对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21333193/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com