-1

我正在为我的计算机编程课程创建一个最终项目,并且遇到了一个奇怪的问题,我希望有人可以帮助我。我正在尝试制作一个将在两台计算机之间运行的二十一点程序,但我无法让我的标签文本更改超出它们最初设置的内容。为了制作这个项目,我使用了带有窗口​​构建器插件的 eclipse 编译器。

问题本身存在于标签“lblEnterTextHere”中,当我尝试在 rungame 方法中这样做时,谁的文本不会改变。随机 System.out.print 消息用于检查代码是否甚至到达了运行此方法的位置,并且确实到达了。打印“等待连接”。

注意:我是java新手,这只是12年级的课程,所以请清楚地解释你的答案。这会有很大帮助。此外,这甚至还不是游戏,只是我正在尝试做的事情的骨架。

我的代码:

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import java.io.*;
import java.net.*;
public class Window {

ServerSocket providerSocket;
Socket connection = null;
ObjectOutputStream out;
ObjectInputStream in;
private JFrame frame;
JLabel label_5;
JButton btnNewButton;
JLabel label;
JLabel label_1;
JLabel label_2;
JLabel label_3;
JLabel label_4;
JLabel label_6;
JLabel label_7;
JLabel label_8;
JLabel label_9;
JLabel lblPlayerOneyou;
JLabel lblPlayerother;
JLabel lblNewLabel;
JLabel lblScore;
JLabel lblNewLabel_1;
JLabel lblNewLabel_2;
JButton btnNewButton_1;
JLabel lblStatus;
JLabel lblEnterTextHere;


public static void main(String[] args) {
    Window window = new Window();
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Window window2 = new Window();
                window2.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
    System.out.print("Hello.");
    window.runGame();
}

public Window() {
    frame = new JFrame();
    frame.setBounds(100, 100, 792, 536);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    label_5 = new JLabel("New label");
    label_5.setEnabled(false);
    label_5.setIcon(new ImageIcon("C:\\Users\\Alexander Wong\\Documents\\Cards\\cardBack.jpg"));
    label_5.setBounds(36, 74, 110, 145);
    frame.getContentPane().add(label_5);

    btnNewButton = new JButton("First Draw");
    btnNewButton.setEnabled(false);
    btnNewButton.setBounds(661, 125, 89, 94);
    frame.getContentPane().add(btnNewButton);

    label = new JLabel("New label");
    label.setEnabled(false);
    label.setIcon(new ImageIcon("C:\\Users\\Alexander Wong\\Documents\\Cards\\cardBack.jpg"));
    label.setBounds(156, 74, 110, 145);
    frame.getContentPane().add(label);

    label_1 = new JLabel("New label");
    label_1.setEnabled(false);
    label_1.setIcon(new ImageIcon("C:\\Users\\Alexander Wong\\Documents\\Cards\\cardBack.jpg"));
    label_1.setBounds(276, 74, 110, 145);
    frame.getContentPane().add(label_1);

    label_2 = new JLabel("New label");
    label_2.setEnabled(false);
    label_2.setIcon(new ImageIcon("C:\\Users\\Alexander Wong\\Documents\\Cards\\cardBack.jpg"));
    label_2.setBounds(393, 74, 110, 145);
    frame.getContentPane().add(label_2);

    label_3 = new JLabel("New label");
    label_3.setEnabled(false);
    label_3.setIcon(new ImageIcon("C:\\Users\\Alexander Wong\\Documents\\Cards\\cardBack.jpg"));
    label_3.setBounds(516, 74, 110, 145);
    frame.getContentPane().add(label_3);

    label_4 = new JLabel("New label");
    label_4.setEnabled(false);
    label_4.setIcon(new ImageIcon("C:\\Users\\Alexander Wong\\Documents\\Cards\\cardBack.jpg"));
    label_4.setBounds(36, 267, 110, 145);
    frame.getContentPane().add(label_4);

    label_6 = new JLabel("New label");
    label_6.setEnabled(false);
    label_6.setIcon(new ImageIcon("C:\\Users\\Alexander Wong\\Documents\\Cards\\cardBack.jpg"));
    label_6.setBounds(156, 267, 110, 145);
    frame.getContentPane().add(label_6);

    label_7 = new JLabel("New label");
    label_7.setEnabled(false);
    label_7.setIcon(new ImageIcon("C:\\Users\\Alexander Wong\\Documents\\Cards\\cardBack.jpg"));
    label_7.setBounds(270, 267, 110, 145);
    frame.getContentPane().add(label_7);

    label_8 = new JLabel("New label");
    label_8.setEnabled(false);
    label_8.setIcon(new ImageIcon("C:\\Users\\Alexander Wong\\Documents\\Cards\\cardBack.jpg"));
    label_8.setBounds(393, 267, 110, 145);
    frame.getContentPane().add(label_8);

    label_9 = new JLabel("New label");
    label_9.setEnabled(false);
    label_9.setIcon(new ImageIcon("C:\\Users\\Alexander Wong\\Documents\\Cards\\cardBack.jpg"));
    label_9.setBounds(516, 267, 110, 145);
    frame.getContentPane().add(label_9);

    lblPlayerOneyou = new JLabel("Player one (You)");
    lblPlayerOneyou.setBounds(46, 26, 100, 24);
    frame.getContentPane().add(lblPlayerOneyou);

    lblPlayerother = new JLabel("Player 2 (Other player)");
    lblPlayerother.setBounds(49, 241, 123, 14);
    frame.getContentPane().add(lblPlayerother);

    lblNewLabel = new JLabel("Score:");
    lblNewLabel.setBounds(170, 31, 46, 14);
    frame.getContentPane().add(lblNewLabel);

    lblScore = new JLabel("Score:");
    lblScore.setBounds(170, 242, 46, 14);
    frame.getContentPane().add(lblScore);

    lblNewLabel_1 = new JLabel("0");
    lblNewLabel_1.setBounds(220, 31, 46, 14);
    frame.getContentPane().add(lblNewLabel_1);

    lblNewLabel_2 = new JLabel("??");
    lblNewLabel_2.setBounds(220, 241, 46, 14);
    frame.getContentPane().add(lblNewLabel_2);

    btnNewButton_1 = new JButton("Quit");
    btnNewButton_1.setEnabled(false);
    btnNewButton_1.setBounds(661, 261, 89, 87);
    frame.getContentPane().add(btnNewButton_1);

    lblStatus = new JLabel("Status:");
    lblStatus.setBounds(23, 458, 46, 14);
    frame.getContentPane().add(lblStatus);

    lblEnterTextHere = new JLabel("Enter text here");
    lblEnterTextHere.setBounds(63, 458, 109, 14);
    frame.getContentPane().add(lblEnterTextHere);

}

public void runGame() {
    try {
        System.out.println("Hello.");
        providerSocket = new ServerSocket(5131, 10);
        lblEnterTextHere.setText("Waiting for connection");
        System.out.println("Waiting for connection");
        lblStatus.setText("me");
        connection = providerSocket.accept();
        lblEnterTextHere.setText("Connection received from " + connection.getInetAddress().getHostName());
        out = new ObjectOutputStream(connection.getOutputStream());
        out.flush();
        in = new ObjectInputStream(connection.getInputStream());
        out.writeInt(-1);
        out.flush();
        System.out.println("All systems go.");
        try {
            in.close();
            out.close();
            providerSocket.close();
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }
    } catch (IOException ioException){
        ioException.printStackTrace();
    }

}

}

4

1 回答 1

3

您通过在该线程中EDT放置功能来阻止 。ServerSocket将功能放在runGameSwingWorker,UI 将可以自由更新其组件。

因为您不需要任何退货信息,您可以使用SwingWorker<Void, Void>

请参阅此相关帖子

于 2013-01-09T23:14:18.217 回答