导出Runnable JAR文件无法正常工作

时间:2014-05-16 17:34:54

标签: java eclipse swing jar

我已经阅读了很多类似的问题,但我找不到任何有用的东西,所以我必须自己问一个。

我有一个程序使用TCP套接字连接服务器和客户端,在一个包中总共有5个类,其中两个有一个main方法(server和clientGUI)。

enter image description here

但是当我在server.java上使用rightclick时 - >出口 - >运行jar文件与服务器运行配置,它工作,我可以从外部eclipse运行它。但是当我为clientGUI.java做同样的事情时,运行clientGUI.jar文件时没有任何反应。

服务器中的

主要方法如下所示:

public static void main(String[] args) {
    Server s = new Server();
    s.start();
}

但在ClientGUI中我只有这个:

public static void main(String[] args) {
    new ClientGUI();
}
那是问题吗?我在ClientGUI中没有start(),因为在用户输入之前没有任何事情发生,并且与图形相关的所有内容都在构造函数中编码,其余的都在ActionPerformed方法中处理按钮。


ClientGUI:

public class ClientGUI extends JFrame implements ActionListener {

private static final long serialVersionUID = -1115273859077115660L;
private JTextField textField;
private JTextArea chatBox;

private static Client client;
private JButton resetB;
JButton btnConnect;
JLabel lblGrid;
JLabel lblGrid2;
static int port = 56667;
String IP;
private JTextField txtPort;
private JTextField txtIP;
final JButton readyB;

ClientGUI() {
    super("Client");
    getContentPane().setBackground(Color.LIGHT_GRAY);
    setResizable(false);
    setBounds(100, 100, 945, 750);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    getContentPane().setLayout(null);

    JPanel panel = new JPanel();
    panel.setBackground(Color.WHITE);
    panel.setBounds(10, 11, 920, 520);
    getContentPane().add(panel);
    panel.setLayout(null);

    readyB = new JButton("READY");
    readyB.setEnabled(false);
    readyB.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            lblGrid.setVisible(true);
            readyB.setEnabled(false);
            client.sendMessage(new Message(Message.READY, client.myID, ""));
        }
    });
    readyB.setBounds(784, 461, 126, 48);
    panel.add(readyB);

    BufferedImage myPicture = null;
    try {
        myPicture = ImageIO.read(new File("img/grid.png"));
    } catch (IOException e) {

        e.printStackTrace();
    }
    lblGrid = new JLabel(new ImageIcon(myPicture));
    lblGrid.setText("");
    lblGrid.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent m) {
            int x = m.getX() / 40;
            int y = m.getY() / 40;
            if(client.myShots[x][y] == false){
            client.sendMessage(new Message(Message.SHOT, client.myID,
                    (x + 1) + " " + (y + 1)));
            client.myShots[x][y] = true;}
        }
    });
    lblGrid.setBounds(480, 40, 401, 401);
    panel.add(lblGrid);
    lblGrid.setVisible(false);

    lblGrid2 = new JLabel(new ImageIcon(myPicture));
    lblGrid2.setText("");
    lblGrid2.setBounds(40, 40, 401, 401);
    panel.add(lblGrid2);

    textField = new JTextField();
    textField.setBounds(493, 681, 437, 20);
    getContentPane().add(textField);
    textField.setColumns(10);
    textField.addActionListener(this);

    JPanel panel_1 = new JPanel();
    panel_1.setBounds(10, 542, 210, 159);
    getContentPane().add(panel_1);
    panel_1.setLayout(null);

    resetB = new JButton("R");
    resetB.setBounds(160, 108, 40, 40);
    resetB.addActionListener(this);
    panel_1.add(resetB);

    btnConnect = new JButton("Connect");
    btnConnect.setBounds(230, 542, 89, 23);
    getContentPane().add(btnConnect);
    btnConnect.addActionListener(this);

    JLabel lblIp = new JLabel("IP:");
    lblIp.setBounds(329, 546, 22, 14);
    getContentPane().add(lblIp);

    JLabel lblPort = new JLabel("Port:");
    lblPort.setBounds(329, 580, 46, 14);
    getContentPane().add(lblPort);

    txtPort = new JTextField();
    txtPort.setText("1234");
    txtPort.setBounds(361, 577, 120, 20);
    getContentPane().add(txtPort);
    txtPort.setColumns(10);

    txtIP = new JTextField();
    txtIP.setText("192.168.0.25");
    txtIP.setBounds(361, 542, 120, 20);
    getContentPane().add(txtIP);
    txtIP.setColumns(10);

    JScrollPane scrollPane = new JScrollPane();
    scrollPane.setBounds(491, 544, 439, 124);
    getContentPane().add(scrollPane);

    chatBox = new JTextArea();
    scrollPane.setViewportView(chatBox);
    chatBox.setEditable(false);
    chatBox.setLineWrap(true);

    textField.requestFocus();

    setVisible(true);
}

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

