如何从一个GUI类传递到另一个GUI类中获取变量?

问题描述

我正在构建一个应用程序,该应用程序将输入作为字符串接收,并将它们传递给存储我的方法以返回为int / numbers的另一个类。在主GUI窗口中,我可以将其显示为textArea。我创建另一个类,当单击一个按钮时,该类将打开一个新窗口。我可以获得显示它的方法,但是除非首先打开新窗口,然后按另一个按钮显示结果,否则无法传递用户输入的任何内容

理想情况下,我想当我按下“提交弹出”按钮时,会打开新窗口并显示结果。

GUI类

//The declaration of my textArea results are displayed in the main window
                JScrollPane scrollPane = new JScrollPane();
        scrollPane.setBounds(22,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,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,230);
        
        PrintStream printStream = new PrintStream(new CustomOutputStream(psuArea));
        System.setout(printStream);
        System.setErr(printStream);
    }
}

方法

public void displayC(String n,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);
        
    }

有什么想法吗?

编辑:多亏了答案,我得以使代码正常工作。这是我用来使其正常工作的凌乱代码

公共类pw {

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 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,fan);
                framePU.setVisible(true);
                
            } catch (Exception e) {
                e.printstacktrace();
            }
        }
    });
}

仍然有些混乱,但是它可以按照我想要的方式工作,现在我可以继续研究如何添加保存功能了。

解决方法

您将各种数据传递到newScreen类的pw方法中。但是您没有做任何事情。更糟糕的是,对于您当前的设计, 确实没有任何用处,因为所有GUI组件的声明都隐藏在initialize方法内部,其中newScreen无法到达他们。

您需要做的第一件事是将GUI组件的声明从initialize中拉出,并放入pw类顶层的成员变量中,其他类成员(例如{ {1}}方法)可以访问它们:

newScreen(...)

现在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,230); PrintStream printStream = new PrintStream(new CustomOutputStream(psuArea)); System.setOut(printStream); System.setErr(printStream); } } 实际上可以对传递给它的数据做一些有用的事情-即,将其显示在newScreen窗口的组件中:

pw