SWING:使用 setBackground Container 调用方法会由于递归调用导致 StackOverFlow 错误
SWING : calling a method with the setBackground Container causes StackOverFlow error due to recursive call
当我调用创建 Jpanel 对象和 GridBagLayout 对象并将 JFrame 的背景设置为青色的方法时。我收到一个 Whosebug 错误,我相信这是由于 setBackground 函数试图设置面板背景以外的容器的背景,例如我的按钮,这就是导致错误的原因,我试图将函数放入另一个 JPanel 但没有 avial。我的问题是,我可以在我的代码中做些什么来修复 Whosebug 错误的发生,我是否需要为背景创建一个新面板。
代码:
挥杆方法:
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;
}
class 方法被调用到:
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);
}
每次调用此方法时,它都会自行调用直到崩溃...
public JPanel panel(){
JPanel panel = new JPanel(new GridBagLayout());
panel().setBackground(Color.cyan);
return panel;
}
现在可能是考虑将这些方法重命名为类似 getXxx
的好时机
Thanks , thats worked :) , now that the frame runs it is completely empty, however when i created all the objects inside the class it worked fine, is there a chance that calling the methods from outside the class is breaking the code due to the objects not being created in the same class ?
每次您调用其中一种方法时,都会创建该对象的一个新实例。
这个...
OPT.getFrame().pack();
OPT.getFrame().setSize(800, 800);
OPT.getFrame().setLocationRelativeTo(null);
OPT.getFrame().setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
OPT.getFrame().setVisible(true);
单独创建了 5 个单独的 JFrame
实例,其中没有一个与之前的
有任何关系
相反,您应该考虑重新设计解决方案以隔离功能或使用“惰性”属性,例如...
public class Options {
private JFrame frame;
private JPanel mainPanel;
private JPanel panel;
private GridBagConstraints gbc;
public JFrame getFrame() {
if (frame == null) {
frame = new JFrame();
frame.setTitle("a new " + MethodHandles.lookup().lookupClass().getName() + " Appears! ");
}
return frame;
}
public JPanel getMainPanel() {
if (mainPanel == null) {
mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
//mainPanel().setBackground(Color.cyan);
}
return mainPanel;
}
public JPanel getPanel() {
if (panel == null) {
panel = new JPanel(new GridBagLayout());
panel.setBackground(Color.cyan);
}
return panel;
}
public GridBagConstraints getGridBagConstraints() {
if (gbc == null) {
gbc = new GridBagConstraints();
}
return gbc;
}
}
当我调用创建 Jpanel 对象和 GridBagLayout 对象并将 JFrame 的背景设置为青色的方法时。我收到一个 Whosebug 错误,我相信这是由于 setBackground 函数试图设置面板背景以外的容器的背景,例如我的按钮,这就是导致错误的原因,我试图将函数放入另一个 JPanel 但没有 avial。我的问题是,我可以在我的代码中做些什么来修复 Whosebug 错误的发生,我是否需要为背景创建一个新面板。
代码:
挥杆方法:
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;
}
class 方法被调用到:
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);
}
每次调用此方法时,它都会自行调用直到崩溃...
public JPanel panel(){
JPanel panel = new JPanel(new GridBagLayout());
panel().setBackground(Color.cyan);
return panel;
}
现在可能是考虑将这些方法重命名为类似 getXxx
Thanks , thats worked :) , now that the frame runs it is completely empty, however when i created all the objects inside the class it worked fine, is there a chance that calling the methods from outside the class is breaking the code due to the objects not being created in the same class ?
每次您调用其中一种方法时,都会创建该对象的一个新实例。
这个...
OPT.getFrame().pack();
OPT.getFrame().setSize(800, 800);
OPT.getFrame().setLocationRelativeTo(null);
OPT.getFrame().setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
OPT.getFrame().setVisible(true);
单独创建了 5 个单独的 JFrame
实例,其中没有一个与之前的
相反,您应该考虑重新设计解决方案以隔离功能或使用“惰性”属性,例如...
public class Options {
private JFrame frame;
private JPanel mainPanel;
private JPanel panel;
private GridBagConstraints gbc;
public JFrame getFrame() {
if (frame == null) {
frame = new JFrame();
frame.setTitle("a new " + MethodHandles.lookup().lookupClass().getName() + " Appears! ");
}
return frame;
}
public JPanel getMainPanel() {
if (mainPanel == null) {
mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
//mainPanel().setBackground(Color.cyan);
}
return mainPanel;
}
public JPanel getPanel() {
if (panel == null) {
panel = new JPanel(new GridBagLayout());
panel.setBackground(Color.cyan);
}
return panel;
}
public GridBagConstraints getGridBagConstraints() {
if (gbc == null) {
gbc = new GridBagConstraints();
}
return gbc;
}
}