Java - 处理大型 GUI 构造函数的最佳方法?
Java - best way to deal with a large GUI constructor?
我发现在 Java 中制作 gui 应用程序时,如果我不 abstract/extract 其他 classes,我的 GUI class 的构造函数会变得很长或缩短它的方法... best/most logical/least 处理大型 gui 构造函数的混乱方法是什么?我收集了两种最常用的方法来处理这个问题...什么是最好的方法,更重要的是,why/why 不是?
方法 1,为每个 gui 组件组织成 classes,其中每个 class 扩展其 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);
}
}
}
}
方法二:使用初始化方法,以及return个每个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);
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;
}
}
我认为结合使用两者是个好主意。
使用顶级 类 而不是使用内部 类 可能会使代码更易于维护。您可以根据功能和职责将框架分成小面板。如果您的隔离足够好,它们将松散耦合,您不需要向它们传递很多参数。
同时,如果构造函数或任何方法超出比例,将紧密相关的操作合并到私有方法中可能有助于提高代码的可读性。
美丽的程序源于高质量的抽象和封装。
尽管实现这些需要实践和经验,但坚持 SOLID principles 应该始终是您的首要任务。
希望这对您有所帮助。
祝你好运。
也许考虑使用 builder pattern。
如果您在许多组件上使用相同的设置(即每个 Button 具有相同的 BG/FG 颜色),请使用工厂:
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 颜色)即可更改所有按钮的外观。
为了保持构造函数简短,我通常将过程拆分为 createChildren()
、layoutComponents()
、registerListeners()
以及其他任何看起来有用的方法。然后我从抽象超类中的构造函数调用这些方法。因此,许多子类根本不需要构造函数——或者一个非常短的调用 super()
并进行一些自定义设置的构造函数。
我发现在 Java 中制作 gui 应用程序时,如果我不 abstract/extract 其他 classes,我的 GUI class 的构造函数会变得很长或缩短它的方法... best/most logical/least 处理大型 gui 构造函数的混乱方法是什么?我收集了两种最常用的方法来处理这个问题...什么是最好的方法,更重要的是,why/why 不是?
方法 1,为每个 gui 组件组织成 classes,其中每个 class 扩展其 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);
}
}
}
}
方法二:使用初始化方法,以及return个每个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);
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;
}
}
我认为结合使用两者是个好主意。
使用顶级 类 而不是使用内部 类 可能会使代码更易于维护。您可以根据功能和职责将框架分成小面板。如果您的隔离足够好,它们将松散耦合,您不需要向它们传递很多参数。
同时,如果构造函数或任何方法超出比例,将紧密相关的操作合并到私有方法中可能有助于提高代码的可读性。
美丽的程序源于高质量的抽象和封装。
尽管实现这些需要实践和经验,但坚持 SOLID principles 应该始终是您的首要任务。
希望这对您有所帮助。
祝你好运。
也许考虑使用 builder pattern。
如果您在许多组件上使用相同的设置(即每个 Button 具有相同的 BG/FG 颜色),请使用工厂:
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 颜色)即可更改所有按钮的外观。
为了保持构造函数简短,我通常将过程拆分为 createChildren()
、layoutComponents()
、registerListeners()
以及其他任何看起来有用的方法。然后我从抽象超类中的构造函数调用这些方法。因此,许多子类根本不需要构造函数——或者一个非常短的调用 super()
并进行一些自定义设置的构造函数。