我有一个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
)中是一个很好的方法,或者有更好的解决方案(也许是继承)。
最佳答案
实现 UI 程序的常见策略是使用 Model-View-Controller方法。在您的程序中,Swing 类将是 View ,而 Person
和ArrayList<Person>
将是模型。
View 需要访问模型中的数据,以便向最终用户显示相关部分。授予 View 访问模型相关部分的常用方法有以下三种:
- 在构造函数中传递模型部件 - 这是您采用的方法,
- 使模型成为单例 - 这种方法允许所有 View 访问模型的所有部分,
- 使用控制容器反转 - 这是一种高级方法,可让您以声明性方式配置访问权限。
对于您开发的小程序来说,第三种方法可能太先进了,但是单例可能比显式传递列表更好:
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);
你的 Swing 类(class)将不再传播ArrayList<Person>
。相反,他们会这样做Model.getPeople()
每次他们需要访问 List<Person>
.
关于java - 构造函数中传递的参数 - Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35394200/