传入构造函数的参数 - Java

时间:2016-02-14 16:36:08

标签: java swing parameters constructor

我有一个Person课来描述一个人的某些特征:

public class Person {

    private String name;
    private String surname;
    private String birthPlace;
    private int age;

    public Person(String name, String surname, String birthPlace, int age) {
        super();
        this.name = name;
        this.surname = surname;
        this.birthPlace = birthPlace;
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public String getSurname() {
        return surname;
    }
    public String getBirthPlace() {
        return birthPlace;
    }
    public int getAge() {
        return age;
    }
}

PersonDemo课程中,我创建了4个人,然后将其添加到ArrayList<Person>。然后我将ArrayList<Person>传递给Frame类(扩展JFrame):

import java.util.ArrayList;

public class PersonDemo {

    public static void main(String[] args) {

        Person p1 = new Person("Cristiano","Ronaldo","Santo Antonio", 31);
        Person p2 = new Person("Zlatan","Ibrahimovic","Malmo", 34);
        Person p3 = new Person("Alessandro","Del Piero", "Conegliano", 41);
        Person p4 = new Person("Zinedine","Zidane","Marseille",43);

        //ArrayList<Person>
        ArrayList<Person> people = new ArrayList<>();
        people.add(p1);
        people.add(p2);
        people.add(p3);
        people.add(p4);

        //Create and make visible a new Frame() f passing 
        //as parameter the previous ArrayList<Person>
        Frame f = new Frame(people);
        f.setVisible(true);
    }
}

我用JSplitPane分割我的框架:

import java.util.ArrayList;

import javax.swing.JFrame;

public class Frame extends JFrame{

    private static final long serialVersionUID = 1L;
    private static final int X_AXIS = 175;
    private static final int Y_AXIS = 25;
    private static final int WIDTH= 930;
    private static final int HEIGHT = 600;

    private SplitPane splitPane;

    public Frame(ArrayList<Person> p) {

        this.setTitle("My Frame");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setBounds(X_AXIS,Y_AXIS,WIDTH,HEIGHT);

        //pass to the splitPane the ArrayList<Person> p
        splitPane = new SplitPane(p);
        this.getContentPane().add(splitPane);   

    }
}

JSplitPane的左侧,我创建了一个新的JPanel;在此JPanel中,我必须处理ArrayList<Person>

import java.util.ArrayList;
import javax.swing.JPanel;
import javax.swing.JSplitPane;

public class SplitPane extends JSplitPane{

    private static final long serialVersionUID = 1L;
    private static final int SPLIT_WEIGHT  = 250;
    private JPanel leftPanel;

    public SplitPane(ArrayList<Person> p) {

        //pass to the JPanel leftPanel the ArrayList<Person> p
        leftPanel = new LeftPanel(p);
        this.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
        this.setDividerLocation(SPLIT_WEIGHT);
        this.setLeftComponent(leftPanel);
    }
}

这是我的LeftPanel类,它扩展了JPanel

import java.util.ArrayList;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class LeftPanel extends JPanel{

    private static final long serialVersionUID = 1L;

    public LeftPanel(ArrayList<Person> p) { 

        JLabel label = new JLabel("This is the left side of \"My Frame\"");
        this.add(label);

        //some code to work with ArrayList<Person> p in this JPanel 
    }
}

我想知道是否在构造函数(ArrayList<Person>)中传递JFrame -> JSplitPane -> JPanel是一种很好的方法,或者有更好的解决方案(可能是继承)。

1 个答案:

答案 0 :(得分:3)

实施UI程序的常用策略是使用Model-View-Controller方法。在您的程序中,Swing类将是视图,而PersonArrayList<Person>将是模型

视图需要访问模型中的数据才能向最终用户显示相关部分。有三种常见的方法可以让视图访问模型的相关部分:

  • 在构造函数中传递模型部件 - 这是您采用的方法,
  • 将模型设为单身 - 此方法允许所有视图访问模型的所有部分,
  • 使用控件容器的反转 - 这是一种高级方法,可让您以声明方式配置访问。

对于您开发的小程序,第三种方法可能过于先进,但单例可能比明确传递列表更好:

class Model {
    private static List<Person> people;
    public static List<Person> getPeople() {
        return people;
    }
    public static void setPeople(List<Person> people) {
        this.people = people;
    }
}

现在你的main会这样做:

Person p1 = new Person("Cristiano","Ronaldo","Santo Antonio", 31);
Person p2 = new Person("Zlatan","Ibrahimovic","Malmo", 34);
Person p3 = new Person("Alessandro","Del Piero", "Conegliano", 41);
Person p4 = new Person("Zinedine","Zidane","Marseille",43);

//ArrayList<Person>
ArrayList<Person> people = new ArrayList<>();
people.add(p1);
people.add(p2);
people.add(p3);
people.add(p4);
Model.setPeople(people);

你的挥杆课程将不再绕过ArrayList<Person>。相反,他们每次需要访问Model.getPeople()时都会List<Person>