目录

我的学习分享

记录精彩的程序人生

java.awt.AWTError: BoxLayout can't be shared

https://stackoverflow.com/questions/5917017/java-awt-awterror-boxlayout-cant-be-shared

When calling setLayout on a JFrame, you're actually adding the layout to
the JFrame's contentPane not the JFrame itself since this method is
more of a convenience method that transmits the method call to the
contentPane. The BoxLayout constructor must reflect this since you can't
add the BoxLayout to one container and then pass in as a parameter a
different container. So change this:

this.setLayout(new BoxLayout(this,BoxLayout.X_AXIS));

 

to this:

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