[英]How can i disable jbutton when pressed 10 times
I need some help about Java GUI. 我需要有关Java GUI的帮助。 I want to add 10 numbers to memory from JTextField.
我想从JTextField向内存添加10个数字。 And when it's done JButton must gonna be disable and program must show me a message dialog.
完成后,必须禁用JButton,程序必须向我显示一个消息对话框。 How can I do this?
我怎样才能做到这一点?
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);
}
}
Add an ActionListener
to your JButton if using Swing, and inside that, you'll increment a global variable by one. 如果使用Swing,则将
ActionListener
添加到JButton中,并在其中将全局变量加1。 Then you ask if the variable is == 10
and do yourButton.setEnabled(false)
or yourButton.setEnabled(counter < 10)
if you're not using an if. 然后,询问变量是否为
== 10
,如果不使用if,则执行yourButton.setEnabled(false)
或yourButton.setEnabled(counter < 10)
。 You already have your listener set up, so it's only a matter of adding a variable you'll increment inside it and a call to setEnabled
of your button. 您已经设置了侦听器,因此只需添加要在其中添加的变量并调用按钮的
setEnabled
即可。
Here's a working example with an ArrayList
used to store the numbers. 这是一个带有用于存储数字的
ArrayList
的工作示例。 No need to use a global count variable here because the size of the ArrayList
already tells us how many numbers we have stored :-) 这里不需要使用全局计数变量,因为
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.