Java为什么我需要最小化然后重新打开加载?

时间:2015-10-11 05:24:54

标签: java swing lag

我制作了一个简单的java程序,可以输入尽可能多的按钮,但是我必须最小化窗口,然后重新启动按钮以实际显示,请帮忙吗?这是代码:

package ButtonTest;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Btest {
public int additive = 20;
private JFrame frame;
private JTextField txtNumber;
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Btest window = new Btest();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}
public Btest() {
    initialize();
}
private void initialize() {
    frame = new JFrame("btest");
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    JLabel lblBtncount = new JLabel("BtnCount");
    lblBtncount.setBounds(24, 39, 61, 16);
    frame.getContentPane().add(lblBtncount);

    txtNumber = new JTextField();
    txtNumber.setText("Number");
    txtNumber.setBounds(99, 33, 134, 28);
    frame.getContentPane().add(txtNumber);
    txtNumber.setColumns(10);

    JButton btnSbmt = new JButton("Sbmt");
    btnSbmt.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String numba = txtNumber.getText();
            int number = Integer.parseInt(numba);
            for(int amntB=1; amntB<number+1; amntB++){
                String amntS = Integer.toString(amntB);
                JButton button = new JButton(amntS);
                button.setBounds(327, 6+additive, 117, 29);
                frame.getContentPane().add(button);
                additive += 20;
             }
        }
    });
    btnSbmt.setBounds(155, 202, 117, 29);
    frame.getContentPane().add(btnSbmt);
}
}

感谢你花时间看看这个,也许给我一个答案?谢谢!

1 个答案:

答案 0 :(得分:2)

  

您建议我使用哪种布局?

我使用GridBagLayout,但你可以使用其他人......

enter image description here

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;


public class Btest {

    public int additive = 20;
    private JFrame frame;
    private JTextField txtNumber;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Btest window = new Btest();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public Btest() {
        initialize();
    }

    private void initialize() {
        frame = new JFrame("btest");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(new GridBagLayout());

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.insets = new Insets(4, 4, 4, 4);
        JLabel lblBtncount = new JLabel("BtnCount");
        frame.getContentPane().add(lblBtncount, gbc);

        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.gridwidth = 1;
        txtNumber = new JTextField();
        txtNumber.setText("Number");
        frame.getContentPane().add(txtNumber, gbc);
        txtNumber.setColumns(10);

        JButton btnSbmt = new JButton("Sbmt");
        btnSbmt.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String numba = txtNumber.getText();
                int number = Integer.parseInt(numba);
                for (int amntB = 1; amntB < number + 1; amntB++) {
                    String amntS = Integer.toString(amntB);
                    JButton button = new JButton(amntS);
                    button.setBounds(327, 6 + additive, 117, 29);
                    frame.getContentPane().add(button);
                    additive += 20;
                }
            }
        });
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.gridwidth = 1;
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        gbc.insets = new Insets(40, 4, 4, 4);
        frame.getContentPane().add(btnSbmt, gbc);

        frame.pack();
        frame.setLocationRelativeTo(null);
    }
}