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

时间:2015-06-06 22:26:32

标签: java swing jbutton actionlistener jlabel

我有三个按钮的代码。我想在按钮点击后添加标签和文本字段。所以对于'按钮',我想添加'标签'和'文本字段。

对于'button2',我想添加'label2'和'textfield2'。等等。

我已经拥有此代码,但它无效。我也不明白如何创建三个不同的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");

    }
}

2 个答案:

答案 0 :(得分:3)

您有几个选择:

你可以......

为每个按钮创建一个ActionListener,这可以使用外部,内部或匿名类来完成,这意味着您为每个按钮提供一个独立的工作单元,该单元仅与该按钮相关。

出于同样的原因,您也可以利用Action API。有关详细信息,请参阅How to Use Actions

你可以......

检查ActionEvent#getSource属性并将其与按钮实例进行比较,但是您的按钮是在本地定义的

你可以......

使用按钮的actionCommand属性,将每个按钮设置为唯一的"命令"您可以使用ActionEvent#getActionCommand与触发ActionListener的时间进行比较

无论您使用哪种选择,请仔细查看 How to Write an Action ListenersHow to Use Buttons, Check Boxes, and Radio Buttons了解详情

实施例

现在,在有机会运行代码的情况下,NullPointerException因为panel nullActionListener的上下文中有// 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(); 。这是因为你正在隐藏变量(声明它两次)

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

因此,您可以为每个按钮使用匿名类......

NumberFormatException

您遇到的下一个问题是String,因为您 您尝试将空白double转换为system.time(foreach (i=1:5, .combine='c') %do% {Sys.sleep(2);i}) user system elapsed 0.053 0.011 10.012 ,因为文字字段中没有任何内容......

答案 1 :(得分:1)

如果要创建三个不同的ActionListener,可以创建三个内部类,并为每个类实现一个ActionListener。并将该类的实例放在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...
        }
    }
}
相关问题