简体   繁体   English

我的JLabel文字不会改变

[英]My JLabel text will not change

I am creating a final project for my computer programming course, and have run into a strange problem that I'm hoping someone could possibly help me with. 我正在为我的计算机编程课程创建一个最终项目,但遇到了一个奇怪的问题,我希望有人可以为我提供帮助。 I am attempting to make a blackjack program which will run between two computers, but am having trouble getting the text of my labels to change beyond what they are initially set to. 我试图制作一个可以在两台计算机之间运行的二十一点程序,但是我无法将标签的文本更改为超出最初设置的范围。 To make this project I am using the eclipse compiler with the window builder addon. 为了创建这个项目,我使用带有窗口构建器插件的eclipse编译器。

The problem itself lies within the label "lblEnterTextHere", who's text will not change when i try to do so in the rungame method. 问题本身位于标签“ lblEnterTextHere”内,当我尝试在rungame方法中进行更改时,谁的文本不会更改。 The random System.out.print messages were to check if the code even reaches the point where this method is run, and it does. 随机的System.out.print消息将检查代码是否到达运行此方法的地步,并且确实可以。 "Waiting for connection" is printed. 打印“等待连接”。

Note: I'm new to java, and this is merely a grade 12 course, so please explain your answer with clarity. 注意:我是Java的新手,这只是12年级的课程,因此请清楚解释您的答案。 It would help a lot. 这会很有帮助。 Also This isn't even a game yet, just the skeleton of what I'm trying to do. 而且这还不是游戏,只是我想要做的事情的骨架。

My Code: 我的代码:

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();
    }

}

} }

You are blocking the EDT by placing ServerSocket functionality in that Thread. 您正在通过在该线程中放置ServerSocket功能来阻止EDT Place the functionality found in runGame in a SwingWorker and the UI will be free to update its components. runGame中找到的功能放在SwingWorker中 ,UI可以自由更新其组件。

As you are don't require any return information you can use SwingWorker<Void, Void> 由于您不需要任何返回信息,因此可以使用SwingWorker<Void, Void>

See this related post . 请参阅此相关文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM