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.
The problem itself lies within the label "lblEnterTextHere", who's text will not change when i try to do so in the rungame method. The random System.out.print messages were to check if the code even reaches the point where this method is run, and it does. "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. 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();
}
}
}