簡體   English   中英

如何將數據從JTextField顯示到JTextArea中?

[英]How do I display the data from a JTextField into a JTextArea?

如何將數據從JTextField顯示到JTextArea

我試圖從四個JTextField獲取輸入,並在單擊按鈕時將它們顯示在JTextArea 我有ActionListener在工作。 我只是不確定如何獲取輸入到JTextArea

我的JTextArea稱為“ ShowActions”。 而且我在JTextArea出現了一些代碼行。 但是它們不是我想要的輸入那么糟糕.....

有人能指出我正確的方向嗎?

private JPanel JPanel1 (){

JP1 = new JPanel();

//Style JP1
JP1.setLayout(new GridLayout(8, 2));
JP1.setBackground(Color.RED);
JP1.setBorder(new EmptyBorder(20, 20, 20, 20));

//Make bits and name them       
heading1 = new JLabel ("Add Landlord");
Font font = new Font("Serif", Font.ITALIC + Font.BOLD, 28);
heading1.setFont(font);
spacer1 = new JLabel ("           ");
LLNameT = new JLabel ("Enter Landlord name");
LLName = new JTextField(30);
LLAddressT = new JLabel ("Enter Landlord Address ");
LLAddress = new JTextField(40);
LLPhoneT = new JLabel ("Enter Landlod Phone No.");
LLPhone = new JTextField(10);
LLbankDeetsT = new JLabel ("Enter Landlod Bank details");
LLbankDeets = new JTextField(10);
sub1 = new JButton("Submit"); 

//Add bits to panel 
JP1.add(heading1);
JP1.add(spacer1);
JP1.add(LLNameT); 
JP1.add(LLName);
JP1.add(LLAddressT);
JP1.add(LLAddress); 
JP1.add(LLPhoneT);
JP1.add(LLPhone); 
JP1.add(LLbankDeetsT );
JP1.add(LLbankDeets); 
JP1.add(sub1);

//Set Action Listener
event1  JP1sub1 = new event1();
sub1.addActionListener(JP1sub1);

return(JP1);
}   

//Activate ActionListener
public class event1 implements ActionListener{

     public void actionPerformed(ActionEvent JP1sub1){

     ShowActions.setText(LLName + "\n" + LLAddress + "\n" + LLPhone + "\n" + LLbankDeets);
}

}

您非常接近,您需要做一件事,那就是通過調用getText()從標簽和文本字段獲取tekst。

 public class event1 implements ActionListener{

     public void actionPerformed(ActionEvent JP1sub1){
         ShowActions.setText(LLName.getText() + "\n" + LLAddress.getText() + "\n" + LLPhone.getText() + "\n" + LLbankDeets.getText());
     }
 }

希望這可以幫助 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM