构建我的JavaSwing应用程序。即何时何地使用类,而不是
我继续向Java Swing按钮和字段等添加操作侦听器。我想知道何时何地应该将代码划分为类和不同的方法。不幸的是,现在我的代码感觉像是一个很长的脚本,就像我习惯用Python而不是像Java这样的OOP语言创建的一样 如何更恰当地将代码划分为类和方法 以下是相关代码:构建我的JavaSwing应用程序。即何时何地使用类,而不是,java,swing,class,methods,constructor,Java,Swing,Class,Methods,Constructor,我继续向Java Swing按钮和字段等添加操作侦听器。我想知道何时何地应该将代码划分为类和不同的方法。不幸的是,现在我的代码感觉像是一个很长的脚本,就像我习惯用Python而不是像Java这样的OOP语言创建的一样 如何更恰当地将代码划分为类和方法 以下是相关代码: /* * To change this license header, choose License Headers in Project Properties. * To change this template file,
/*
* 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();
}
}
这本身并不是一件容易的事情,要认识到什么时候应该从经验中获得很多东西哦,我记得上次我这么做的时候,管理和维护Python是一种OOP语言是很可怕的。就编码风格而言,这将是一个更好的地方。一篇很好的文章,总的来说,大概可以在八个短段落中找到。整本书都是关于最佳实践和如何分解软件的。程序员没有提到的一件事是测试。提前了解测试需求并设计代码以便于测试通常是很好的。这导致了上面M.P.所描述的崩溃类型,但记住为什么要以这种方式分解代码是很重要的,这通常是为了使部分代码更易于测试。