简体   繁体   English

在按钮上单击Java添加新标签和文本字段

[英]Add new label and textfield on button click Java

I have this code with three buttons. 我有三个按钮的代码。 I want to add a label and a textfield after a buttonclick. 我想在buttonclick之后添加标签和文本字段。 So for the 'button' I want to add the 'label' and 'textfield. 因此,对于“按钮”,我想添加“标签”和“文本字段”。

For 'button2' I want to add 'label2' and 'textfield2'. 对于“ button2”,我想添加“ label2”和“ textfield2”。 And so on. 等等。

I already have this code, but it isn't working. 我已经有此代码,但无法正常工作。 I also don't understand how I can create three different ActionListeners. 我也不了解如何创建三个不同的ActionListener。

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

public class Interface implements ActionListener{

    private JLabel label;
    private JLabel label2;
    private JLabel label3;
    private JTextField textfield;
    private JTextField textfield2;
    private JTextField textfield3;
    private JButton button;
    private JButton button2;
    private JButton button3;
    private JPanel panel;

    public static void main(String[] args){
        new Interface();
    }

    public Interface()
    {
        JFrame frame = new JFrame("Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(600,600);
        frame.setLayout(null);

        JPanel panel = new JPanel();
        panel.setBounds(10,10,600,600);
        panel.setLayout(null);

        label = new JLabel("Button 1");
        label.setBounds(5,5,600,20);

        label2 = new JLabel("Button 2");
        label2.setBounds(5,5,600,20);

        label3 = new JLabel("Button 3");
        label3.setBounds(5,5,600,20);

        textfield = new JTextField();
        textfield.setBounds(5,30,100,20);

        JButton button = new JButton("cirkel");
        button.setBounds(130,30,100,20);
        button.addActionListener(this);

        JButton button2 = new JButton("driehoek");
        button2.setBounds(250,30,100,20);
        button2.addActionListener(this);

        JButton button3 = new JButton("vierhoek");
        button3.setBounds(370,30,100,20);
        button3.addActionListener(this);

        panel.add(button);
        panel.add(button2);
        panel.add(button3);

        frame.add(panel);
        frame.setVisible(true);
    }

    public void actionPerformed(ActionEvent arg0)
    {
            panel.add(label);
            panel.add(textfield);
            panel.validate();
            panel.repaint();
            String text = textfield.getText();

            // Van de String 'text' een double maken
            double diameter = Double.parseDouble(text);

            cirkel C1 = new cirkel();
            C1.setDiam(diameter);
            label.setText("De diameter = " + C1.getDiam() + " cm \n\n");
            label.setText("De straal = " + C1.getRadius() + " cm");
            label.setText("De oppervlakte van de cirkel = " + C1.berekenOpp() + " cm2");

    }
}

You have a few choices: 您有几种选择:

You could... 你可以...

Create a ActionListener for each button, this can be done using a outer, inner or anonymous class, this means that you provide a self contained unit of work for each button which is related to only that button. 为每个按钮创建一个ActionListener ,可以使用外部,内部或匿名类来完成,这意味着您为每个按钮提供了一个独立的工作单元,该工作单元仅与该按钮相关。

You could also take advantage of the Action API for the same reasons. 出于相同的原因,您还可以利用Action API。 See How to Use Actions for more details 有关更多详细信息,请参见如何使用动作

You could... 你可以...

Check the ActionEvent#getSource property and compare it to an instance of the button, but your buttons are defined locally 检查ActionEvent#getSource属性并将其与按钮的实例进行比较,但是您的按钮是在本地定义的

You could... 你可以...

Use the actionCommand property of the buttons, setting each button a unique "command" which you can use the ActionEvent#getActionCommand to compare with when the ActionListener is triggered 使用按钮的actionCommand属性,为每个按钮设置一个唯一的“命令”,您可以使用它来与ActionEvent#getActionCommand进行比较,以与触发ActionListener进行比较

Regardless of which choice you use, have a closer look at How to Write an Action Listeners and How to Use Buttons, Check Boxes, and Radio Buttons for more details 不管使用哪种选择,请仔细阅读如何编写动作侦听器以及如何使用按钮,复选框和单选按钮以获取更多详细信息。

Example

Now, having had a chance to run your code, you have a NullPointerException because panel is null in the context of the ActionListener . 现在,有了运行代码的机会,您将获得NullPointerException因为在ActionListener的上下文中panelnull This is cased by the fact that you are shadowing the variable (declaring it twice) 这是因为您正在对变量进行阴影处理(将其声明两次)

// Declared here
private JPanel panel;

public Interface() {
    JFrame frame = new JFrame("Test");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(600, 600);
    frame.setLayout(null);

    // And here...
    JPanel panel = new JPanel();

So, instead, you could use a anonymous class for each button... 因此,您可以为每个按钮使用匿名类...

import java.awt.BorderLayout;
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.JPanel;
import javax.swing.JTextField;

public class Interface {

    private JLabel label;
    private JLabel label2;
    private JLabel label3;
    private JTextField textfield;
    private JTextField textfield2;
    private JTextField textfield3;
    private JButton button;
    private JButton button2;
    private JButton button3;
    private JPanel panel;

    public static void main(String[] args) {
        new Interface();
    }

    public Interface() {
        JFrame frame = new JFrame("Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(600, 600);

        JPanel buttonPane = new JPanel();
        label = new JLabel("Button 1");
        label2 = new JLabel("Button 2");
        label3 = new JLabel("Button 3");

        textfield = new JTextField(5);

        JButton button = new JButton("cirkel");
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                panel.removeAll();
                panel.add(label);
                panel.add(textfield);
                panel.revalidate();
                panel.repaint();
                String text = textfield.getText();

                // Van de String 'text' een double maken
                double diameter = Double.parseDouble(text);

//      cirkel C1 = new cirkel();
//      C1.setDiam(diameter);
                label.setText("De diameter = " + 1 + " cm \n\n");
                label.setText("De straal = " + 2 + " cm");
                label.setText("De oppervlakte van de cirkel = " + 3 + " cm2");
            }
        });

        JButton button2 = new JButton("driehoek");
        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                panel.removeAll();
                panel.add(label);
                panel.add(textfield);
                panel.revalidate();
                panel.repaint();
                // driehoek
            }
        });

        JButton button3 = new JButton("vierhoek");
        button3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                panel.removeAll();
                panel.add(label);
                panel.add(textfield);
                panel.revalidate();
                panel.repaint();
                // vierhoek
            }
        });

        buttonPane.add(button);
        buttonPane.add(button2);
        buttonPane.add(button3);

        panel = new JPanel();

        frame.add(buttonPane, BorderLayout.NORTH);
        frame.add(panel);
        frame.setVisible(true);
    }
}

The next problem you'll have is a NumberFormatException , because you you're trying to convert a blank String to a double , because there is nothing in the text field yet... 下一个问题是NumberFormatException ,因为您正试图将空白String转换为double ,因为文本字段中还没有任何内容...

If you want to create three different ActionListeners, you can create three inner classes, and implement an ActionListener to each class. 如果要创建三个不同的ActionListener,则可以创建三个内部类,并为每个类实现一个ActionListener。 and put the instance of the class inside the addActionListener() . 并将该类的实例放入addActionListener()

public class Example {

    ActionListen listen = new ActionListen();

    button.addActionListener(listen);

    private class ActionListen implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            //code here to change...
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM