簡體   English   中英

按下10次后如何禁用jbutton

[英]How can i disable jbutton when pressed 10 times

我需要有關Java GUI的幫助。 我想從JTextField向內存添加10個數字。 完成后,必須禁用JButton,程序必須向我顯示一個消息對話框。 我怎樣才能做到這一點?

import java.awt.BorderLayout;
import java.awt.EventQueue;

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

public class Uygulama extends JFrame {
    private JPanel contentPane;
    private JTextField textField;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Uygulama frame = new Uygulama();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    public Uygulama() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 233, 140);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
        textField = new JTextField();
        textField.setBounds(48, 13, 116, 22);
        contentPane.add(textField);
        textField.setColumns(10);
        JButton btnHesapla = new JButton("HESAPLA");
        btnHesapla.addActionListener(new ActionListener() {int counter = 0; 
        public void actionPerformed(ActionEvent arg0) {
            while(counter < 9) {
                counter++;
                if(counter == 10) {
                    buton.setEnabled(false);}
            }
        });
        btnHesapla.setBounds(58, 48, 97, 25);
        contentPane.add(btnHesapla);
    }
}

如果使用Swing,則將ActionListener添加到JButton中,並在其中將全局變量加1。 然后,詢問變量是否為== 10 ,如果不使用if,則執行yourButton.setEnabled(false)yourButton.setEnabled(counter < 10) 您已經設置了偵聽器,因此只需添加要在其中添加的變量並調用按鈕的setEnabled即可。

這是一個帶有用於存儲數字的ArrayList的工作示例。 這里不需要使用全局計數變量,因為ArrayList的大小已經可以告訴我們已經存儲了多少個數字:-)

public class NumbersApplication extends JFrame {

    private JPanel contentPane;
    private JTextField textField;
    private List<Integer> numbers;

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

    public NumbersApplication() {
        numbers = new ArrayList<>();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 233, 140);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
        textField = new JTextField();
        textField.setBounds(48, 13, 116, 22);
        contentPane.add(textField);
        textField.setColumns(10);
        JButton btnHesapla = new JButton("HESAPLA");
        btnHesapla.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int number = Integer.parseInt(textField.getText());
                numbers.add(number);
                btnHesapla.setEnabled(numbers.size() < 10);
                System.out.println(number + " has been added to memory.");
                if (numbers.size() == 10) {
                    System.out.println("Your numbers are: " + numbers);
                }
            }
        });
        btnHesapla.setBounds(58, 48, 97, 25);
        contentPane.add(btnHesapla);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM