摆动组件位置不正确 [英] Incorrect position of Swing components

查看:60
本文介绍了摆动组件位置不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在JFrame的底部具有按钮btn.我在右边.我的代码中的错误在哪里?

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);
    }
}

推荐答案

BoxLayout.Y_AXIS.可见.

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();
            }
        });
    }
}

这篇关于摆动组件位置不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