I m writing a program for my company that references an excel file for project information. I ve organized the program into 4 JPanels, each in it s own class. i.e. Panel_1, Panel_2, etc. I have a button in panel_1 that allows the user to select the project info file and that works just fine. what I want to do is have that button apply the info from the project to the JLabels in the other JPanels 2, 3, & 4.
UI等级
public class UI extends JFrame implements ActionListener {
//Variables declared outside constructor, and to be used in multiple Panels
static String JobNo = "";
static String JobName = "";
static String Attention = "";
static String Contractor = "";
public UI() {
//Define JFrame
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().setLayout(new GroupLayout(this.getContentPane()));
this.setSize(FW + 36, FH);
this.getContentPane().setBackground(col1);
//Add panels to JFrame
Panel_1 panel1 = new Panel_1();
this.add(panel1);
Panel_2 panel2 = new Panel_2();
this.add(panel2);
Panel_3 panel3 = new Panel_3();
this.add(panel3);
Panel_4 panel4 = new Panel_4();
this.add(panel4);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
}
小组1人
public class Panel_1 extends JPanel implements ActionListener{
Panel_1() {
//Define Panel_1
this.setBounds(UI.offSet, UI.offSet, UI.p1W, UI.p1H);
this.setBackground(UI.col2);
this.setLayout(new GroupLayout(this));
JLabel title = new JLabel("Please select Project Folder:");
title.setBounds(20, 10, 300, 15);
title.setForeground(UI.colText);
this.add(title);
//Text field to display project file location
UI.field1 = new JTextField();
UI.field1.setBounds(20, 35, 570, 20);
UI.field1.setBackground(UI.colTF);
UI.field1.setForeground(UI.colText);
UI.field1.setText(UI.sDefPath);
UI.field1.setCaretColor(UI.colText);
UI.field1.setBorder(BorderFactory.createLoweredBevelBorder());
UI.field1.addActionListener(this);
this.add(UI.field1);
//Browse for project file
UI.butBrowse1 = new JButton("Browse");
UI.butBrowse1.setBounds(600, 35, 80, 20);
UI.butBrowse1.setBackground(UI.colBut);
UI.butBrowse1.setForeground(UI.colBT);
UI.butBrowse1.setBorder(BorderFactory.createEmptyBorder());
UI.butBrowse1.setFocusable(false);
UI.butBrowse1.addActionListener(this);
this.add(UI.butBrowse1);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getSource()==UI.butBrowse1) {
//Choose project file location
JFileChooser fileChoose1 = new JFileChooser(UI.sDefPath);
fileChoose1.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
Action details = fileChoose1.getActionMap().get("viewTypeDetails");
details.actionPerformed(null);
fileChoose1.setPreferredSize(new Dimension(500, 600));
fileChoose1.showOpenDialog(null);
UI.sPath = fileChoose1.getSelectedFile().getAbsolutePath();
//Assign file paths from selected folder
UI.field1.setText(UI.sPath);
UI.sPIPath = UI.sPath + UI.sPIadd;
UI.sPDFPath = UI.sPath + UI.sPDFadd;
UI.sSubPath = UI.sPath + UI.sSubadd;
//Test assigned file paths
System.out.println(UI.sPIPath);
System.out.println(UI.sPDFPath);
System.out.println(UI.sSubPath);
//Assigning data from xlsx
//These are the variable that I need to display in Panel_4
UI.JobNo = XLSX_Reader.XLSX_Reader(UI.sPIPath, 0, 1);
UI.JobName = XLSX_Reader.XLSX_Reader(UI.sPIPath, 1, 1) + "
" + XLSX_Reader.XLSX_Reader(UI.sPIPath, 2, 1);
UI.Contractor = XLSX_Reader.XLSX_Reader(UI.sPIPath, 4, 1);
UI.Attention = XLSX_Reader.XLSX_Reader(UI.sPIPath, 8, 1);
//testing xlsx reader
System.out.println(UI.JobNo);
System.out.println(UI.JobName);
System.out.println(UI.Contractor);
System.out.println(UI.Attention);
}
}
小组4级
public class Panel_4 extends JPanel implements ActionListener{
Panel_4() {
//Define Panel_4
this.setBounds((UI.offSet * 2) + UI.p1W, UI.offSet, UI.p4W, UI.p4H);
this.setBackground(UI.col2);
this.setLayout(new GroupLayout(this));
//Doesn t update when Panel_1 assigns value to UI.Contractor
JLabel to = new JLabel("To: " + UI.Contractor);
to.setBounds(50, 190, 200, 30);
to.setForeground(UI.colText);
this.add(to);
//Doesn t update when Panel_1 assigns value to UI.JobNo
JLabel Job = new JLabel("Job No. " + UI.JobNo);
Job.setBounds(500, 110, 100, 15);
Job.setForeground(UI.colText);
this.add(Job);
//Doesn t update when Panel_1 assigns value to UI.Attention
JLabel Att = new JLabel("Attention " + UI.Attention);
Att.setBounds(350, 150, 100, 15);
Att.setForeground(UI.colText);
this.add(Att);
//Doesn t update when Panel_1 assigns value to UI.JobName
JLabel Re = new JLabel("Re: " + UI.JobName);
Re.setBounds(350, 190, 100, 15);
Re.setForeground(UI.colText);
this.add(Re);
如果他们是同一类的,我通常会做这样的事情:
String pName = "Sam";
JLabel name = new JLabel(pName);
name.setBounds(...);
this.add(name);
JButton button = new JButton();
button.addActionListener(this);
this.add(button);
}
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource()==UI.butBrowse1) {
pName = "Dave";
name.setText(pName);
但是,我不知道这能否跨越各个班级。