Java小程序布局和操作处理程序问题

Java小程序布局和操作处理程序问题,java,swing,Java,Swing,在我的应用程序中,当用户单击JButton时,显示一个JLabel,提示用户输入一个整数,一个JTextField,用户可以在其中键入整数,以及一个包含文本Double me的第二个JButton。当用户单击第二个按钮时,整数加倍,答案显示在jtext字段中 我无法显示第二个按钮和文本字段,当我单击第一个按钮时…请帮助 import java.awt.Container; import java.awt.FlowLayout; import java.awt.event.ActionEvent;

在我的应用程序中,当用户单击JButton时,显示一个JLabel,提示用户输入一个整数,一个JTextField,用户可以在其中键入整数,以及一个包含文本Double me的第二个JButton。当用户单击第二个按钮时,整数加倍,答案显示在jtext字段中

我无法显示第二个按钮和文本字段,当我单击第一个按钮时…请帮助

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;


public class JDouble extends JApplet implements ActionListener {
    private Container container = getContentPane();

    /**
     * The JButton.
     */
    private JButton button = new JButton("Click me");
    private JButton button2 = new JButton("Double me");

    /**
     * The JLabel.
     */
    private JLabel label = new JLabel("Enter Integer");
    private JTextField textfield = new JTextField(4);
    private JTextField textfield2 = new JTextField(4);

    public void init() {
        // set the layout to FlowLayout
        container.setLayout(new FlowLayout());

        // register the 'this' action listener for the button
        button.addActionListener(this);

        container.add(button);
    }

    public void init1(){
        container.setLayout(new FlowLayout());
        container.add(textfield);
        container.add(button2);
        container.add(textfield2);
        button2.addActionListener(this);
    }

    public void actionPerformed(ActionEvent actionEvent) {
        container.add(label);
    }

    public void actionPerformed1(ActionEvent actionEvent) {
        String me = textfield.getText();
        int computation = Integer.parseInt(me);
        computation = computation*2;
        String changecomputation = Integer.toString(computation);
        textfield2.setText(changecomputation);
        container.remove(button);

        container.add(label);

        repaint();
        validate();
    }

}
您的init()方法:

我们将使用此函数在单击“单击我”时显示其他字段

  private showInputFields(){
        container.add(textfield);
        button2.addActionListener(this);
        container.add(button2);
        container.add(textfield2);
  }
让我们修复您的动作侦听器。如果您想在启动时显示“单击我”按钮,
init()
会解决这个问题。当用户单击“单击我”时,我们调用
showInputFields()
来显示其他组件;我们使用同一个侦听器处理“双击我”按钮,我们只需检查事件源以适当地处理

    private boolean inputFieldsDisplayed; 
    public void actionPerformed(ActionEvent actionEvent) {
        if( actionEvent.getSource() == button && !inputFieldsDisplayed){
            showInputFields();
            inputFieldsDisplayed = true;

        } else if ( actionEvent.getSource() == button2){                
            String me = textfield.getText();
            int computation = Integer.parseInt(me);
            computation = computation*2;
            String changecomputation = Integer.toString(computation);
            textfield2.setText(changecomputation);                
        }
        validate();
        repaint();            
    }

另外,像
init1
actionPerformed1
这样的函数名可能没有帮助。但是两个按钮同时出现..我应该怎么做。当我单击按钮1..按钮2时,会出现textfield1,textfield2?当我单击它时,其他控件应该会出现appear@raffian:
validate()/revalidate()
应该在
重新绘制()
之前,如果没有任何更改,为什么需要
重新绘制()
。这就是为什么我们首先
重新验证()/validate()
,这样布局管理器就可以布置组件,然后我们调用
重新绘制()
要使这些更改看起来很漂亮:-)@raffian:请看看这个和这个
    private boolean inputFieldsDisplayed; 
    public void actionPerformed(ActionEvent actionEvent) {
        if( actionEvent.getSource() == button && !inputFieldsDisplayed){
            showInputFields();
            inputFieldsDisplayed = true;

        } else if ( actionEvent.getSource() == button2){                
            String me = textfield.getText();
            int computation = Integer.parseInt(me);
            computation = computation*2;
            String changecomputation = Integer.toString(computation);
            textfield2.setText(changecomputation);                
        }
        validate();
        repaint();            
    }