本文介绍了分配布局时出错:BoxLayout无法共享的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个Java JFrame类,我想在其中使用boxlayout,但是出现错误,提示java.awt.AWTError: BoxLayout can't be shared.我已经见过其他人遇到此问题,但是他们通过在contentpane上创建boxlayout来解决了这个问题,但这就是我在这里所做的.这是我的代码:

I have this Java JFrame class, in which I want to use a boxlayout, but I get an error saying java.awt.AWTError: BoxLayout can't be shared. I've seen others with this problem, but they solved it by creating the boxlayout on the contentpane, but that is what I'm doing here. Here's my code:

class EditDialog extends JFrame {
    JTextField title = new JTextField();
    public editDialog() {
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setTitle("New entity");
        getContentPane().setLayout(
            new BoxLayout(this, BoxLayout.PAGE_AXIS));
        add(title);
        pack();
        setVisible(true);
    }
}

推荐答案

您的问题是您要为JFrame(this)创建BoxLayout,但是将其设置为(getContentPane()).试试:

Your problem is that you're creating a BoxLayout for a JFrame (this), but setting it as the layout for a JPanel (getContentPane()). Try:

getContentPane().setLayout(
    new BoxLayout(getContentPane(), BoxLayout.PAGE_AXIS)
);

这篇关于分配布局时出错:BoxLayout无法共享的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 00:43