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

标签 java swing

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

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

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() 方法:

   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);
}

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

  private showInputFields(){
        container.add(textfield);
        button2.addActionListener(this);
        container.add(button2);
        container.add(textfield2);
  }

让我们修复您的 Action 监听器。如果您希望在启动时显示“单击我”按钮,init() 会处理此问题。当用户单击“单击我”时,我们调用 showInputFields() 来显示其他组件;我们使用相同的监听器处理“Double me”点击,我们只是检查事件源以适当处理...

    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();            
    }

关于Java 小程序布局和操作处理程序问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18086387/

相关文章:

java - Netbeans scala 插件 - 无文档

java - 如何在 ant 文件中对 ipv6 地址执行 scp

java - Jtree:如何隐藏给定默认可变树节点文本的部分内容?

Java Swing 处理状态

java - 使用 jdk8 和 groovy 2.4.4- java.lang.InknownClassChangeError 运行 job-dsl-plugin

Java用HTML标签<i>替换字符下划线并再次关闭它的最有效方法

java - 圆角半径可绘制形状

Java - JRadioButton选择出现面板

java - 如何在现有 JTable 的顶部添加一个表,其中一行由 JComboboxes 组成

java - Window JPanel 是透明的吗?