void drawX(JLabel grid, int x, int y) {
    JLabel xg = (new JLabel(new ImageIcon("img/x.png")));
    xg.setBounds(40 * (x - 1), 40 * (y - 1), 40, 40);
    grid.add(xg);
    xg.setVisible(true);
    repaint();
}

void drawMiss(JLabel grid, int x, int y) {
    JLabel xg = (new JLabel(new ImageIcon("img/miss.png")));
    xg.setBounds(40 * (x - 1), 40 * (y - 1), 40, 40);
    grid.add(xg);
    xg.setVisible(true);
    repaint();
}

public void actionPerformed(ActionEvent e) {
    Object o = e.getSource();

    if (o == textField) {
        String text = textField.getText();

        client.sendMessage(new Message(Message.CHAT, client.myID, text));
        textField.setText("");
    }

    if (o == resetB) {
    }

    if (o == btnConnect) {
        IP = txtIP.getText();
        port = Integer.parseInt(txtPort.getText());

        client = new Client(IP, port, this);
        // test if we can start the Client
        if (!client.start())
            return;
        else
        client.display("Connected!");
        txtIP.setEnabled(false);
        txtPort.setEnabled(false);
        btnConnect.setEnabled(false);
        readyB.setEnabled(true);
    }
}

public void append(String string) {
    chatBox.append(string);
    chatBox.setCaretPosition(chatBox.getText().length() - 1);
}
}

客户端:

public class Client {
private ObjectOutputStream sOutput;
private ObjectInputStream sInput;
Socket socket;
private SimpleDateFormat sdf;
private static ClientGUI cg;
private static String IP;
private static int port;
int myID;
boolean[][]myShots = new boolean [10][10];

Client(String IP, int port, ClientGUI cg) {
    this.IP = IP;
    this.port = port;
    this.cg = cg;

    sdf = new SimpleDateFormat("HH:mm:ss");
}


public boolean start() {
    try {
        socket = new Socket(IP, port);
    } catch (Exception e) {
        display("Server not running!");
        return false;
    }
    try {
        sInput = new ObjectInputStream(socket.getInputStream());
        sOutput = new ObjectOutputStream(socket.getOutputStream());
    } catch (IOException e) {
        display("Exception creating new Input/output Streams: " + e);
        return false;
    }

    new ListenFromServer().start();

    return true;
}

void display(String msg) {
    cg.append(sdf.format(new Date()) + ": " + msg + "\n");
}

void sendMessage(Message msg) {
    try {
        sOutput.writeObject(msg);
    } catch (IOException e) {
        display("Exception writing to server: " + e);
    }
}

public void clearMyShots(int id) {
        for (int i = 0; i < 10; i++)
            for (int j = 0; j < 10; j++)
                myShots[i][j] = false;
}

class ListenFromServer extends Thread {
    public void run() {
        Message cm = null;
        while (true) {

            try {
                cm = (Message) sInput.readObject();

            } catch (IOException ioe) {
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }

            switch (cm.getType()) {
            case Message.ID:
                myID = Integer.parseInt(cm.getMessage());
                display("Your ID is " + myID);
                break;
            case Message.HIT:

                String[] partHit = cm.getMessage().split(" ");
                int x = Integer.parseInt(partHit[0]);
                int y = Integer.parseInt(partHit[1]);
                int whoShot = cm.getID();
                if (whoShot == myID) {
                    display("HIT: " + (char) (x + 64) + y);
                    cg.drawX(cg.lblGrid, x, y);

                } else
                    cg.drawX(cg.lblGrid2, x, y);
                break;
            case Message.SHOT:
                display("SHOT: " + cm.getMessage());

                break;
            case Message.MISS: {
                String[] partMiss = cm.getMessage().split(" ");
                x = Integer.parseInt(partMiss[0]);
                y = Integer.parseInt(partMiss[1]);
                whoShot = cm.getID();
                if (whoShot == myID) {
                    display("MISS: " + (char) (x + 64) + y);
                    cg.drawMiss(cg.lblGrid, x, y);
                } else
                    cg.drawMiss(cg.lblGrid2, x, y);
                break;
            }

            case Message.DESTROYED:
                break;
            case Message.CHAT: {
                display("Player " + cm.getID() + ": " + cm.getMessage());
                cm = null;

                break;
            }
            }

        }
    }
}
}

2 个答案:

答案 0 :(得分:3)

我发现原因与主要方法无关。跟随用户@vanje我从Windows命令行运行我的程序,它显示IOException读取我忘记复制到新目录的图像文件。我做了,它正在工作

答案 1 :(得分:0)

要启动Swing应用程序,例如GUI客户端,您需要在main方法中使用以下内容:

javax.swing.SwingUtilities.invokeLater
(
    new Runnable()
    {
        public void run()
        {
            new ClientGUI();
        }
    }
);

这是一个Hello World Sample,可以帮助您了解启动Swing应用程序所需的最低要求。