我使用 windowbuilder 创建了 3 个 GUI 框架,以按此顺序加载和显示。
- 第一个 GUI (Home.java)
- 第二个图形用户界面和第三个图形用户界面(Results1.java,Results2.java)
显然 Home.java 可以首先加载,但 2 个正在进行的 GUI 与 Home.java 同时加载。我可以验证它,因为我在初始化 GUI 元素的方法中插入了打印语法。在 Home.java 上按下按钮后,我可以显示最后 2 个 GUI,但内容已经加载。因此,当我需要将变量传递给这 2 个 GUI 时,值变为空。有任何想法吗?
按下 Home.Java 按钮。
//Compare Button
btnCompare.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(lbl_filecontainer1.getText() != "No File" && lbl_filecontainer2.getText() != "No File")
{
r1.setFileName1(lbl_filecontainer1.getText());
r1.Launch();
}
else if (lbl_filecontainer1.getText() == "No File" && lbl_filecontainer2.getText() != "No File")
{
JOptionPane.showMessageDialog(null, "No file 1 selected");
}
else if (lbl_filecontainer1.getText() != "No File" && lbl_filecontainer2.getText() == "No File")
{
JOptionPane.showMessageDialog(null, "No file 2 selected");
}
else
JOptionPane.showMessageDialog(null, "No files selected");
}
}
);
结果1.java
public class Results1 {
private JFrame frmFileOverall;
String FileName1;
//Instances
Database db = new Database();
/**
* Launch the application.
*/
public void Launch(String FileName) {
try {
setFileName1(FileName);
Results1 window = new Results1();
window.frmFileOverall.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the application.
*/
public Results1() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmFileOverall = new JFrame();
frmFileOverall.setResizable(false);
frmFileOverall.setTitle("File 1 overall result");
frmFileOverall.setBounds(100, 100, 550, 450);
JLabel lblNewLabel = new JLabel("Number of words: ");
lblNewLabel.setFont(new Font("Verdana", Font.BOLD, 11));
JLabel lblNumberOfSentences = new JLabel("Number of sentences: ");
lblNumberOfSentences.setFont(new Font("Verdana", Font.BOLD, 11));
JLabel label_1 = new JLabel("Number of paragraphs: ");
label_1.setFont(new Font("Verdana", Font.BOLD, 11));
JTextArea txtAreaFile1 = new JTextArea();
txtAreaFile1.setEditable(false);
JLabel wordsContainerFile1 = new JLabel("New label");
JLabel sentencesContainerFile1 = new JLabel("New label");
JLabel paragraphsContainerFile1 = new JLabel("New label");
GroupLayout groupLayout = new GroupLayout(frmFileOverall.getContentPane());
groupLayout.setHorizontalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addContainerGap()
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addComponent(txtAreaFile1, GroupLayout.DEFAULT_SIZE, 514, Short.MAX_VALUE)
.addGroup(groupLayout.createSequentialGroup()
.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 124, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(wordsContainerFile1))
.addGroup(groupLayout.createSequentialGroup()
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addComponent(label_1)
.addComponent(lblNumberOfSentences, GroupLayout.PREFERRED_SIZE, 151, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addComponent(paragraphsContainerFile1)
.addComponent(sentencesContainerFile1))))
.addContainerGap())
);
groupLayout.setVerticalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addContainerGap()
.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(lblNewLabel)
.addComponent(wordsContainerFile1))
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(lblNumberOfSentences)
.addComponent(sentencesContainerFile1))
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(label_1)
.addComponent(paragraphsContainerFile1))
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(txtAreaFile1, GroupLayout.DEFAULT_SIZE, 322, Short.MAX_VALUE)
.addContainerGap())
);
frmFileOverall.getContentPane().setLayout(groupLayout);
System.out.print(getFileName1());
//txtAreaFile1.setText(db.loadFile(getFileName()));
}
public void setFileName1(String fileName)
{
this.FileName1 = fileName;
}
public String getFileName1()
{
return this.FileName1;
}
}