我继续向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();
}
}
发布于 2018-09-10 07:17:39
这本身并不是一件容易的事情,也不是一件容易的事情,当你应该从经验中获得很多东西的时候(哦,我记得上次我这样做了,管理和维护很糟糕),但是,有很多可用的模式每天都被用来让软件开发变得更容易,并解决日常常见的问题。
你需要记住的一件事是谁有责任做什么。例如,可能无法分离按钮的操作侦听器,因为它们需要执行UI本身内部的操作。
但是,您可以通过使用Anonymous Classes甚至Actions API来简化操作,这样就可以隔离按钮的功能
我要考虑的另一件事是将所有独立的容器(面板)隔离到它们自己的类中。这隔离了功能并降低了复杂性,因为它迫使您考虑每个子容器是如何工作的,以及它将负责什么,并减少来自外部影响的不必要访问。
一个更复杂的解决方案是让UI依赖于一个“模型”,该模型从UI中分离出来。然后,UI将从用户处采取操作并更新模型,这将内部生成通知,UI将使用该通知来更新自身以反映更改,有关更多详细信息,请参见Model-View-Controller
那么,答案是什么呢?
https://stackoverflow.com/questions/52249265
复制相似问题