【发布时间】:2021-11-07 02:08:15
【问题描述】:
当我调用创建 Jpanel 对象和 GridBagLayout 对象的方法,并将 JFrame 的背景设置为青色时。我收到 stackOverFlow 错误,我相信这是由于 setBackground 函数试图设置面板背景以外的容器的背景,例如我的按钮,这就是导致错误的原因,我试图将函数放入另一个JPanel,但没有avial。我的问题是,我可以在我的代码中做些什么来修复 StackOverFlow 错误的发生,我是否需要为背景创建一个新面板。
代码:
摆动方法:
public JFrame frame(){
JFrame frame = new JFrame();
frame.setTitle("a new " + MethodHandles.lookup().lookupClass().getName() + " Appears! ");
return frame;
}
public JPanel mainPanel(){
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
//mainPanel().setBackground(Color.cyan);
return mainPanel;
}
public JPanel panel(){
JPanel panel = new JPanel(new GridBagLayout());
panel().setBackground(Color.cyan);
return panel;
}
public GridBagConstraints constr(){
GridBagConstraints constr = new GridBagConstraints();
return constr;
}
调用方法的分类:
static void AddWindow(){
Options OPT = new Options();
Tutors TRS = new Tutors();
/*JFrame frame = new JFrame();
frame.setTitle("a new " + MethodHandles.lookup().lookupClass().getName() + " Appears! ");
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));*/
// JPanel panel = new JPanel(new GridBagLayout());
//panel.setBackground(Color.cyan);
OPT.frame();
OPT.mainPanel();
OPT.panel();
OPT.constr();
// Constraints for the layout
//GridBagConstraints constr = new GridBagConstraints();
// Setting initial grid values to 0,0
/////////// first columns ///////////////////////
OPT.constr().weightx = 0.5;
OPT.constr().weighty = 0.5;
OPT.constr().gridx = 0;
OPT.constr().gridy = 0;
OPT.panel().add(OPT.NamesText(), OPT.constr());
OPT.constr().gridx = 0;
OPT.constr().gridy = 1;
OPT.panel().add(OPT.ModuleNameText(), OPT.constr());
OPT.constr().gridx = 0;
OPT.constr().gridy = 2;
OPT.panel().add(OPT.PostitionText(), OPT.constr());
OPT.constr().gridx = 0;
OPT.constr().gridy = 3;
OPT.panel().add(OPT.AreaOfExpertiseText(), OPT.constr());
OPT.constr().gridx = 0;
OPT.constr().gridy = 4;
OPT.panel().add(OPT.StudentsSupervisedText(), OPT.constr());
/////////// second column ////////////////////
OPT.constr().gridx = 1;
OPT.constr().gridy = 0;
OPT.panel().add(OPT.Names(), OPT.constr());
OPT.constr().gridx = 1;
OPT.constr().gridy = 1;
OPT.panel().add(OPT.ModuleName(), OPT.constr());
OPT.constr().gridx = 1;
OPT.constr().gridy = 2;
OPT.panel().add(OPT.Position(), OPT.constr());
OPT.constr().gridx = 1;
OPT.constr().gridy = 3;
OPT.panel().add(OPT.AreaOfExperise(), OPT.constr());
OPT.constr().gridx = 1;
OPT.constr().gridy = 4;
OPT.panel().add(OPT.StudentsSupervised(), OPT.constr());
OPT.constr().gridx = 1;
OPT.constr().gridy = 5;
OPT.panel().add(OPT.SubmitButton(), OPT.constr());
OPT.mainPanel().add(OPT.panel());
OPT.frame().add(OPT.mainPanel());
OPT.frame().pack();
OPT.frame().setSize(800,800);
OPT.frame().setLocationRelativeTo(null);
OPT.frame().setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
OPT.frame().setVisible(true);
}
【问题讨论】:
-
panel().setBackground(Color.cyan);正在调用panel()方法,一遍又一遍,一遍又一遍……你明白了,你的意思是使用panel.setBackground(Color.cyan);。这可能是重新审视方法名称的好时机,也许可以改用getXxx...我也会考虑一些惰性初始化 -
谢谢,那行得通:),现在框架运行它是完全空的,但是当我在类中创建所有对象时它工作正常,是否有机会从外部调用方法由于没有在同一个类中创建对象,类正在破坏代码?
-
每次调用其中一个方法时,都会为这些对象创建一个新实例,所以你,我不知道,几十个对象都什么都不做。所以,正如我所说,我会考虑让这些方法使用“延迟初始化”工作流程