Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JButton can';不能残疾_Java_Swing_User Interface_Awt - Fatal编程技术网

Java JButton can';不能残疾

Java JButton can';不能残疾,java,swing,user-interface,awt,Java,Swing,User Interface,Awt,我有一个JButton,我希望在按下10次后禁用它。虽然,由于某种原因,我的代码不起作用。你能在这方面给我帮助吗 button.addActionListener(new ActionListener() { int counter = 0; public void actionPerformed(ActionEvent arg0) { liste.add((double) Integer.parseInt(textField.getText()));

我有一个JButton,我希望在按下10次后禁用它。虽然,由于某种原因,我的代码不起作用。你能在这方面给我帮助吗

button.addActionListener(new ActionListener() {
    int counter = 0;
    public void actionPerformed(ActionEvent arg0)
    {
        liste.add((double) Integer.parseInt(textField.getText()));
        textField.setText("");
        while(counter < 9)
        {
            counter++;

            if(counter == 10)
            {
                buton.setEnabled(false);
            }
        }
    }
}
button.addActionListener(新ActionListener(){
int计数器=0;
已执行的公共无效操作(操作事件arg0)
{
add((双精度)Integer.parseInt(textField.getText());
textField.setText(“”);
while(计数器<9)
{
计数器++;
如果(计数器==10)
{
buton.setEnabled(假);
}
}
}
}
另外,我想在按下10次后显示一个消息对话框。你能帮我吗

public void mousePressed( MouseEvent e ) {
    if ( e.getClickCount() == 10 ) {
        ( (JButton) ev.getSource).setEnabled( false );
    }
}


答案是,谢谢你们

buton.addActionListener(new ActionListener() {
  int counter = 0;
  public void actionPerformed(ActionEvent arg0) {
     liste.add((double) Integer.parseInt(textField.getText()));
     textField.setText("");
     counter++;
     if (counter == 10) {
        buton.setEnabled(false);
     }
   }
 }

您甚至不需要while循环,只需将
int counter=0
移动到
addActionListener(…
)上方,并在
public void actionPerformed()内递增计数器。然后在actionPerformed()内选中
if(counter==10)

要显示消息框,请使用
JOptionPane.showMessageDialog();


你根本不需要循环。只需在按下按钮时增加计数器。非常感谢你的帮助。嘿,你能教我如何使用arraylist进行加减吗?@UğurHıdır在堆栈溢出上发布一个单独的线程并将我链接到它,然后我会尽我所能回答它。@UğurHıdır如果我的回答对你有帮助,请请把它标记为答案。
buton.addActionListener(new ActionListener() {
  int counter = 0;
  public void actionPerformed(ActionEvent arg0) {
     liste.add((double) Integer.parseInt(textField.getText()));
     textField.setText("");
     counter++;
     if (counter == 10) {
        buton.setEnabled(false);
     }
   }
 }
public static void showMessageDialog(Component parentComponent, Object message, String title, int messageType) throws HeadlessException
int counter = 0;

button.addActionListener(new ActionListener()
{
    public void actionPerformed(ActionEvent e)
    {
        counter++;

        if(counter == 10)
        {
            button.setEnabled(false);

            // Show message dialog
            JOptionPane.showMessageDialog(null, "This is my message", "This is my message title", JOptionPane.INFORMATION_MESSAGE);
        }
    }
}