[英]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.