简体   繁体   中英

How to place a JTextField without it generating more fields every time I minimize the window in Java?

My code places a JTextField in a specific randomized location, but it also creates a generic JTextField in the top of the Frame, which I don't want it to do. When I minimize it and then re maximize it it creates a second JTextField right next to the first one, and again and again. It also deletes any thing that has been typed in the field. Note that when I place the JTextField in a random location I want it to stay there when I minimize the window. Code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import java.util.Random;

@SuppressWarnings("serial")
public class AC extends JPanel implements ActionListener {

    JTextField text = new JTextField(10);
    int x;
    int y;

    public AC() {
        Random r = new Random();
        x = r.nextInt(500);
        y = r.nextInt(500);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        JTextField text = new JTextField(10);
        text.setBounds(x, y, 150, 20);
        this.add(text);
    }

    static void createandshowGUI() {
        JFrame frame = new JFrame("AC");
        frame.getContentPane().setBackground(Color.white);
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        frame.setSize(dim.width, dim.height);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new AC());
        frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createandshowGUI();
            }
        });
    }

    public void actionPerformed(ActionEvent e) {
    }

}

Painting methods are for painting, not for creating components.

You should never create a component in the paintComponent() method. Every time Swing determines the component needs to be repainted the paintComponent() method is invoked so a new component will be created.

Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
frame.setSize(dim.width, dim.height);   

There is no need for the above code since you are using the setExtendedState(...) method to set the size of the frame.

Why would you put a text field at a random location? Sounds like a strange design. Swing was designed to be used with layout managers. Read the section from the Swing tutorial on Layout Managers for more information.

Whenever you minimize and re-maximize the window, the paintComponent() method is called. You are creating new JTextField inside paintComponent() method as a result of which a new JTextField is created whenever you remaximize the window.

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