Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java—处理大型GUI构造函数的最佳方法?_Java_User Interface_Constructor - Fatal编程技术网

Java—处理大型GUI构造函数的最佳方法?

Java—处理大型GUI构造函数的最佳方法?,java,user-interface,constructor,Java,User Interface,Constructor,我发现,在Java中制作gui应用程序时,如果我不将gui类的构造函数抽象/提取到其他类或方法中以缩短它,它就会变得很长。。。处理大型gui构造函数的最佳/最符合逻辑/最不混乱的方式是什么?我收集了两种最常用的处理方法。。。最好的方法是什么,更重要的是,为什么/为什么不 方法1,为每个gui组件组织类,其中每个类扩展其gui组件: public class GUI extends JFrame{ public GUI(String title){ super(title); th

我发现,在Java中制作gui应用程序时,如果我不将gui类的构造函数抽象/提取到其他类或方法中以缩短它,它就会变得很长。。。处理大型gui构造函数的最佳/最符合逻辑/最不混乱的方式是什么?我收集了两种最常用的处理方法。。。最好的方法是什么,更重要的是,为什么/为什么不

方法1,为每个gui组件组织类,其中每个类扩展其gui组件:

public class GUI extends JFrame{
public GUI(String title){
    super(title);
    this.setVisible(true);
    this.setLayout(new GridBagLayout());
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setSize(500,500);
    this.add(new mainPanel());
}
private class mainPanel extends JPanel{
    private mainPanel(){
        this.setSize(new Dimension(500,500));
        this.setLayout(new BorderLayout());
        this.add(new PlayButton("Play Now"));
    }
    private class PlayButton extends JButton{
        private PlayButton(String text){
            this.setText(text);
            this.setSize(150,50);
            this.setBackground(Color.WHITE);
            this.setForeground(Color.BLACK);
        }
    }
}
}
public class GUI extends JFrame{
public GUI(String title){
    super(title);
    this.setVisible(true);
    this.setLayout(new GridBagLayout());
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setSize(500,500);
    initGuiComponents();
}

private void initGuiComponents(){
    this.add(mainPanel());
}
private JPanel mainPanel(){
    JPanel mainPanel = new JPanel();
    mainPanel.setSize(new Dimension(500,500));
    mainPanel.setLayout(new BorderLayout());
    mainPanel.add(playButton("Play Now"));
    return mainPanel;
}

private JButton playButton(String text){
JButton button = new JButton();
button.setText(text);
button.setSize(150,50);
button.setBackground(Color.WHITE);
button.setForeground(Color.BLACK);
return button;
    }
}
方法2:使用初始化方法和返回每个gui组件实例的方法:

public class GUI extends JFrame{
public GUI(String title){
    super(title);
    this.setVisible(true);
    this.setLayout(new GridBagLayout());
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setSize(500,500);
    this.add(new mainPanel());
}
private class mainPanel extends JPanel{
    private mainPanel(){
        this.setSize(new Dimension(500,500));
        this.setLayout(new BorderLayout());
        this.add(new PlayButton("Play Now"));
    }
    private class PlayButton extends JButton{
        private PlayButton(String text){
            this.setText(text);
            this.setSize(150,50);
            this.setBackground(Color.WHITE);
            this.setForeground(Color.BLACK);
        }
    }
}
}
public class GUI extends JFrame{
public GUI(String title){
    super(title);
    this.setVisible(true);
    this.setLayout(new GridBagLayout());
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setSize(500,500);
    initGuiComponents();
}

private void initGuiComponents(){
    this.add(mainPanel());
}
private JPanel mainPanel(){
    JPanel mainPanel = new JPanel();
    mainPanel.setSize(new Dimension(500,500));
    mainPanel.setLayout(new BorderLayout());
    mainPanel.add(playButton("Play Now"));
    return mainPanel;
}

private JButton playButton(String text){
JButton button = new JButton();
button.setText(text);
button.setSize(150,50);
button.setBackground(Color.WHITE);
button.setForeground(Color.BLACK);
return button;
    }
}

我认为两者结合使用是个好主意

与使用内部类不同,使用顶级类可能会使代码更易于维护。您可以根据功能和职责将框架划分为小面板。如果您的分离足够好,它们将是松散耦合的,您不需要向它们传递许多参数

同时,如果构造器或任何方法增长过快,将紧密相关的操作加入私有方法可能有助于使代码可读

漂亮的程序源于高质量的抽象和封装

尽管实现这些目标需要实践和经验,但坚持始终是你的首要任务

希望这有帮助。
祝您好运。

也许可以考虑使用。

如果您在许多组件上使用相同的设置,即每个按钮具有相同的背景/背景颜色,请使用出厂:

class ComponentFactory{
    static JButton createButton(String text) {
        JButton button = new JButton();
        button.setBackground(Color.WHITE);
        button.setForeground(Color.BLACK);
    }
}
然后,在构造函数中,可以调整非常量设置:

JButton button = ComponentFactory.createButton();
button.setText(text);
[...]
这样做的好处是,您只需更改一次设置(如BG color),即可更改所有按钮的外观

为了缩短构造函数的长度,我通常将进程拆分为createChildren、layoutComponents、registerListeners和其他可能有用的内容。然后,我从抽象超类中的构造函数调用这些方法。因此,许多子类根本不需要构造函数——或者一个很短的调用super并进行一些自定义设置的构造函数