我有一个Jpanel类,每个类代表3个面板,用于自己的选项卡。它决定在面板上添加什么,以及什么不是。然后我有一个类,我存储程序使用的数据。但是当我创建3个实例时,每个选项卡一个,我必须确保每个实例的数据类相同,即使修改后仍然保持不变。
这里我实例化了标签,并传递了标签和数据的编号(Mensola)
Mensola m = new Mensola(20);
JTabbedPane tabbedPane = new JTabbedPane();
JComponent panel1 = new MyPanel1(1,m);
tabbedPane.addTab("Aggiungi",null ,panel1,"Aggiungi");
JComponent panel2 = new MyPanel1(2,m);
tabbedPane.addTab("Cerca",null, panel2,"Cerca");
JComponent panel3 = new MyPanel1(3,m);
tabbedPane.addTab("Togli",null, panel3,"Togli");
add(tabbedPane);
tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
小组类:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
public class MyPanel1 extends JPanel implements ActionListener, ListSelectionListener{
private Libro temp;
ActionEvent Event;
Mensola m = new Mensola(20);
private String sTemp = "Non conosiuto";
private JLabel title;
private JLabel author;
private JLabel editor;
private JLabel pages;
private JLabel price;
private JLabel type;
private JLabel inProduzione;
private JLabel message;
private JLabel nummeroLibri;
private JRadioButton si;
private JRadioButton no;
private ButtonGroup group;
private JList list;
private String[] generi = {"Horror", "Commedia", "Thriller", "Comic"};
private JTextField titleField;
private JTextField authorField;
private JTextField editorField;
private JTextField pagesField;
private JTextField priceField;
private JButton aggiungi;
private JButton cerca;
private JButton togli;
public MyPanel1(int sheet,Mensola x){
m = x;
this.setLayout(new FlowLayout());
m = new Mensola(20);
title = new JLabel("Titolo: ");
author = new JLabel("autore: ");
editor = new JLabel("editore: ");
pages = new JLabel("pagine: ");
price = new JLabel("prezzo: ");
type = new JLabel("genere: ");
type.setPreferredSize(new Dimension(280,20));
inProduzione = new JLabel("In porduzione: ");
message = new JLabel("");
message.setPreferredSize(new Dimension(280,170));
nummeroLibri = new JLabel("Nummero di liberi: 0");
list = new JList(generi);
list.addListSelectionListener(this);
list.setPreferredSize(new Dimension(280,70));
titleField = new JTextField("",20);
authorField = new JTextField("",20);
editorField = new JTextField("",20);
pagesField = new JTextField("",20);
priceField = new JTextField("",20);
aggiungi = new JButton("Aggiungi Libro");
aggiungi.addActionListener(this);
aggiungi.setPreferredSize(new Dimension(280,30));
cerca = new JButton("Cerca Libro In Base Al Titolo");
cerca.addActionListener(this);
cerca.setPreferredSize(new Dimension(280,30));
togli = new JButton("Togli Libro In Base Al Titolo");
togli.addActionListener(this);
togli.setPreferredSize(new Dimension(280,30));
si = new JRadioButton("Si");
si.addActionListener(this);
no = new JRadioButton("No");
no.addActionListener(this);
group = new ButtonGroup();
group.add(si);
group.add(no);
if(sheet == 1){
this.add(title);
this.add(titleField);
this.add(author);
this.add(authorField);
this.add(editor);
this.add(editorField);
this.add(pages);
this.add(pagesField);
this.add(price);
this.add(priceField);
this.add(inProduzione);
this.add(si);
this.add(no);
this.add(type);
this.add(list);
this.add(aggiungi);
this.add(nummeroLibri);
this.add(message);
}else if (sheet == 2){
this.add(title);
this.add(titleField);
this.add(cerca);
this.add(nummeroLibri);
this.add(message);
}else {
this.add(title);
this.add(titleField);
this.add(togli);
this.add(nummeroLibri);
this.add(message);
}
}
}
答案 0 :(得分:1)
如果我正确理解您的问题,您可能需要使用Singleton Design Pattern。
因此,不是每次都创建新对象,而是创建一个方法getInstance()
,它创建一个新的Object(如果还没有现有的)或返回当前实例。因此,确保您始终使用对象的相同实例。
另一种方法是每次都使用构造函数变量,但是你不能创建一个新的Object,否则你将使用不同的实例。
编辑:澄清我的回答:
将方法getInstance()
添加到Mensola类。简化为:
private static Mensola mensolaInstance;
[...]
public static Mensola getInstance() {
if (mensolaInstance == null)
mensolaInstance = new Mensola(20); // or whatever
return mensolaInstance;
}
您想要访问Mensola对象的任何时间和地点,只需使用Mensola.getInstance()
即可。您不再需要将其作为参数传递。
所以A级需要Mensola,你可以从那里打电话给getInstance()
。之后,B级需要与Mensola合作,您只需拨打getInstance()
。