簡體   English   中英

構建我的Java Swing應用程序。 IE瀏覽器何時何地使用類而不是

[英]Structuring my Java Swing application. I.E. when and where to use classes and not

我繼續在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本身內部的操作。

但是,您可以通過使用匿名類甚至是Actions API來簡化操作 ,該操作允許隔離按鈕的功能

我要看的另一件事是將所有單獨的容器(面板)隔離到自己的類中。 這樣可以隔離功能並降低復雜性,因為它迫使您考慮每個子容器的工作方式以及它負責什么,並減少了來自外部影響的不必要訪問。

更復雜的解決方案將使UI依賴於與UI分離的“模型”。 然后,UI會從用戶處采取措施並更新模型,這將實習生生成通知,UI會使用該通知來更新自身以反映更改,有關更多詳細信息,請參見Model-View-Controller

那么,答案是什么?

  • 將數據與用戶界面分開。 使用一個或多個容器/模型類來表示數據。 利用觀察者模式允許模型在發生更改時生成事件,以便有興趣的各方可以采取適當的措施
  • 將您的UI分解為“可用的”組件,其中每個組件都是獨立的,並且負責管理UI的單個部分(這是怎么回事,這是它自己的事)
  • 我還建議您使用依賴注入在分離的元素之間共享對象,這將使您能夠隔離並測試代碼的各個區域,這也將導致...
  • 測試驅動開發(TDD) 在嘗試設計代碼之前,請先知道要測試什么以及如何進行測試

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM