繁体   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