是的,你可以很容易地打破这一点。任何地方代码生成一个新的面板,可能作为一个“构建”方法,你可以把它拉出来,并在另一个类的结构中创建。一个例子是这个样子:
// New class file named testPanel.java
public class testPanel extends JPanel{
// Constructor
public textPanel(){
// Add an example button
JButton btn_exit = new JButton("Exit");
btn_exit.addActionListener(new ExitButtonListener());
buttons.add(btn_exit);
}
// Private inner class which does event handling for our example button
private class ExitButtonListener implements ActionListener{
public void actionPerformed(ActionEvent e){
System.exit(0);
}
}
// Add whatever other code you like here or above or anywhere else :)
}
然后使用该面板在主JFrame中,你可以聚合这样的:
private testPanel pnl_test = new textPanel();
// You can place this in the constructor
// for your JFrame without the "MyJFrame."
// But for demonstration purposes I will include it
MyJFrame.add(pnl_test);
// Or if you were placing a panel inside of another panel,
// you can use the same method associated with a JPanel object
MyJPanel.add(pnl_test);
我不会建议使用的IntelliJ的自动化Swing GUI的发电机,如果你正在构建一个复杂的GUI。该代码不可读且不可维护,也不允许您更改Swing中通常可编辑的组件的许多属性。我的建议是你编写你自己的图形用户界面,而不需要Intellij的自动代码生成器。 –