Java - 使用 AWT 更改 JFrame 的标题
Java - Using AWT to change title of JFrame
我正在编写一个简单的 multi-threaded client/server 聊天系统。项目要求指定:“连接仅在单击连接按钮时发生。断开连接按钮应断开连接 connection.User 在按下连接按钮之前输入用户名,如上检索用户名后,还应将其添加到标题栏框架的。”基本上,我有连接按钮 hooked-up 和 running.When 用户输入他的名字并按连接,我如何更改主框架的标题。请帮忙,这是我的第一个套接字编程项目,我对这个项目感到非常难过!谢谢大家
客户:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.ArrayList;
public class ClientFrame extends Frame {
public ClientFrame() {
setSize(500, 500);
setTitle("Chat Client");
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent We) {
System.exit(0);
}
});
add(new ClientPanel(), BorderLayout.CENTER);
setVisible(true);
}
public static void main(String[] args) {
new ClientFrame();
}
} // end ClientFrame
class ClientPanel extends Panel implements ActionListener, Runnable {
TextField tf;
TextArea ta;
List list;
Button connect, disconnect;
Socket socketToServer;
PrintWriter pw;
BufferedReader br;
Thread t;
String userName;
public ClientPanel() {
setLayout(new BorderLayout());
tf = new TextField();
ta = new TextArea();
list = new List();
connect = new Button("Connect");
disconnect = new Button("Disconnect");
Panel bPanel = new Panel();
bPanel.add(connect);
disconnect.setEnabled(false);
bPanel.add(disconnect);
tf.addActionListener(this);
add(tf, BorderLayout.NORTH);
add(ta, BorderLayout.CENTER);
add(list, BorderLayout.EAST);
add(bPanel, BorderLayout.SOUTH);
connect.addActionListener(this);
disconnect.addActionListener(this);
} // end ClientPanel constructor
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == tf) {
System.out.println(tf + " " + tf.getText());
String temp = tf.getText();
pw.println(userName + ": " + temp);
tf.setText("");
} else if (ae.getSource() == connect) {
if (tf.getText() == null || tf.getText().equals("")) {
ta.append("Must enter a name to connect\n");
} else {
userName = tf.getText();
connect.setEnabled(false);
disconnect.setEnabled(true);
tf.setText("");
try {
socketToServer = new Socket("127.0.0.1", 3000);
pw = new PrintWriter(new OutputStreamWriter(socketToServer.getOutputStream()), true);
br = new BufferedReader(new InputStreamReader(socketToServer.getInputStream()));
} catch (UnknownHostException uhe) {
System.out.println(uhe.getMessage());
} catch (IOException ioe) {
System.out.println(ioe.getMessage());
}
}
t = new Thread(this);
t.start();
pw.println(userName);
pw.println(userName + " has entered the chat.");
} else if (ae.getSource() == disconnect) {
connect.setEnabled(true);
disconnect.setEnabled(false);
try {
tf.setText("");
try {
socketToServer = new Socket("127.0.0.1", 3000);
pw = new PrintWriter(new OutputStreamWriter(socketToServer.getOutputStream()), true);
br = new BufferedReader(new InputStreamReader(socketToServer.getInputStream()));
} catch (UnknownHostException uhe) {
System.out.println(uhe.getMessage());
} catch (IOException ioe) {
System.out.println(ioe.getMessage());
}
t = new Thread(this);
t.start();
pw.println(userName);
pw.println(userName + " has Disconnected the chat.");
t.interrupt();
socketToServer.close();
} catch (IOException ioe) {
System.out.println(ioe.getMessage());
}
}
} // end actionPerformed
public void run() {
try {
for (;;) {
try {
String temp = br.readLine();
ta.append(temp + "\n");
} catch (IOException ioe) {
System.out.println(ioe.getMessage());
}
Thread.sleep(10);
}
} catch (InterruptedException e) {
pw.println("Disconnected.");
}
} // end run
} // end ClientPanel
服务器:
import java.io.*;
import java.net.*;
import java.util.ArrayList;
import java.awt.*;
public class ThreadedServerWithPresence {
public static void main(String[] args) {
ArrayList<ThreadedHandlerWithPresence> handlers;
try {
handlers = new ArrayList<ThreadedHandlerWithPresence>();
ServerSocket s = new ServerSocket(3000);
for (;;) {
Socket incoming = s.accept();
new ThreadedHandlerWithPresence(incoming,
handlers).start();
}
} catch (Exception e) {
System.out.println(e);
}
}
}
class ThreadedHandlerWithPresence extends Thread {
Socket incoming;
ArrayList<ThreadedHandlerWithPresence> handlers;
PrintWriter pw;
BufferedReader br;
String userName;
public ThreadedHandlerWithPresence(Socket i,
ArrayList<ThreadedHandlerWithPresence> handlers) {
incoming = i;
this.handlers = handlers;
handlers.add(this);
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserName() {
return userName;
}
public void run() {
try {
br = new BufferedReader(new InputStreamReader(incoming.getInputStream()));
pw = new PrintWriter(new OutputStreamWriter(incoming.getOutputStream()), true);
String firstLine = br.readLine();
setUserName(firstLine);
for (;;) {
String temp = br.readLine();
if (temp == null) {
break;
}
System.out.println("Message read: " + temp);
for (int i = 0; i < handlers.size(); i++) {
handlers.get(i).pw.println(temp);
}
}
} catch (Exception e) {
System.out.println(e);
} finally {
handlers.remove(this);
}
}
}
很简单。如果您将 class 扩展到 Frame,您只需将 ClientFrame class 传递给 ClientPanel,如下所示:
add(new ClientPanel(this), BorderLayout.CENTER);
在 ClientPanle 构造函数中,您可以执行如下操作:
JFrame clientFrame;
public ClientPanel(JFrame clientFrame) {
this.clientFrame=clientFrame;
最后你调用了想要的方法:
clientFrame.setTitle(userName);
我正在编写一个简单的 multi-threaded client/server 聊天系统。项目要求指定:“连接仅在单击连接按钮时发生。断开连接按钮应断开连接 connection.User 在按下连接按钮之前输入用户名,如上检索用户名后,还应将其添加到标题栏框架的。”基本上,我有连接按钮 hooked-up 和 running.When 用户输入他的名字并按连接,我如何更改主框架的标题。请帮忙,这是我的第一个套接字编程项目,我对这个项目感到非常难过!谢谢大家
客户:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.ArrayList;
public class ClientFrame extends Frame {
public ClientFrame() {
setSize(500, 500);
setTitle("Chat Client");
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent We) {
System.exit(0);
}
});
add(new ClientPanel(), BorderLayout.CENTER);
setVisible(true);
}
public static void main(String[] args) {
new ClientFrame();
}
} // end ClientFrame
class ClientPanel extends Panel implements ActionListener, Runnable {
TextField tf;
TextArea ta;
List list;
Button connect, disconnect;
Socket socketToServer;
PrintWriter pw;
BufferedReader br;
Thread t;
String userName;
public ClientPanel() {
setLayout(new BorderLayout());
tf = new TextField();
ta = new TextArea();
list = new List();
connect = new Button("Connect");
disconnect = new Button("Disconnect");
Panel bPanel = new Panel();
bPanel.add(connect);
disconnect.setEnabled(false);
bPanel.add(disconnect);
tf.addActionListener(this);
add(tf, BorderLayout.NORTH);
add(ta, BorderLayout.CENTER);
add(list, BorderLayout.EAST);
add(bPanel, BorderLayout.SOUTH);
connect.addActionListener(this);
disconnect.addActionListener(this);
} // end ClientPanel constructor
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == tf) {
System.out.println(tf + " " + tf.getText());
String temp = tf.getText();
pw.println(userName + ": " + temp);
tf.setText("");
} else if (ae.getSource() == connect) {
if (tf.getText() == null || tf.getText().equals("")) {
ta.append("Must enter a name to connect\n");
} else {
userName = tf.getText();
connect.setEnabled(false);
disconnect.setEnabled(true);
tf.setText("");
try {
socketToServer = new Socket("127.0.0.1", 3000);
pw = new PrintWriter(new OutputStreamWriter(socketToServer.getOutputStream()), true);
br = new BufferedReader(new InputStreamReader(socketToServer.getInputStream()));
} catch (UnknownHostException uhe) {
System.out.println(uhe.getMessage());
} catch (IOException ioe) {
System.out.println(ioe.getMessage());
}
}
t = new Thread(this);
t.start();
pw.println(userName);
pw.println(userName + " has entered the chat.");
} else if (ae.getSource() == disconnect) {
connect.setEnabled(true);
disconnect.setEnabled(false);
try {
tf.setText("");
try {
socketToServer = new Socket("127.0.0.1", 3000);
pw = new PrintWriter(new OutputStreamWriter(socketToServer.getOutputStream()), true);
br = new BufferedReader(new InputStreamReader(socketToServer.getInputStream()));
} catch (UnknownHostException uhe) {
System.out.println(uhe.getMessage());
} catch (IOException ioe) {
System.out.println(ioe.getMessage());
}
t = new Thread(this);
t.start();
pw.println(userName);
pw.println(userName + " has Disconnected the chat.");
t.interrupt();
socketToServer.close();
} catch (IOException ioe) {
System.out.println(ioe.getMessage());
}
}
} // end actionPerformed
public void run() {
try {
for (;;) {
try {
String temp = br.readLine();
ta.append(temp + "\n");
} catch (IOException ioe) {
System.out.println(ioe.getMessage());
}
Thread.sleep(10);
}
} catch (InterruptedException e) {
pw.println("Disconnected.");
}
} // end run
} // end ClientPanel
服务器:
import java.io.*;
import java.net.*;
import java.util.ArrayList;
import java.awt.*;
public class ThreadedServerWithPresence {
public static void main(String[] args) {
ArrayList<ThreadedHandlerWithPresence> handlers;
try {
handlers = new ArrayList<ThreadedHandlerWithPresence>();
ServerSocket s = new ServerSocket(3000);
for (;;) {
Socket incoming = s.accept();
new ThreadedHandlerWithPresence(incoming,
handlers).start();
}
} catch (Exception e) {
System.out.println(e);
}
}
}
class ThreadedHandlerWithPresence extends Thread {
Socket incoming;
ArrayList<ThreadedHandlerWithPresence> handlers;
PrintWriter pw;
BufferedReader br;
String userName;
public ThreadedHandlerWithPresence(Socket i,
ArrayList<ThreadedHandlerWithPresence> handlers) {
incoming = i;
this.handlers = handlers;
handlers.add(this);
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserName() {
return userName;
}
public void run() {
try {
br = new BufferedReader(new InputStreamReader(incoming.getInputStream()));
pw = new PrintWriter(new OutputStreamWriter(incoming.getOutputStream()), true);
String firstLine = br.readLine();
setUserName(firstLine);
for (;;) {
String temp = br.readLine();
if (temp == null) {
break;
}
System.out.println("Message read: " + temp);
for (int i = 0; i < handlers.size(); i++) {
handlers.get(i).pw.println(temp);
}
}
} catch (Exception e) {
System.out.println(e);
} finally {
handlers.remove(this);
}
}
}
很简单。如果您将 class 扩展到 Frame,您只需将 ClientFrame class 传递给 ClientPanel,如下所示:
add(new ClientPanel(this), BorderLayout.CENTER);
在 ClientPanle 构造函数中,您可以执行如下操作:
JFrame clientFrame;
public ClientPanel(JFrame clientFrame) {
this.clientFrame=clientFrame;
最后你调用了想要的方法:
clientFrame.setTitle(userName);