我想这个肯定有很多的错误,只是编译的时候能通过...运行的时候不能通过。
希望大家帮帮我,我是新手..
谢谢大家啦import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.UIManager;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;public class Register extends JFrame implements ActionListener
{
public static final int WIDTH = 300;
public static final int HEIGHT =200;
private JTextField name;
private JPasswordField password;
private JPasswordField confirm;
private JLabel messageLabel;
private JLabel nameLabel;
private JLabel passwordLabel;
private JLabel confirmLabel;
private JButton ok;
private JButton reset;
private JButton exit;
private JPanel messagePanel;
private JPanel textPanel;
private JPanel buttonPanel;

public Register()
{
setTitle("Register_TestSystem");
setSize(WIDTH,HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container content = getContentPane();
content.setLayout(new GridLayout(3,1));

try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e)
{
throw new RuntimeException(e);
}

messagePanel = new JPanel();
messagePanel.setLayout(new FlowLayout());
messagePanel.setBackground(Color.white);
messagePanel.add(messageLabel);
messageLabel.setText("Please register first, and then to log in.");

textPanel = new JPanel();
textPanel.setLayout(new GridLayout(6,1));
textPanel.setBackground(Color.white);
textPanel.add(nameLabel);
textPanel.add(name);
textPanel.add(passwordLabel);
textPanel.add(password);
textPanel.add(confirmLabel);
textPanel.add(confirm);
nameLabel.setText("Name :");
passwordLabel.setText("Password :");
confirmLabel.setText("Confirm :");

buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());
buttonPanel.setBackground(Color.white);
buttonPanel.add(ok);
buttonPanel.add(reset);
buttonPanel.add(exit);
ok = new JButton("ok");
ok.addActionListener(this);
reset = new JButton("reset");
reset.addActionListener(this);
exit = new JButton("exit");
exit.addActionListener(this);
JButton b = new JButton("shit");


content.add(messagePanel);
content.add(textPanel);
content.add(buttonPanel);
}

public void actionPerformed(ActionEvent event)
{
String command = event.getActionCommand();
if(command.equals("ok"))
{
if(password.getText().equals(confirm.getText()))
{
BufferedReader fileInput = null;
PrintWriter output = null;
ObjectOutputStream pw = null;
String getUid;
boolean a = true;
try
{
fileInput = new BufferedReader(new FileReader("/home/kumuking/uid.txt"));
getUid = fileInput.readLine();
while(getUid != null)
{
if(getUid.equals(name.getText()))
{
a = false;
}
getUid = fileInput.readLine();
}

fileInput.close();

if(a)
{
output = new PrintWriter(new FileOutputStream("/home/kumuking/uid.txt"),true);
pw = new ObjectOutputStream(new FileOutputStream(name.getText()));
output.println(name.getText());
pw.writeUTF(password.getText());
}
else
{
messagePanel.setBackground(Color.red);
messageLabel.setText("ERROR : The entered username has existed! TRY AGAIN!");
}

output.close();
pw.close();
}
catch(Exception e)
{
messagePanel.setBackground(Color.red);
messageLabel.setText("UNEXPECTED ERROR : Please contact with administrator!");
}
}
else
{
messagePanel.setBackground(Color.red);
messageLabel.setText("ERROR : Enter two separate passwords! TRY AGAIN! ");
}
}
else if(command.equals("reset"))
{
name.setText("");
password.setText("");
confirm.setText("");
}
else if(command.equals("exit"))
{
System.exit(0);
}
else 
{
messagePanel.setBackground(Color.red);
messageLabel.setText("UNEXPECTED ERROR : Please contact with administrator!");
}
}}

