gpt4 book ai didi

java - 将字符串传递给另一个类

转载 作者:行者123 更新时间:2023-11-30 07:56:18 25 4
gpt4 key购买 nike

我正在编写一个应用程序,需要将两个 String 对象从 GUI 获取到 nullObject 类。

我对编程还比较陌生,正在尽力学习。如果您有任何关于如何做得更好的建议,我将非常感激!

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

我想将字符串 jsonTitlejsonDescr 放入另一个类中,以便可以存储它们。最后我会有一些字符串,我需要将它们保存在 JSON 文件中。我需要一种方法来获取这两个字符串,你们有什么建议吗?

最佳答案

埃里克的回答是正确的。只是觉得我应该添加额外的信息。如果您像使用 private 声明其他字段一样声明 jstonTitlejsonDescr,您仍然无法从其他类访问这些字段。为字段编写 getter 并在 GUI 顶部声明它们应该可以解决您的问题。然后只需在其他类中创建一个 GUI 实例并调用该方法即可。

public String getJsonTitle(){
return this.jsonTitle;
}

关于java - 将字符串传递给另一个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32630234/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com