从JFrame类调用的终端类由于某种原因没有用户输入

时间:2014-10-12 01:12:55

标签: java swing terminal jframe user-input

这是我的代码

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

public class Login
{
    static BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
    static String a, b, c;
    static int d, z, f, g, h, i, k;
    public static void Login()
    {
        JFrame frame = new JFrame("Login");

        JButton button1 = new JButton("Login");

        JLabel label1 = new JLabel("Username: ");
        JLabel label2 = new JLabel("Pin: ");

        JTextField txt1 = new JTextField(8);
        JPasswordField pass1 = new JPasswordField(8);

        JPanel panel1 = new JPanel();
        JPanel panel2 = new JPanel();
        JPanel FormPanel = new JPanel();

        txt1.setBackground(Color.white);
        pass1.setBackground(Color.white);

        panel1.add(label1);
        panel1.add(txt1);
        panel2.add(label2);
        panel2.add(pass1);

        FormPanel.setLayout(new GridLayout(3,8));
        FormPanel.add(panel1);
        FormPanel.add(panel2);
        FormPanel.add(button1);

        pass1.getDocument().addDocumentListener(new DocumentListener() 
        {
          public void changedUpdate(DocumentEvent e) 
          {
            changed();
          }
          public void removeUpdate(DocumentEvent e) 
          {
            changed();
          }
          public void insertUpdate(DocumentEvent e) 
          {
            changed();
          }
          public void changed() 
          {
             if (pass1.getText().equals(""))
             {
               button1.setEnabled(false);
             }
             else 
             {
               button1.setEnabled(true);
             }

          }
        });

        txt1.getDocument().addDocumentListener(new DocumentListener() 
        {
          public void changedUpdate(DocumentEvent e) 
          {
            changed();
          }
          public void removeUpdate(DocumentEvent e) 
          {
            changed();
          }
          public void insertUpdate(DocumentEvent e) 
          {
            changed();
          }
          public void changed() 
          {
             if (txt1.getText().equals(""))
             {
               button1.setEnabled(false);
             }
             else 
             {
               button1.setEnabled(true);
             }

          }
        });

        button1.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                frame.hide();

                Body a = new Body();
                a.Body();
            }
        });

        button1.setActionCommand("Open");

        frame.setContentPane(FormPanel);
        frame.setSize(8,9);
        frame.pack();
        frame.show();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
    }
}

我的Body.java是

try
        {
            do
            {
                System.out.print("Working time (hours): ");
                a=dataIn.readLine();
                e=Integer.parseInt(a);

                k=e;
                if(k<8)
                {
                    System.out.print("\nYou have worked undertime");
                    g=e * 30;
                    h=g * 500;
                    i=h-200;
                    System.out.print("\nYour payment (per month) is: " +i);
                }

                if(k>8)
                {
                    System.out.print("\nYou have worked overtime");
                    g=e*30;
                    h=g*500;
                    i=h+200;
                    System.out.print("\nYour payment (per month) is: " +i);
                }

                if(k==8)
                {
                    System.out.print("\nYou have worked ontime");
                    g=e*30;
                    h=g*500;
                    System.out.print("\nYour payment (per month) is: " +h);
                }

                System.out.print("\n\nPress 0 to logout: ");
                c=dataIn.readLine();
                d=Integer.parseInt(c);
            }while(d!=0);
        }

        catch(Exception j)
        {
            System.out.print("\nYou probably need to work for more than an hour to start earning");
        }

当然我有import java.io.*;BufferedReader dataIn = new BufferedReader(<arguments>);但是当我调用在终端中打开的body.java时它不会要求用户输入,但是当我尝试执行body.java时它要求用户输入...... 我现在需要帮助......

1 个答案:

答案 0 :(得分:1)

你问:

  

好的..那么请告诉我如何在完整的GUI中进行一些简单的计算?

您再次希望避免尝试将GUI与控制台程序混合,因为它们以两种非常不同的方式与用户交互,如果您不小心,控制台可以锁定GUI。而是考虑使用所有控制台或所有GUI。

如果您使用所有GUI,一种可能的解决方案是创建一个类似于您已经使用的用户名和PIN号的GUI:

  • 为用户提供GUI JTextFields以输入他的数据。
  • 添加&#34;计算&#34;的JButton
  • 在按钮的ActionListener中,从JTextFields中提取数据,将任何字符串转换为需要转换的数字,计算您的值并将其显示在另一个JTextField或JLabel中。

其他方面的建议:

  • 请不要使用已弃用的方法,因为它们有充分的理由被弃用。相反,Java API通常会告诉您使用哪些替代方案。
  • 避免过度使用静态变量和方法,因为这会导致难以测试或增强的严格代码。
  • 尝试为变量命名,使其代码变为&#34;自我评论&#34;。
  • 您的登录窗口应该是某种类型的模式对话框,例如模态JDialog,而不是JFrame,因为
    • 关闭它不会关闭整个GUI
    • 在完全处理之前,它将阻止与主GUI的交互。