2016-11-01 59 views
1

我做了class a使用文本框输入预设值,然后打印出值

class a 
int account 
string first name 
string last name 
int balance 

然后,我创建一个textfieldclass b

class b 
textField_4 = new JTextField(); 
    textField_4.setBounds(210, 123, 150, 30); 
    getContentPane().add(textField_4); 
    textField_4.setColumns(10); 

    JButton btnNewButton = new JButton("Enter"); 
    btnNewButton.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent arg0) { 
       PrintStream diskWriter = new PrintStream(""); 
       diskWriter.print("Account: "); 
       diskWriter.println(account); 

我怎样写值在class a account进入class b textfield

+0

你想在哪里打印值? –

+0

磁盘上的一些文件,我将添加后 –

回答

0

尝试这样的事情,从文本字段获取文本,并打印成文本文件:

更新: 第一类:

public class ClassB { 

int account = 3000; 
String firstname = "Coder"; 
String lastname = "ACJHP"; 
int balance = 300; 

public String getDatas() { 
    return new String(account + firstname + lastname + balance); 
    } 
} 

这是另一个类:

textField = new JTextField(); 
    ClassB classB = new ClassB(); //INITIALIZING FIRST CLASS 
    textField.setText(classB.getDatas());//THERE GETTING THE TEXT AND ADDING TO TEXTFIELD 
    textField.setBounds(97, 28, 151, 42); 
    contentPane.add(textField); 
    textField.setColumns(10); 

    JButton btnPrintIt = new JButton("Print it!"); 
    btnPrintIt.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      String text = textField.getText(); 
      if(!text.equals(null) || text.length() > 0){ 


       File file = new File("text.txt");     
       PrintStream printStream; 
       try { 
        printStream = new PrintStream(file); 
        printStream.println(text); 

       } catch (FileNotFoundException e1) { 
        e1.printStackTrace(); 
       } 

      } 
     } 
    }); 
+0

不是我的意思。我的意思是我的“int account”变量在一个类中,我想把它放在我的textField中,然后将其打印到一个文件中。感谢您的帮助tho –

+0

看到我的更新.. –

+0

多一个问题,我可以键入而不是将预设值置于文本字段中,但仍保留“int”或“字符串”类型? –

0

所以你的“帐户”变量在不同的类,并且希望把它放在你的文本字段?

那么你可以宣布你的帐户变量public int account;,并以调用它,你将不得不使用class_b variable = new class_b();,并把它放在你的文本字段,你可以使用textField_4.setText(variable.account);

希望我帮助!

+0

我试一下,谢谢 –

+0

我认为它的假设是class_变量=新class_a(); ? –

+0

不是那样的 –