In: Computer Science
I need to add variables such as name, address and phone into a Jpanel in the constructor called AddressBook. I then need to add that panel into a Jframe and call it in the main method how would i do that in java.
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Sample {
JFrame frame;
Sample(){
//creating jpanel
JPanel panel = new JPanel();
frame = new JFrame("JFrame Example");
panel.setLayout(null);
//creating labels
final JLabel label3 = new JLabel("Name:
");
label3.setBounds(200, 15, 500, 30);
JLabel label1 = new JLabel("Address: ");
label1.setBounds(200, 50, 300, 30);
final JTextField adult = new
JTextField(10);
adult.setBounds(400, 50, 100, 30);
//creating labels
JLabel label2 = new JLabel("Phone : ");
label2.setBounds(200, 100, 250, 30);
// password field
final JTextField children = new
JTextField(10);
children.setBounds(400, 100, 100, 30);
//adding to Jpanel
panel.add(label1);
panel.add(label2);
panel.add(label3);
panel.add(adult);
panel.add(children);
//adding JPanel to frame
frame.add(panel);
frame.setSize(600, 300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String s[]) {
new
Sample();
}
}
Note : Please comment below if you have concerns. I am here to help you
If you like my answer please rate and help me it is very Imp for me