Java 如何将变量从一个GUI类传递到另一个GUI类?
我正在构建一个应用程序,它将输入作为字符串接收,并将它们传递给另一个类,在该类中存储我的方法,以返回int/numbers。在我的主GUI窗口中,我可以将其显示到文本区域。我创建了另一个类,在单击按钮时打开一个新窗口。我可以得到显示它的方法,但我无法让用户输入的任何内容通过,除非新窗口首先打开,然后我按下另一个按钮以获得结果 理想情况下,当我按下“提交弹出”按钮时,新窗口将打开并显示结果 GUI类Java 如何将变量从一个GUI类传递到另一个GUI类?,java,swing,Java,Swing,我正在构建一个应用程序,它将输入作为字符串接收,并将它们传递给另一个类,在该类中存储我的方法,以返回int/numbers。在我的主GUI窗口中,我可以将其显示到文本区域。我创建了另一个类,在单击按钮时打开一个新窗口。我可以得到显示它的方法,但我无法让用户输入的任何内容通过,除非新窗口首先打开,然后我按下另一个按钮以获得结果 理想情况下,当我按下“提交弹出”按钮时,新窗口将打开并显示结果 GUI类 //The declaration of my textArea results are disp
//The declaration of my textArea results are displayed in the main window
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(22, 354, 354, 230);
contentPane.add(scrollPane);
JTextArea psuArea = new JTextArea();
scrollPane.setViewportView(psuArea);
PrintStream printStream = new PrintStream(new CustomOutputStream(psuArea));
System.setOut(printStream);
System.setErr(printStream);
JButton popUp = new JButton("Submit Pop");
popUp.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
pw ok = new pw();
ok.newScreen(in , cpuBrand, cpuLine, gpuBrand, gpuLine, flash, platter, sata, cd, fan);
}
});
//The button that calls my method and passes the variables entered to it.
JButton btnAdvanced = new JButton("Advanced Results");
btnAdvanced.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
pcp.displayC(in , cpuBrand, cpuLine, gpuBrand, gpuLine, flash, platter, sata, cd, fan);
}
});
结果窗口类
package pcbuilder;
import java.awt.EventQueue;
import java.io.PrintStream;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JLabel;
public class pw {
private JFrame framePU;
pccalc pcp = new pccalc("");
/**
* Launch the application.
*/
public void newScreen(String n, String cb, String cl, String gb, String gl, String fs, String ss, String sa, String fq, String cf) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
pw window = new pw();
window.framePU.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public pw() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
framePU = new JFrame();
framePU.setBounds(100, 100, 450, 348);
framePU.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
framePU.getContentPane().setLayout(null);
JLabel lblNewLabel = new JLabel("New label");
lblNewLabel.setBounds(182, 6, 61, 16);
framePU.getContentPane().add(lblNewLabel);
JTextArea psuArea = new JTextArea();
psuArea.setBounds(6, 26, 438, 246);
framePU.getContentPane().add(psuArea);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(22, 354, 354, 230);
PrintStream printStream = new PrintStream(new CustomOutputStream(psuArea));
System.setOut(printStream);
System.setErr(printStream);
}
}
方法类
public void displayC(String n, String cb, String cl, String gb, String gl, String fs, String ss, String sa, String fq, String cf) {
//When called in GUI class total wattage is correct. I may need to modify something in my Results or GUI class to have total wattage display correctly in the popup.
curWatt = cpu + cpuO + mobo + gpu + fastStore + slowStore + store + exStore + fan + sound;
System.out.printf("%nBuild Name: %s", buildName);
System.out.printf("%nCPU - %s %s: %d", cb, cl, cpu);
System.out.printf("%nCPU OC Needed: %d", cpuO);
System.out.printf("%nMotherboard: %d", mobo);
System.out.printf("%nGPU - %s %s: %d", gb, gl, gpu);
System.out.printf("%nSSD/NVME - %s: %d", fs, fastStore);
System.out.printf("%nHDD - %s: %d", ss, slowStore);
System.out.printf("%nUnused Sata/NVME - %s: %d", sa, store);
System.out.printf("%nCD/DVD/BlueRay - %s: %d", fq, exStore);
System.out.printf("%nFans - %s: %d", cf, fan);
System.out.printf("%nOverhead for Sound: %d", sound);
System.out.printf("%nTotal Estimated Wattage: %d%n", curWatt);
}
有什么想法吗
编辑:多亏了这个答案,我才能够让代码正常工作。这是我用来让它工作的乱七八糟的代码
公共类{
test nt = new test();
private JFrame framePU;
JLabel lblNewLabel = new JLabel("New label");
JScrollPane scrollPane_1 = new JScrollPane();
JTextArea psuArea = new JTextArea();
PrintStream printStream = new PrintStream(new CustomOutputStream(psuArea));
JScrollPane scrollPane = new JScrollPane();
pccalc pcp = new pccalc("");
/**
* Launch the application.
*/
public void newScreen(String n, String cb, String cl, String gb, String gl, String fs, String ss, String sa, String fq, String cf) {
EventQueue.invokeLater(new Runnable() {
public void run() {
String in = n;
String cpuBrand = cb;
String cpuLine = cl;
String gpuBrand = gb;
String gpuLine = cl;
String flash = fs;
String platter = ss;
String sata = sa;
String cd = fq;
String fan = cf;
try {
lblNewLabel.setText("A different label");
pcp.buildName(in);
if (pcp.cpuBrand(cb) == true) {
pcp.amdLine(cl);
} else {
pcp.intelLine(cl);
}
if (pcp.gpuBrand(gb) == true) {
pcp.graphAmd(gl);
} else {
pcp.graphNvidia(gl);
}
pcp.driveS(fs);
pcp.driveH(ss);
pcp.driveF(fq);
pcp.driveA(sa);
pcp.displayC(in , cpuBrand, cpuLine, gpuBrand, gpuLine, flash, platter, sata, cd, fan);
framePU.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
仍然有点混乱,但它正在按我想要的方式工作,现在我可以开始考虑如何添加保存功能。您将各种数据传递到
pw
类的新闻屏幕方法中。但是您没有对它做任何事情。更糟糕的是,您当前的设计没有任何用处ul您可以使用它,因为所有GUI组件的声明都隐藏在initialize
方法中,newScreen
无法获取它们
您需要做的第一件事是将GUI组件的声明从initialize
中拉出来,放入pw
类顶层的成员变量中,其他类成员(例如新闻屏幕(…)
方法)可以访问它们:
public class pw {
private JFrame framePU;
private JLabel lblNewLabel = new JLabel("New label");
private JTextArea psuArea = new JTextArea();
private JScrollPane scrollPane = new JScrollPane();
pccalc pcp = new pccalc("");
// ....
private void initialize() {
framePU = new JFrame();
framePU.setBounds(100, 100, 450, 348);
framePU.setDefaultCloseOperation(
JFrame.DISPOSE_ON_CLOSE);
framePU.getContentPane().setLayout(null);
lblNewLabel.setBounds(182, 6, 61, 16);
framePU.getContentPane().add(lblNewLabel);
psuArea.setBounds(6, 26, 438, 246);
framePU.getContentPane().add(psuArea);
scrollPane.setBounds(22, 354, 354, 230);
PrintStream printStream = new PrintStream(new CustomOutputStream(psuArea));
System.setOut(printStream);
System.setErr(printStream);
}
}
现在,newScreen
实际上可以对传递给它的数据做一些有用的事情——即,通过pw
窗口的组件显示它:
public class pw {
private JFrame framePU;
private JLabel lblNewLabel = new JLabel("New label");
private JTextArea psuArea = new JTextArea();
private JScrollPane scrollPane = new JScrollPane();
//...
public void newScreen(String n, String cb, String cl, String gb, String gl, String fs, String ss, String sa, String fq, String cf) {
final String txt =
String.join(" ", "n=",n,"cb=",cb,"cl=",cl/* etc. */);
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
// We're already *in* a pw, so no need for a new one...
// pw window = new pw();
// window.framePU.setVisible(true);
lblNewLabel.setText("A different label");
psuArea.setText(txt);
framePU.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
在pw
类中的initialize
方法上,添加framePU.setSize(300300);
好的,这对GUI有很大的帮助,但对GUI来说还是新的,我在Eclipse中使用GUI构建器,所以仍然在学习细节。我不知道你可以这样传递数据。