【问题标题】:SWING : calling a method with the setBackground Container causes StackOverFlow error due to recursive callSWING : 使用 setBackground Container 调用方法由于递归调用导致 StackOverFlow 错误
【发布时间】: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 ...我也会考虑一些惰性初始化
  • 谢谢,那行得通:),现在框架运行它是完全空的,但是当我在类中创建所有对象时它工作正常,是否有机会从外部调用方法由于没有在同一个类中创建对象,类正在破坏代码?
  • 每次调用其中一个方法时,都会为这些对象创建一个新实例,所以你,我不知道,几十个对象都什么都不做。所以,正如我所说,我会考虑让这些方法使用“延迟初始化”工作流程

标签: java swing jframe


【解决方案1】:

每次调用此方法时,它都会调用自身,直到崩溃...

public JPanel panel(){
    JPanel panel = new JPanel(new GridBagLayout());
    panel().setBackground(Color.cyan);
    return panel;
}

现在可能是考虑将这些方法重命名为 getXxx 之类的好时机

谢谢,这行得通:),现在框架运行它是完全空的,但是当我在类中创建所有对象时它工作正常,是否有可能从类外部调用方法会破坏代码由于对象不是在同一个类中创建的?

每次调用其中一个方法时,它都会创建对象的一个​​新实例。

这...

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;
    }
}

【讨论】:

    猜你喜欢
    • 2017-09-13
    • 1970-01-01
    • 2012-07-24
    • 1970-01-01
    • 1970-01-01
    • 2014-10-31
    • 2016-10-14
    • 2016-08-17
    • 2021-12-25
    相关资源
    最近更新 更多