📜  数字输出摇摆java代码示例

📅  最后修改于: 2022-03-11 14:52:37.760000             🧑  作者: Mango

代码示例1
**UPDATE: I GOT IT*******************

private int price;
private int setPrice;

*othercodes here*

private void setPrice(int price) {
    setPrice = setPrice + price;
    textField.setText(Integer.toString(setPrice));
}

btnNails.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            setPrice(5);
        }
*************************************

import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JTable;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class Frame52 extends JFrame {

    private JPanel contentPane;
    private JTextField textField;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Frame52 frame = new Frame52();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public Frame52() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);            
        JButton btnNails = new JButton("Nails");
        btnNails.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                int getPrice = 0;
                getPrice = getPrice + 5;
            }
        });
        btnNails.setBounds(43, 70, 114, 23);
        contentPane.add(btnNails);          
        JButton btnWood = new JButton("Wood");
        btnWood.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                int getPrice = 0;
                getPrice = getPrice + 20;
            }
        });
        btnWood.setBounds(43, 114, 114, 23);
        contentPane.add(btnWood);

        JButton btnNewButton = new JButton("Galvanized Iron");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                int getPrice = 0;
                getPrice = getPrice + 30;
            }
        });
        btnNewButton.setBounds(43, 159, 114, 23);
        contentPane.add(btnNewButton);          
        JButton btnNewButton_1 = new JButton("Steel");
        btnNewButton_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                int getPrice = 0;
                getPrice = getPrice + 40;
            }
        });
        btnNewButton_1.setBounds(43, 203, 114, 23);
        contentPane.add(btnNewButton_1);            
        JLabel lblTotalIs = new JLabel("Total is:");
        lblTotalIs.setBounds(203, 163, 46, 14);
        contentPane.add(lblTotalIs);            
        textField = new JTextField();
        textField.setText(Integer.toString(getPrice));
        textField.setBounds(259, 160, 86, 20);
        contentPane.add(textField);
        textField.setColumns(10);           
        JButton btnConfirm = new JButton("Confirm");
        btnConfirm.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
            }
        });
        btnConfirm.setBounds(257, 203, 91, 23);
        contentPane.add(btnConfirm);
    }
}