我的JLabel文本不会改变

时间:2013-01-09 23:04:30

标签: java eclipse swing jlabel windowbuilder

我正在为我的计算机编程课程创建一个最终项目,并遇到了一个奇怪的问题,我希望有人可以帮助我。我正在尝试制作一个将在两台计算机之间运行的二十一点程序,但是我无法将标签的文本更改为超出它们最初设置的范围。为了完成这个项目,我使用了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();
    }

}

}

1 个答案:

答案 0 :(得分:3)

您通过在该线程中放置EDT功能来阻止ServerSocket。将runGame中的功能放在SwingWorker中,用户界面可以免费更新其组件。

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

请参阅此related post