解决方案 »

  1.   

    你变量一个都没实例话怎么运行,还有窗口也没设置可以显示,现在可以显示了,你的功能大概还有点问题
    import javax.swing.JFrame;
    import javax.swing.JTextField;
    import javax.swing.JPasswordField;
    import javax.swing.JLabel;
    import javax.swing.JButton;
    import javax.swing.JPanel;
    import javax.swing.UIManager;
    import java.awt.Color;
    import java.awt.FlowLayout;
    import java.awt.GridLayout;
    import java.awt.Container;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.BufferedReader;
    import java.io.FileOutputStream;
    import java.io.FileReader;
    import java.io.ObjectOutputStream;
    import java.io.PrintWriter;public class Register extends JFrame implements ActionListener {
    public static final int WIDTH = 300;
    public static final int HEIGHT = 200;
    private JTextField name = new JTextField();
    private JPasswordField password = new JPasswordField();
    private JPasswordField confirm = new JPasswordField();
    private JLabel messageLabel = new JLabel();
    private JLabel nameLabel = new JLabel();
    private JLabel passwordLabel = new JLabel();
    private JLabel confirmLabel = new JLabel();
    private JButton ok = new JButton();
    private JButton reset = new JButton();
    private JButton exit = new JButton();
    private JPanel messagePanel;
    private JPanel textPanel;
    private JPanel buttonPanel; public static void main(String[]args) {
    new Register();
    }

    public Register() {
    setTitle("Register_TestSystem");
    setSize(WIDTH, HEIGHT);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container content = getContentPane();
    content.setLayout(new GridLayout(3, 1)); try {
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (Exception e) {
    throw new RuntimeException(e);
    } messagePanel = new JPanel();
    messagePanel.setLayout(new FlowLayout());
    messagePanel.setBackground(Color.white);
    messagePanel.add(messageLabel);
    messageLabel.setText("Please register first, and then to log in."); textPanel = new JPanel();
    textPanel.setLayout(new GridLayout(6, 1));
    textPanel.setBackground(Color.white);
    textPanel.add(nameLabel);
    textPanel.add(name);
    textPanel.add(passwordLabel);
    textPanel.add(password);
    textPanel.add(confirmLabel);
    textPanel.add(confirm);
    nameLabel.setText("Name :");
    passwordLabel.setText("Password :");
    confirmLabel.setText("Confirm :"); buttonPanel = new JPanel();
    buttonPanel.setLayout(new FlowLayout());
    buttonPanel.setBackground(Color.white);
    buttonPanel.add(ok);
    buttonPanel.add(reset);
    buttonPanel.add(exit);
    ok = new JButton("ok");
    ok.addActionListener(this);
    reset = new JButton("reset");
    reset.addActionListener(this);
    exit = new JButton("exit");
    exit.addActionListener(this);
    JButton b = new JButton("shit"); content.add(messagePanel);
    content.add(textPanel);
    content.add(buttonPanel);
    this.setVisible(true);
    } public void actionPerformed(ActionEvent event) {
    String command = event.getActionCommand();
    if (command.equals("ok")) {
    if (password.getText().equals(confirm.getText())) {
    BufferedReader fileInput = null;
    PrintWriter output = null;
    ObjectOutputStream pw = null;
    String getUid;
    boolean a = true;
    try {
    fileInput = new BufferedReader(new FileReader(
    "/home/kumuking/uid.txt"));
    getUid = fileInput.readLine();
    while (getUid != null) {
    if (getUid.equals(name.getText())) {
    a = false;
    }
    getUid = fileInput.readLine();
    } fileInput.close(); if (a) {
    output = new PrintWriter(new FileOutputStream(
    "/home/kumuking/uid.txt"), true);
    pw = new ObjectOutputStream(new FileOutputStream(name
    .getText()));
    output.println(name.getText());
    pw.writeUTF(password.getText());
    } else {
    messagePanel.setBackground(Color.red);
    messageLabel
    .setText("ERROR : The entered username has existed! TRY AGAIN!");
    } output.close();
    pw.close();
    } catch (Exception e) {
    messagePanel.setBackground(Color.red);
    messageLabel
    .setText("UNEXPECTED ERROR : Please contact with administrator!");
    }
    } else {
    messagePanel.setBackground(Color.red);
    messageLabel
    .setText("ERROR : Enter two separate passwords! TRY AGAIN! ");
    }
    } else if (command.equals("reset")) {
    name.setText("");
    password.setText("");
    confirm.setText("");
    } else if (command.equals("exit")) {
    System.exit(0);
    } else {
    messagePanel.setBackground(Color.red);
    messageLabel
    .setText("UNEXPECTED ERROR : Please contact with administrator!");
    }
    }}
      

  2.   

    谢谢你,坏孩子
    我按你的做法添加了
    public static void main(String[] args)
    {
    Register test =  new Register();
    test.setVisible(true); }
    可是仍然不能显示,并且抛出了异常
    Exception in thread "main" java.lang.NullPointerException
    at java.awt.Container.addImpl(Container.java:1070)
    at java.awt.Container.add(Container.java:377)
    at newTest.Register.<init>(Register.java:62)
    at newTest.Register.main(Register.java:170)我很郁闷诶,从早上11点开始写的,结果竟然是这样的...
      

  3.   

    我的代码jdk1.5没问题,你复制下来用就可以了,当然你里面的功能我没改,里面有问题
      

  4.   


    谢谢你,我已经找到我的错误所在啦,真是太感谢你啦现在有个更郁闷的问题。output = new PrintWriter(new FileOutputStream(
                                    "/home/kumuking/uid.txt"), true);
    这里我写的是希望能够在/home/kumuking/uid.txt 这个文件里添加新的用户名,可是为什么每次一运行,就直接把文件里原有的信息给刷没了呢?
      

  5.   

    我心里就想吃了苍蝇一样难受,
    我就是找不出和坏孩子有什么地方不一样的...package easy;import javax.swing.JFrame;
    import javax.swing.JTextField;
    import javax.swing.JPasswordField;
    import javax.swing.JLabel;
    import javax.swing.JButton;
    import javax.swing.JPanel;
    import javax.swing.UIManager;import UI.test1;import java.awt.Color;
    import java.awt.FlowLayout;
    import java.awt.GridLayout;
    import java.awt.Container;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.BufferedReader;
    import java.io.FileOutputStream;
    import java.io.FileReader;
    import java.io.ObjectOutputStream;
    import java.io.PrintWriter;public class Register extends JFrame implements ActionListener
    {
    public static final int WIDTH = 400;
    public static final int HEIGHT = 400;
    private JTextField name = new JTextField();
    private JPasswordField password = new JPasswordField();
    private JPasswordField confirm = new JPasswordField();
    private JLabel messageLabel = new JLabel();
    private JLabel nameLabel = new JLabel();
    private JLabel passwordLabel = new JLabel();
    private JLabel confirmLabel = new JLabel();
    private JButton ok = new JButton();
    private JButton reset = new JButton();
    private JButton exit = new JButton();
    private JPanel messagePanel;
    private JPanel textPanel;
    private JPanel buttonPanel;

     public static void main(String[] args) {
            new Register();
        }

    public Register()
    {
    setTitle("Register_TestSystem");
    setSize(WIDTH,HEIGHT);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container content = getContentPane();
    content.setLayout(new GridLayout(3,1));

    try
    {
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    }
    catch(Exception e)
    {
    throw new RuntimeException(e);
    }

    messagePanel = new JPanel();
    messagePanel.setLayout(new FlowLayout());
    messagePanel.setBackground(Color.white);
    messagePanel.add(messageLabel);
    messageLabel.setText("Please register first, and then to log in.");

    textPanel = new JPanel();
    textPanel.setLayout(new GridLayout(6,1));
    textPanel.setBackground(Color.white);
    textPanel.add(nameLabel);
    textPanel.add(name);
    textPanel.add(passwordLabel);
    textPanel.add(password);
    textPanel.add(confirmLabel);
    textPanel.add(confirm);
    nameLabel.setText("Name :");
    passwordLabel.setText("Password :");
    confirmLabel.setText("Confirm :");

    ok = new JButton("ok");
    ok.addActionListener(this);
    reset = new JButton("reset");
    reset.addActionListener(this);
    exit = new JButton("exit");
    exit.addActionListener(this);
    buttonPanel = new JPanel();
    buttonPanel.setLayout(new FlowLayout());
    buttonPanel.setBackground(Color.white);
    buttonPanel.add(ok);
    buttonPanel.add(reset);
    buttonPanel.add(exit);



    content.add(messagePanel);
    content.add(textPanel);
    content.add(buttonPanel);

    this.setVisible(true);
    }

    public void actionPerformed(ActionEvent event)
    {
    String command = event.getActionCommand();
    if(command.equals("ok"))
    {
    if(password.getText().equals(confirm.getText()))
    {
    BufferedReader fileInput = null;
    PrintWriter output = null;
    ObjectOutputStream pw = null;
    String getUid;
    boolean a = true;
    try
    {
    fileInput = new BufferedReader(new FileReader("/home/kumuking/uid.txt"));
    getUid = fileInput.readLine();
    while(getUid != null)
    {
    if(getUid.equals(name.getText()))
    {
    a = false;
    }
    getUid = fileInput.readLine();
    }

    fileInput.close();

    if(a)
    {
    output = new PrintWriter(new FileOutputStream("/home/kumuking/uid.txt"),true);
    pw = new ObjectOutputStream(new FileOutputStream(name.getText()));
    output.println(name.getText());
    pw.writeUTF(password.getText());
    }
    else
    {
    messagePanel.setBackground(Color.red);
    messageLabel.setText("ERROR : The entered username has existed! TRY AGAIN!");
    }

    output.close();
    pw.close();
    }
    catch(Exception e)
    {
    messagePanel.setBackground(Color.red);
    messageLabel.setText("UNEXPECTED ERROR : Please contact with administrator!");
    }
    }
    else
    {
    messagePanel.setBackground(Color.red);
    messageLabel.setText("ERROR : Enter two separate passwords! TRY AGAIN! ");
    }
    }
    else if(command.equals("reset"))
    {
    name.setText("");
    password.setText("");
    confirm.setText("");
    }
    else if(command.equals("exit"))
    {
    System.exit(0);
    }
    else 
    {
    messagePanel.setBackground(Color.red);
    messageLabel.setText("UNEXPECTED ERROR : Please contact with administrator!");
    }
    }}难受 难受阿...
      

  6.   

    FileOutputStream(String   name,boolean   append)查api我不讲
      

  7.   

    Exception in thread "main" java.lang.Error: Unresolved compilation problem:  at easy.Register.main(Register.java:54)