I hope this helps...
TextGenerator.java
package textgenerator;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JTextPane;
public class TextGenerator {
JFrame frame;
JPanel panel;
JTextPane textPane;
JLabel namel;
JLabel agel;
JTextField namef;
JTextField agef;
JButton button;
public TextGenerator() {
frame = new JFrame("My Frame");//Construct the frame
frame.setBounds(200, 100, 1000, 500);//set the size and position
frame.setLayout(new GridLayout(1, 2));//set layout with 1 row and 2 columns
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);//restrict the resixing of the frame
panel = new JPanel();//create a panel
panel.setLayout(null);
textPane = new JTextPane();//create a textpane
textPane.setEditable(false);
frame.add(panel);//add panel to the frame
frame.add(textPane);//add textpane to the frame
//create labels and textfields and add them to the panel
namel = new JLabel("Name : ");
namel.setBounds(20, 200, 150, 20);
agel = new JLabel("Age : ");
agel.setBounds(20, 250, 150, 20);
namef = new JTextField();
namef.setBounds(220, 200, 150, 20);
agef = new JTextField();
agef.setBounds(220, 250, 150, 20);
panel.add(namel);
panel.add(agel);
panel.add(namef);
panel.add(agef);
//create button and add it to the panel
button = new JButton("Done !");
button.setBounds(350, 400, 100, 20);
panel.add(button);
//set the required text to the textfield on button click
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
textPane.setText("Hello !\n"
+ "My name is " + namef.getText() + ", and I'm " + agef.getText() + ".\n"
+ "How are you ?");
}
});
frame.setVisible(true);//make the frame visible
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new TextGenerator();
}
});
}
}
Output looks like this