简体   繁体   中英

Java: JTextField won't appear

public class HandleUI {     
    public static void setUpUI(){   
        JPanel jPan = new JPanel();
        FlowLayout flow = new FlowLayout();
        jPan.setLayout(flow);           
        txtFld = new JTextField();
        txtFld.setSize(550,5);
        jPan.add(txtFld);
        jPan.setSize(10,200);
        MainClass.mainFrame.add(jPan);
        int gapX = MainClass.mainFrame.getX()-(txtFld.getX()/2);
    }   
    //Instance variables.
    public static JTextField txtFld;
    public JButton [] buttons;
}   
public class MainClass { 
    public static void main (String [] args){           
        int frameX = Constants.FRAME_WIDTH;
        int frameY = Constants.FRAME_HEIGHT;
        mainFrame = new JFrame();
        mainFrame.setSize(frameX,frameY);
        mainFrame.setResizable(false);
        mainFrame.setVisible(true);
        HandleUI.setUpUI(); 
    }
    //Instance variables
    public static JFrame mainFrame;
}

It's supposed to show JTextField , but as you might have guessed - JFrame shows nothing. I didn't type in imports on purpose, but they are all there. I can't find the problem. Can anyone help?

1.) Simply write:

JTextField tField = new JTextField(10);

Here In the constructor you are passing the number of columns, which is sufficient for a layout like FlowLayout to set the size of the JTextField

2.) The line mainFrame.setVisible(true); must be the last line of the main method. You need to put the code at main() method, inside SwingUtilities.invokeLater(...) thingy.

3.) Instead of setting size on the JFrame use JFrame.pack() , to set the window to the preferred size.

4.) Creation of unnecessary static members is a design flaw. Try to keep yourself away from such thingies.

5.) Read a bit about Concurrency in Swing

One Example Program for help( Use the order of lines as specified in this answer ):

import java.awt.*;
import javax.swing.*;

public class Example {

    private void displayGUI() {
        JFrame frame = new JFrame("Example Demo");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        JPanel contentPane = new JPanel();
        JTextField tField = new JTextField(10);
        contentPane.add(tField);

        frame.setContentPane(contentPane);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                new Example().displayGUI();
            }           
        };
        EventQueue.invokeLater(runnable);
    }
}

You have to call setVisible(true) on your JFrame AFTER having initialised your UI.

Simply pulling the following line:

HandleUI.setUpUI(); 

... right before:

mainFrame.setVisible(true);

... will do the trick.

As a side note, I'd like to point out that setting the size of your text field won't really work like you did. You probably would use setPreferredSize(Dimension) instead. Or, even better, organise your UI only using Layouts and not manually setting any component's size.

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