构建我的 Java Swing 应用程序. 即何时何地使用类,何时不使用类



我继续向我的Java Swing按钮和字段等添加操作侦听器。我想知道何时何地应该将代码分成类和不同的方法。不幸的是,现在我的代码感觉就像一个长脚本,就像我习惯用 Python 而不是像 Java 这样的 OOP 语言一样。

如何更恰当地将此代码分为类和方法?

这是有问题的代码:

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package business;
import java.awt.BorderLayout;
import java.awt.Color;
import static java.awt.Component.RIGHT_ALIGNMENT;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTree;

/**
*
* @author bob
*/
public class NewClass {

//Initialize GUI elements
JFrame myFrame = new JFrame();

JTree jtree1 = new JTree();
JTree jtree2 = new JTree();
JLabel label1 = new JLabel("Welcome to the person tester application");
JLabel label2 = new JLabel("Test2");
JLabel spacer1 = new JLabel("");
JLabel spacer2 = new JLabel("");
//buttons
JRadioButton radioCustomer = new JRadioButton("Customer");
JRadioButton radioEmployee = new JRadioButton("Employee");
ButtonGroup group = new ButtonGroup();
JButton okButton = new JButton();
JButton button2 = new JButton("Create");
JButton button3 = new JButton("EXIT");

JScrollPane sp = new JScrollPane(jtree1);
JScrollPane sp2 = new JScrollPane(jtree2);
//Panels
JPanel mainPanel = new JPanel(new GridLayout(3,1));
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel(new GridLayout(1,2));
JPanel panel4 = new JPanel();
JPanel createPanel = new JPanel();

//Constructor
public NewClass(){
}

//The createGUI method is inside the class so we can reference the GUI objects created above
public void createGUI(){
//Buttons
button2.setToolTipText("Create");
button3.setToolTipText("Exit");
button3.setForeground(Color.red);
button3.setAlignmentX(RIGHT_ALIGNMENT);
group.add(radioEmployee);
group.add(radioCustomer);

//Adding actionListeners
GUIListener myListener = new GUIListener();
okButton.addActionListener(myListener);
button2.addActionListener(myListener);
button3.addActionListener(myListener);


//adding to and customizing the panels

createPanel.setLayout(new BoxLayout(createPanel, BoxLayout.PAGE_AXIS));
createPanel.add(radioEmployee);
createPanel.add(radioCustomer);
createPanel.add(button2);

panel2.setLayout(new BoxLayout(panel2, BoxLayout.PAGE_AXIS));
panel2.add(label1);
panel2.add(createPanel);

panel3.add(BorderLayout.CENTER, sp);
panel3.add(BorderLayout.CENTER, sp2);

panel4.setLayout(new BoxLayout(panel4, BoxLayout.PAGE_AXIS));
panel4.add(spacer1);
panel4.add(button3);


//adding panels to main panel
mainPanel.add(panel2);
mainPanel.add(panel3);
mainPanel.add(panel4);
//adding panels we created to the frame

myFrame.add(mainPanel);

//setting some parameters to customize the frame
myFrame.setSize(600, 400);
myFrame.setLocationRelativeTo(null);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setVisible(true);
}

public class GUIListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == okButton){
label1.setText("okButton was pushed!");
}
else if (e.getSource() == button2){
}

else if (e.getSource() == button3){

System.out.println("button3 was pusshed");
}
}
}
//main method that makes the program run
public static void main(String[] args) {
//instantiate an object of the NewClass class
NewClass GUI = new NewClass();
//Use the method to create and display the GUI
GUI.createGUI();
}
}

这本身并不是一件容易的事情,并且认识到什么时候你应该从经验中得到很多(哦,我记得上次我这样做,管理和维护😝很糟糕(,但是,有很多可用的模式每天都在用,使软件开发更容易并解决日常常见问题。

您需要记住的一件事是谁有责任做什么。 例如,可能无法分离按钮的操作侦听器,因为它们需要执行 UI 本身内部的操作。

但是,您可以通过使用匿名类甚至操作 API 来简化它,这允许隔离按钮的功能

我要看的另一件事是将所有单独的容器(面板(隔离到它们自己的类中。 这隔离了功能并降低了复杂性,因为它迫使您考虑每个子容器将如何工作以及它负责什么,以及减少来自外部影响的不必要访问。

更复杂的解决方案是让 UI 依赖于与 UI 分离的"模型"。 然后,UI 将从用户那里执行操作并更新模型,这将生成 UI 将用于更新自身以反映更改的通知,有关更多详细信息,请参阅模型-视图-控制器

那么,答案是什么?

  • 将数据与 UI 分开。使用一个或多个容器/模型类来表示数据。 利用观察者模式允许模型在发生更改时生成事件,以便相关方可以采取适当的操作
  • 将 UI 分解为"可用"组件,其中每个组件都是隔离的,负责管理 UI 的单个部分(如何发生是它自己的业务(
  • 我还建议使用依赖注入在分离的元素之间共享对象,这将允许您隔离和测试代码的各个区域,奖金,这也会导致......
  • 测试驱动开发 (TDD(。在尝试设计代码之前,了解要测试的内容和方式

最新更新