简体   繁体   中英

How add a jtextfield to a jpanel that has paint component?

I'm making a game and I want to add a JTextField to a JPanel that has Paint Component. I repaint the JPanel every 16 milliseconds.

I add() the textfield to the panel but it show up only for a single frame when i click on it. Then I tried to repaint() the textfield but now it is flashing.

public class Screen extends JPanel {


    public Screen() {
        JTextField txt = new JTextField();
        txt.setBounds(10, 10, 300, 50);
        this.add(txt);
    }
    @Override
    public void paint(Graphics g) {
        Graphics2D g2D = (Graphics2D) g;

        g2D.setColor(Color.BLACK);
        g2D.fillRect(0, 0, this.getWidth(), this.getHeight());

        g2D.setColor(Color.WHITE);
        g2D.fillRect(0, 0, this.getWidth(), 20);

        txt.repaint();
    }
}

I want to show the textfield on the top of the panel

JTextField txt = new JTextField();

When you create a JTextField you should use code like:

JTextField txt = new JTextField(10);

Now the text field can calculate its own preferred size.

//txt.setBounds(10, 10, 300, 50);

Don't use setBounds() to give a component a size. Again each Swing component is responsible for determining its own preferred size. Then the layout manager will set the size/location of the component on the panel.

//public void paint(Graphics g) {
public void paintComponent(Graphics g) 
{
    super.paintComponent(g);

    // add custom painting here
}

Don't override paint(). Custom painting is done by overriding the paintComponent() method. And the first statement in the method should be super.paintComopnent(g)

//g2D.setColor(Color.BLACK);
//g2D.fillRect(0, 0, this.getWidth(), this.getHeight());

Don't paint the background of the panel. That is the job of the panel and that is why you need to super.paintComponent(), to make sure the background is painted.

Then in the constructor of your JPanel class you simply use setBackground( Color.BLACK )

//txt.repaint();

Don't ever invoke repaint() on any component in a painting method.

Read the section from the Swing tutorial on Custom Painting for working examples to get you started. Use the demo code as the starting point for you program. Then you simply add a JTextField to the panel, so it will be a single line of code that is needed to display the text field.

It seems like you want to have a JTextField on a black panel. You don't need to set the colour of the panel every time in paint() method. Instead add this to the constructor:

public Screen() {
    setOpaque(true);
    setBackground(Color.BLACK);
    //... 
}

and remove paint() method.

Also, if you want to use absolute positioning with setBounds() method then you should set the layout to null setLayout(null) in constructor. If you use absolute positioning you will also need to specify the size of the panel explicitly. However, I would still suggest you use a layout manager that takes care of panel sizing as well. See this post for more information about absolute positioning.

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