windowbuilderを使用して3つのGUIフレームを作成し、この順序でロードおよび表示します。
- 1番目のGUI(Home.java)
- 2番目のGUIと3番目のGUI(Results1.java、Results2.java)
明らかに、Home.javaを最初にロードできますが、後続の2つのGUIはHome.javaと同時にロードされます。GUIの要素を初期化するメソッド内に印刷構文を挿入したので、それを確認できます。Home.javaのボタンを押した後、最後の2つのGUIを表示できますが、コンテンツはすでにロードされています。したがって、これら2つのGUIに変数を渡す必要がある場合、値はnullになります。何か案は?
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");
}
}
);
Results1.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;
}
}