Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

JAVA Button與Label應用

我想運用Button按下時Label裡面的數字可以做加減的動作~~

不知道如何去編寫??請各位程式高手賜教~~~謝謝!!

2 Answers

Rating
  • 志明
    Lv 5
    1 decade ago
    Favorite Answer

    寫了基本的加法你試試看吧^^

    import java.applet.Applet;

    import java.awt.Component;

    import java.awt.Container;

    import java.awt.FlowLayout;

    import java.awt.Label;

    import java.awt.TextField;

    import java.awt.event.ActionEvent;

    import java.awt.event.ActionListener;

    import javax.swing.JButton;

    import javax.swing.JFrame;

    public class TakeEasy3 extends Applet implements ActionListener {

    private static final long serialVersionUID = 1L;

    JFrame jFrame = new JFrame("Action Event");

    JButton button = new JButton("add");

    Container container;

    public TakeEasy3() {

    container = jFrame.getContentPane();

    container.setLayout(new FlowLayout());

    TextField textField = new TextField();

    container.add(textField);

    container.add(new Label("+"));

    container.add(new TextField());

    container.add(new Label("="));

    container.add(new Label(" "));

    container.add(button);

    button.addActionListener(this);

    jFrame.setSize(444, 444);

    jFrame.setVisible(true);

    }

    public static void main(String args[]) {

    new TakeEasy3();

    }

    public void actionPerformed(ActionEvent e) {

    Component[] component = container.getComponents();

    int a = Integer.parseInt(((TextField) component[0]).getText());

    int b = Integer.parseInt(((TextField) component[2]).getText());

    ((Label) component[4]).setText(a + b + "");

    }

    }

    Source(s): mySelf
  • 1 decade ago

    相信你應該有點基礎

    關鍵就是如何取得Label裡面的數字

    用Integer.parseInt(JLabelOBJ.getText())

    取得Label裡面的數字(int型別)

    Source(s): 自己
Still have questions? Get your answers by asking now.