Swing 组件位置不正确
我希望在 JFrame
底部有按钮 btn
。我的右边有这个。我的代码中的错误在哪里?
class MainClass extends JFrame {
private JSplitPane splitPan=null;
private void treePanel(){
DefaultMutableTreeNode nod=new DefaultMutableTreeNode("AAA",true);
nod.add(new DefaultMutableTreeNode("abcd"));
JTree tree=new JTree(nod);
JScrollPane scroll=new JScrollPane(tree);
splitPan=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,scroll,new JLabel("aaaaa"));
splitPan.setSize(this.getMaximumSize());
add(splitPan);
}
public MainClass() {
setLayout(new BoxLayout(getContentPane(), BoxLayout.X_AXIS));
treePanel();
add(new JButton("btn"));
pack();
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(300,200);
setVisible(true);
}
}
I woud like to have button btn
at the bottom of a JFrame
. I have this in right side. Where is bug in my code?
class MainClass extends JFrame {
private JSplitPane splitPan=null;
private void treePanel(){
DefaultMutableTreeNode nod=new DefaultMutableTreeNode("AAA",true);
nod.add(new DefaultMutableTreeNode("abcd"));
JTree tree=new JTree(nod);
JScrollPane scroll=new JScrollPane(tree);
splitPan=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,scroll,new JLabel("aaaaa"));
splitPan.setSize(this.getMaximumSize());
add(splitPan);
}
public MainClass() {
setLayout(new BoxLayout(getContentPane(), BoxLayout.X_AXIS));
treePanel();
add(new JButton("btn"));
pack();
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(300,200);
setVisible(true);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
BoxLayout.Y_AXIS
。维斯。BoxLayout.Y_AXIS
. Vis.您应该使用
BoxLayout.Y_AXIS
而不是
X_AXIS
:You should use
BoxLayout.Y_AXIS
instead ofX_AXIS
:JFrame 已实现 BorderLayout 默认情况下,如果你想放置 JButton 到
JFrame
底部,然后你必须定义add(new JButton("btn", BorderLayout.SOUTH));
JFrame has implemented BorderLayout by default, if you want to put JButton to the bottom of
JFrame
, then you have to defineadd(new JButton("btn", BorderLayout.SOUTH));