[英]Pass a String to another class
我正在编写一个应用程序,我需要从GUI到nullObject
类中获取两个String
对象。
我是编程的新手,正在努力学习。 如果您有任何技巧可以改善这个问题,我将非常感激!
我的GUI类:
package com.giuly.jsoncreate;
public class GUI {
private JFrame startFrame;
private JFrame chkFrame;
private JFrame osFrame;
private JFrame appVFrame;
private JPanel controlPanel;
private JButton nextPage;
private JButton cancel;
private JButton save;
public GUI() {
generateGUI();
}
public static void main(String[]args) {
GUI gui = new GUI();
}
public void generateGUI() {
//Creation of the First Frame
startFrame = new JFrame("JSCON Creator");
startFrame.setSize(1000, 700);
startFrame.setLayout(new FlowLayout());
startFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
//Panel Creation
controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());
//Button Creation
cancel = new JButton("Cancel");
cancel.setSize(100, 100);
cancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
nextPage = new JButton("Next");
nextPage.setSize(100, 100);
nextPage.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
startFrame.setVisible(false);
showText();
}
});
startFrame.add(controlPanel);
startFrame.add(cancel);
startFrame.add(nextPage);
startFrame.setVisible(true);
startFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public void showText() {
JFrame textFrame = new JFrame();
textFrame.setSize(1000, 700);
textFrame.setTitle("Text");
textFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
JPanel textPanel = new JPanel();
JLabel titleLabel = new JLabel("Title");
textPanel.add(titleLabel);
JLabel descrLabel = new JLabel("Description");
JTextField tfTitle = new JTextField("",15);
tfTitle.setForeground(Color.BLACK);
tfTitle.setBackground(Color.WHITE);
JTextField tfDescr = new JTextField("",30);
tfDescr.setForeground(Color.BLACK);
tfDescr.setBackground(Color.WHITE);
textPanel.add(tfTitle);
textPanel.add(descrLabel);
textPanel.add(tfDescr);
JButton buttonOK = new JButton("OK");
textPanel.add(buttonOK);
buttonOK.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String jsonTitle = tfTitle.getText();
String jsonDescr = tfDescr.getText();
System.exit(0);
}
});
textFrame.add(textPanel);
textFrame.setVisible(true);
}
我想将Strings jsonTitle
和jsonDescr
放入另一个类,以便我可以存储它们。 最后,我将有一些字符串,并且需要将它们保存在JSON文件中。 我需要一种获取这两个字符串的方法,你们有什么建议?
埃里克的回答是正确的。 只是想我应该添加其他信息。 如果像其他字段一样使用private
声明jstonTitle
和jsonDescr
,则仍然无法从另一个类访问这些字段。 编写字段的getter并在GUI
顶部声明它们应该可以解决您的问题。 然后,只需在其他类中创建GUI
实例并调用该方法即可。
public String getJsonTitle(){
return this.jsonTitle;
}
您在actionPerformed()
方法中声明了jstonTitle
和jsonDescr
。 这意味着,一旦actionPerformed()
退出,您将丢失这些变量。 您需要在一个封闭的上下文中声明它们。 例如,您可以使它们成为GUI
类上的字段。 仍然在actionPerformed()
分配它们,但是在要声明startFrame
, chkFrame
等的GUI
顶部声明它们。
这将使您能够从GUI
任何位置访问这些值。
哦,顺便说一句,摆脱System.exit(0);
。 (您是否真的尝试过运行程序?)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.