怎样将一个GUI类中的变量传递到另一个类中?

huangapple go评论56阅读模式
英文:

How to get variables from one GUI class passed into another?

问题

I'm building an application which takes in inputs as strings and passes them to another class where my methods are stored to return as int/numbers. In my main GUI window I am able to display this to a textArea. I create another class that opens a new window when a button is clicked. I can get the method to display it, but am unable to have anything the user entered passed unless the new window is open first then I press another button for the result.

Ideally, what I want to happen is that when I press my 'Submit Pop' button, the new window opens and displays the result.

// GUI class
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);
    }
});

// Result window class
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();
            }
        }
    });
}

// Method class
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);
}

It seems like you want to open a new window and display the result when the 'Submit Pop' button is pressed. The code structure you've provided looks mostly correct. However, ensure that the variables in, cpuBrand, cpuLine, gpuBrand, gpuLine, flash, platter, sata, cd, and fan are properly initialized before calling the newScreen method.

Your code should work as expected, but make sure you have proper variable values and initialization to ensure the expected behavior.

英文:

I'm building an application which takes in inputs as strings and passes them to another class where my methods are stored to return as int/numbers. In my main GUI window I am able to display this to a textArea. I create another class that opens a new window when a button is clicked. I can get the method to display it, but am unable to have anything the user entered passed unless the new window is open first then I press another button for the result.

Ideally, what I want to happen is that when I press my 'Submit Pop' button, the new window opens and displays the result.

GUI class

//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);
}
});

Result window class

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);
}
}

Method class

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);
}

Any ideas?

Edit: Thanks to the answer, I was able to get the code working. Here is the messy code I used to get it working.

public class 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 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();
}
}
});
}

Still a bit messy, but it's working the way I want and now I can move onto figuring out how to add a save feature.

答案1

得分: 1

你将各种数据传递给了pw类的newScreen方法。但是你对其中的任何数据都没有进行任何处理。更糟糕的是,根据你当前的设计,实际上无法对其进行任何有用的处理,因为所有GUI组件的声明都隐藏在initialize方法中,而newScreen无法访问这些组件。

首先,你需要将GUI组件的声明从initialize方法中提取出来,放到pw类的顶层成员变量中,这样其他类成员(比如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, 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 {
                    lblNewLabel.setText("A different label");
                    psuArea.setText(txt);
                    framePU.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
英文:

You passed all kinds of data into the newScreen method of the pw class. But you didn't do anything with any of it. Worse yet, with your current design, there's really nothing useful you can do with it, because the declarations of all your GUI components are hidden inside the initialize method, where newScreen cannot get at them.

First thing you need to do is pull the declarations of your GUI components out of initialize and into member variables at the top level of the pw class, where other class members (such as the newScreen(...) method) can access them:

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);
}
}

Now newScreen can actually do something useful with the data that was passed to it---namely, get it displayed by the components of the pw window:

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();
}
}
});
}
}

huangapple
  • 本文由 发表于 2020年8月15日 12:51:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/63422689.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定