java按顺序显示/加载gui框架
我已经使用windowbuilder创建了3个GUI框架,以便按此顺序加载和显示
- 第一个GUI(Home.java)
- 第二届图形用户界面&;第三个GUI(Results1.java、Results2.java)
显然是在家里。可以首先加载java,但两个正在进行的GUI会与主GUI同时加载。JAVA我可以验证它,因为我在初始化GUI元素的方法中插入了打印语法。在家里按下按钮后,我可以显示最后2个GUI。java,但内容已加载。因此,当我需要向这两个GUI传递一个变量时,该值将变为null。有什么想法吗
家。按下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。爪哇
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;
}
}
# 1 楼答案
与其只在按钮按下时显示已经创建的结果GUI,为什么不只在按下按钮时创建结果GUI,即从主JButton的ActionListener调用结果创建代码?因此,再次总结一下,关键似乎是您何时以及如何创建结果GUI,您似乎是在程序启动时这样做的,您应该在响应用户事件时这样做
请注意,这里的许多人,包括我自己,都建议不要向用户拍摄不同的窗口,而只使用一个JFrame作为GUI视图,并使用CardLayout更改此视图中的JPanel。此外,这里的许多人建议在使用诸如WindowBuilder之类的代码生成工具之前,先学习手工编写SwingGUI的代码。通过这种方式,如果以后使用代码生成工具,您将更好地了解它们在幕后的作用,并使使用它们变得更容易
编辑1
请注意,我不确定您所说的:
“使用普通类创建gui”是什么意思?他们都是普通班
编辑2
你的代码有点精神分裂,因为你有一个Results1对象创建了一个新的Results1对象,这就是你搞砸的地方。让我们调用第一个Results1对象r1A和第二个r1B。在我看来,这就是正在发生的事情:
我的建议是:
编辑3
关于您的代码:
您不希望使用
!=
或==
比较字符串,因为这将检查一个字符串对象是否与另一个字符串对象相同,这可能会导致误导性结果。相反,您需要知道两个字符串对象是否包含相同的文本,为此,您需要使用字符串方法equals(...)
或equalsIgnoreCase(...)
: