Create one Button on our window.
Go to its properties and change the text on it to Copy
Change its name to b1 ( read the part 1 on how to add button)
Add one Label
Create one Lable by selecting one JLable from the components
Go to its properties and change the text on its to Output
Change its name to l1
Now all our components are ready and we will add event to our button( b1 ) to copy the text ( from t1 ) and place the same in our Lable ( l1 )
Doble click the button on our design Panel.
On the source panel check that this code is written for the event or ActionListener()
We used getText() to read the text written at textfield t1.
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String str=t1.getText(); // read the value of text field.
l1.setText(str); // add the text to Label
}
});
If you are getting any error saying l1 cannot be resolved. Then go to Class declaration and ensure that our three components are declared.
public class Basic {
private JFrame frame;
private JTextField t1;
private JLabel l1;
.........
.......
Now our output is here .
Displaying Message box
We can show the message box like this.
JOptionPane.showMessageDialog(null,"welcome to plus2net");