简体   繁体   中英

Not able to add JTextField to JFrame

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 .

框架有JLabel

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.

具有JLabel和JTextField的框架

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.

Actually you are successful in adding the JTextField . The problem you are experiencing stems from the layout manager which stretches it across the whole frame.

The content pane of JFrame uses a BorderLayout manager by default. (See How to Use BorderLayout )

In my application a always ended up using the MigLayout manager, but first you might want to familiarize yourself with layout managers in general. (See A Visual Guide to Layout Managers )

Your ContentPane has a BorderLayout by default, which only accepts a single element (in the default position - the center). After you added a second element (the JTextField ), it replaced the last one (the JLabel ).

When you add elements to a Container using the single-argument add , you're not specifying in which position you want them, so the layout manager chooses a position at will. OTOH if you specify constraints (using the overloaded add ) then you have more control over where the element will be placed. Check the docs for each layout manager to see how they work and which constraints it supports.

For your current situation, you can use an intermediate JPanel (which has FlowLayout as its default LayoutManager - the simplest IMHO for who is still learning) instead of adding elements directly to the content pane, or simply change its layout to something else.

A JFrame can only have one component (unless you are using that it has a BorderLayout ). The solution is to use a JPanel . You add the objects to a JPanel and then add the JPanel to the JFrame . You need to add import javax.swing.JPanel; too. You can do it something like this:

private static void createandshowGUI()
{
    JFrame frame =new JFrame("HelloSwing");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setBackground(Color.red);
    frame.setSize(200,200);

    JPanel panel = new JPanel(); //Create a JPanel

    JLabel label=new JLabel("New To Java!!");
    panel.add(label); // Add the label to the panel

    JTextField jtf = new JTextField();
    panel.add(jtf); // Add the JTextField to the panel

    frame.getContentPane().add(panel); // Add the panel to the JFrame
    frame.setVisible(true);
}
public static void main(String[] args) {
    createandshowGUI();}

This should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM