JOptionPane.showMessageDialog不断重现

时间:2014-09-10 18:15:02

标签: java swing while-loop joptionpane

我的代码应该确保用户只输入二进制字符串。如果我输入正确的二进制数(1,0),一切顺利。但是当我输入一个错误的数字(字母或数字而不是0和1)时,会出现JOptionPane.ShowMessageDialog,显示错误信息,当我点击" OK"或者" Cross"按钮再次出现。因此,要关闭应用程序,我必须使用进程管理器来杀死" java.exe"。

以下是我的应用的完整代码:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

class Bn extends JFrame {
    private JLabel l1;
    private JLabel l2;
    private JLabel l3;

    private JTextField tf;

    private JButton oc;
    private JButton hd;
    private JButton dc;

public Bn() {
    setTitle("Binary Conversion");
    setSize(350 , 190);
    setResizable(false);
    setLocation(720 , 200);
    //setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new FlowLayout(FlowLayout.CENTER, 25, 10));

    l1 = new JLabel(">>>>>>>>BINARY CONVERSION<<<<<<<<");
    l2 = new JLabel("Enter The Number:");
    l3 = new JLabel("                                    Convert to:                                    ");

    tf = new JTextField(25);

    oc = new JButton("Octal");
    hd = new JButton("Hexa Decimal");
    dc = new JButton("Decimal");

    add(l1); // Binary conversion
    add(l2); // Enter The Number

    add(tf); // Text Field

    add(l3); // Conver to


    add(oc); // Octal
    add(hd); // Hexa Decimal
    add(dc); // Decimal

    oc.addActionListener(new cvr());
    hd.addActionListener(new cvr());
    dc.addActionListener(new cvr());

    setVisible(true);
}

void res()
{
    int b = 0;
    String num = tf.getText();
    int i , l;
    l = num.length();
    char ch;

    do
    {
        for (i=0 ; i<l ; i++)
        {
            b = 0;
            ch = num.charAt(i);
            if (Character.isDigit(ch) && (ch == 48 || ch == 49))
                b=1;
            else
            {
                JOptionPane.showMessageDialog(null, "Please enter a binary number (1 , 0)");
            }
        }
    }
    while(b != 1);
}


void occ()
{
    res();

    String num = tf.getText();

    long dec = Long.parseLong(num,2);

    String oct = Long.toOctalString(dec);

    JOptionPane.showMessageDialog(null, "Octal equivalent is: "+ oct);
}

void hdc()
{
    res();

    String num = tf.getText();

    long dec = Integer.parseInt(num,2);

    String hex = Long.toHexString(dec);

    JOptionPane.showMessageDialog(null, "Hexa Decimal equivalent is: "+ hex);

}

void dcc()
{
    res();

    String num = tf.getText();
    long decimal = 0, temp, i = 0;
    temp = Long.parseLong(num);
    while (temp != 0) {
        long r = temp % 10;
        long value = r * (int)Math.pow(2, i);
        i++;
        decimal = decimal + value;
        temp /= 10;
    }
    JOptionPane.showMessageDialog(null, "Decimal equivalent is: "+ decimal);
}


private class cvr implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        if (e.getSource() == oc)
        {
            occ();
        }

        if (e.getSource() == hd)
        {
            hdc();
        }

        if (e.getSource() == dc)
        {
            dcc();
        }
    }
}

public static void main(String args[])
{
    Bn obj = new Bn();
}
} 

JOptionPane的代码位于void res()方法中。如何关闭此“对话框”窗格以便重新输入输入值?

1 个答案:

答案 0 :(得分:0)

删除do {} while循环

do
{
   for (i=0 ; i < l ; i++)
    {
        b = 0;
        ch = num.charAt(i);
        if (Character.isDigit(ch) && (ch == 48 || ch == 49))
            b=1;
        else
        {
            JOptionPane.showMessageDialog(null, "Please enter a binary number (1 , 0)");
        }
    }
}
while(b != 1);