简体   繁体   English

每次我最小化Java中的窗口时,如何放置JTextField而不使其生成更多字段?

[英]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. 我的代码在特定的随机位置放置了一个JTextField ,但它还在Frame的顶部创建了一个通用的JTextField ,我不希望这样做。 When I minimize it and then re maximize it it creates a second JTextField right next to the first one, and again and again. 当我最小化它,然后重新最大化它时,它会在第一个JTextField旁边并一次又一次地创建第二个JTextField 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. 请注意,当我将JTextField放置在随机位置时,我希望在最小化窗口时将其保留在该位置。 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. 您永远不要在paintComponent()方法中创建组件。 Every time Swing determines the component needs to be repainted the paintComponent() method is invoked so a new component will be created. 每次Swing确定需要重绘组件时,都会调用paintComponent()方法,以便创建一个新组件。

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. 由于您正在使用setExtendedState(...)方法来设置框架的大小,因此不需要上面的代码。

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. Swing旨在与布局管理器一起使用。 Read the section from the Swing tutorial on Layout Managers for more information. 阅读有关布局管理器的Swing教程中的部分,以获取更多信息。

Whenever you minimize and re-maximize the window, the paintComponent() method is called. 每当您最小化和重新最大化窗口时,都会调用paintComponent()方法。 You are creating new JTextField inside paintComponent() method as a result of which a new JTextField is created whenever you remaximize the window. 您正在内部paintComponent()方法中创建new JTextField ,因此,只要您最大化窗口,就会创建一个新的JTextField

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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