简体   繁体   中英

JComboBox and JTextField disappearing when button is pressed

I'm having some trouble with a simple application I'm creating to count the number of clicks per second. The code is as follows:

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.Border;

public class CPSFrame implements ActionListener {

    JFrame frame;
    JPanel borderPanel, settings;
    JButton click;
    String[] timesList = {"5","10","15","20","25","30"};
    JComboBox times;
    JTextField showCps;


    public CPSFrame() {

    }


    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                CPSFrame c = new CPSFrame();
                c.drawGui();
            }
        });
    }

    public void drawGui() {

        frame = new JFrame();
        borderPanel = new JPanel();
        settings = new JPanel();
        click = new JButton("Click me!");
        times = new JComboBox(timesList);
        showCps = new JTextField();

        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("CPS Calculator");
        {
            borderPanel.setLayout(new BorderLayout(0, 0));
            frame.add(borderPanel);
            {
                borderPanel.add(click);

                settings.setLayout(new GridLayout(1, 0, 0, 0));
                borderPanel.add(settings, BorderLayout.SOUTH);
                {
                    settings.add(times);

                    settings.add(showCps);

                }
            }
        }

        frame.pack();
        frame.setVisible(true);
    }


    @Override
    public void actionPerformed(ActionEvent evt) {

    }
}

For some reason, if I resize the JFrame and press click , both times and showCPS disappear. I have a feeling it has something to do with the way I have the BorderLayout set up, but I'm not sure.

Here is a video of what is happening.

edit: updated the code and added a video of what is happening.

Works fine for me (Java 1.6, Win) and nothing disappears after resize and click. Try to specify the location of c.click explicitly c.borderPanel.add(c.click,BorderLayout.CENTER); Maybe there are Java implementations (platforms, versions) where BorderLayout don't like unspecified location.

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