Java 回转组件的位置不正确
我希望在Java 回转组件的位置不正确,java,swing,jframe,jbutton,layout-manager,Java,Swing,Jframe,Jbutton,Layout Manager,我希望在JFrame的底部有按钮btn。我右边有这个。我的代码中的bug在哪里 class MainClass extends JFrame { private JSplitPane splitPan=null; private void treePanel(){ DefaultMutableTreeNode nod=new DefaultMutableTreeNode("AAA",true); nod.add(new DefaultMuta
JFrame
的底部有按钮btn
。我右边有这个。我的代码中的bug在哪里
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);
}
}
默认情况下已实现,如果要将其置于JFrame
的底部,则必须定义add(new JButton(“btn”,BorderLayout.SOUTH))代码>您应该使用而不是X\u轴
:
组件从上到下垂直布置
BoxLayout.Y_轴
。维斯
import javax.swing.*;
import javax.swing.tree.*;
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() {
// this is it!
setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
treePanel();
add(new JButton("btn"));
pack();
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(300,200);
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new MainClass();
}
});
}
}