I am not able to add JTextField
to JFrame
. My JFrame
contains a JLabel
and a JTextField
.
First, i have added the JLabel
, and it is working. Here is the code.
private static void createandshowGUI()
{
JFrame frame =new JFrame("HelloSwing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setBackground(Color.red);
frame.setSize(200,200);
JLabel label=new JLabel("New To Java!!");
frame.getContentPane().add(label);
frame.setVisible(true);
}
public static void main(String[] args) {
createandshowGUI();} //and it shows the output like below .
Then i added JTextField .
JLabel label=new JLabel("New To Java!!");
frame.getContentPane().add(label);
JTextField jtf=new JTextField();
frame.getContentPane().add(jtf);
frame.setVisible(true);
But then it shows output like this.
Please somebody help me on this issue.Can i add more than one component to JFrame?As i am new to Java, i am having a confusion between frame,ContentPane and Layouts.