0

http://i415.photobucket.com/albums/pp235/wong93_photos/Untitled-3.png

皆さん、ここでいくつか質問があります。質問する前に、このプログラムでやりたいことは、押されたトグルの数をカウントし、「数量」の下の jTextField に合計を設定することです。たとえば、A と B のトグルを押しましたボタンなので、数量は 2 と表示され、jLabel7 は 20 と表示されます

1) 押されたトグルの数をカウントするにはどうすればよいですか?? どこにもこれが見つからない

2) jTextField に値を入力する方法?? 基本的なことは知っていますが、方法がわかりません。検索しましたが、それらはすべて、挿入するのではなく、テキストフィールドから情報を取得することに関するものです

3) jLabel7 から他の jFrame に値を渡すにはどうすればよいですか?? 領収書として使用したいので、例: 総費用は 20 です

どうもありがとう !

4

1 に答える 1

2
import java.awt.Dimension;
import java.awt.FlowLayout;
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 Example extends JPanel {

    private static final long serialVersionUID = 1L;

    private int toggle_count = 0;
    private JTextField text_field;
    private JLabel label;

    public Example() {

        this.setLayout(new FlowLayout());

        JButton button_a = new JButton("Button A");
        this.add(button_a);

        JButton button_b = new JButton("Button B");
        this.add(button_b);

        text_field = new JTextField("0");
        text_field.setPreferredSize(new Dimension(50, 20));
        text_field.setEditable(false);
        this.add(text_field);

        label = new JLabel("x 10 = 0");
        this.add(label);

        button_a.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub

                updateToggle();
            }
        });

        button_b.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub

                updateToggle();
            }
        });

        JButton receipt = new JButton("Receipt");
        this.add(receipt);

        receipt.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub

                SomeClass.main(toggle_count*10);
            }
        });

    }

    public void updateToggle() {

        toggle_count++;
        text_field.setText("" +toggle_count);
        label.setText("x 10 = " + toggle_count * 10);
    }

    public static void main(String[] args) {

        JFrame frame = new JFrame("Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(640, 480);
        frame.setLocationRelativeTo(null);
        frame.setResizable(false);
        frame.setContentPane(new Example());

        frame.setVisible(true);
    }
}

SomeClass は、引数として toggle_count*10 を取るメイン メソッドを持つ他のクラスです。

于 2014-01-08T12:22:36.643 に答える