gpt4 book ai didi

java - 如何在按 10 次时禁用 jbutton

转载 作者:行者123 更新时间:2023-12-02 11:15:47 25 4
gpt4 key购买 nike

我需要一些有关 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);
}
}

最佳答案

添加ActionListener如果使用 Swing,则将其添加到 JButton,并且在其中将全局变量加一。然后你询问变量是否是 == 10并做yourButton.setEnabled(false)yourButton.setEnabled(counter < 10)如果你不使用 if 。您已经设置了监听器,因此只需添加一个要在其中递增的变量并调用 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);
}
}

关于java - 如何在按 10 次时禁用 jbutton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50292404/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com