gpt4 book ai didi

java - JPanel : how to add buttons above table?

转载 作者:行者123 更新时间:2023-12-04 06:43:22 25 4
gpt4 key购买 nike

    JPanel panel = new JPanel();
bigbutton = new JButton("Big Button");
clearbutton = new JButton("Clear Page");
resetbutton = new JButton("Start Over");
finishbutton = new JButton("Finish");


panel.add(sometable);
return panel;

现在发生的是它们水平蔓延。我想要水平和 table 上方的四个按钮。

最佳答案

right now what happens is they sprawl horizontally.



除了@tulskiy 和@Andrew Thompson 的建议之外,注意 JPanel 的默认布局可能很有用。是 FlowLayout ,它将组件排列在水平行中。

附录:

change JPanel's FlowLayout to BorderLayout or some other layout?



选择布局在一定程度上取决于品味,但我会结合建议的方法,如 How to Use Tool Bars 所示。 .注意如何 ToolBarDemo扩展 JPanel然后调用父类(super class)构造函数来指定 BorderLayout() :
public class ToolBarDemo extends JPanel implements ActionListener {
...
public ToolBarDemo() {
super(new BorderLayout());
...
JToolBar toolBar = new JToolBar("Still draggable");
addButtons(toolBar);
...
setPreferredSize(new Dimension(450, 130));
add(toolBar, BorderLayout.PAGE_START);
add(sometable, BorderLayout.CENTER);
}
...
}

没有扩展,直接使用构造函数即可:
JPanel panel = new JPanel(new BorderLayout());

或者,使用 setLayout() JPanel继承的方法.
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());

关于java - JPanel : how to add buttons above table?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3974468/

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