在java中,如何在不最小化界面的情况下显示新按钮

时间:2015-03-25 04:00:33

标签: java

每当我点击一个按钮时,就会出现一个新按钮,但是按下它后它没有出现,除非我最小化窗口然后再打开它。继承我的GUI代码: 附:我正在玩代码以防万一你想知道变量。

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Gui extends JFrame{
private Label Count;
private JTextField tfCount;
private Button btnCount;

public Gui(){
    super("counter Gui");
    setLayout(new FlowLayout());

    Count = new Label("Box Maker");
    add(Count);

    tfCount = new JTextField(10);
    tfCount.setEditable(true);
    add(tfCount);

    btnCount = new Button("Enter");
    add(btnCount);

    potatoHandler handler = new potatoHandler();
    btnCount.addActionListener(handler);
    tfCount.addActionListener(handler);
}
private class potatoHandler implements ActionListener{

    public void actionPerformed(ActionEvent event) {
        String s = "";
        if (event.getSource() == tfCount || event.getSource() == btnCount){
            Button newButton = new Button("new Button");
            add(newButton);

        }


    }

}

}

1 个答案:

答案 0 :(得分:1)

在您添加按钮的容器上调用revalidaterepaint

private class potatoHandler implements ActionListener{

    public void actionPerformed(ActionEvent event) {
        String s = "";
        if (event.getSource() == tfCount || event.getSource() == btnCount){
            Button newButton = new Button("new Button");
            add(newButton);
            revalidate();
            repaint();
        }
   }
}