class Server1 {
private JTextField userText;
private JTextArea display;
private ObjectOutputStream output;
private ObjectInputStream input;
private ServerSocket server;
private Socket connection;
JFrame frame = new JFrame("Server");
public void changeVisibility() {
frame.setVisible(true);
}
public Server1() {
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
userText = new JTextField();
display = new JTextArea();
display.setBackground(new Color(224, 235, 235));
JButton send = new JButton("Send");
try {
Image img = ImageIO.read(getClass().getResource("sendmsg.png"));
Image dimg = img.getScaledInstance(20, 20, Image.SCALE_SMOOTH);
send.setIcon(new ImageIcon(dimg));
send.setHorizontalAlignment(SwingConstants.CENTER);
} catch (Exception ex) {
System.out.println(ex);
}
send.setBounds(300, 25, 100, 40);
send.setBackground(new Color(75, 9, 155));
send.setForeground(Color.white);
send.setFocusPainted(false);
userText.setBounds(10, 25, 300, 40);
LineBorder panelBorder = new LineBorder(new Color(75, 9, 155), 2);
TitledBorder TitlePanel1 = new TitledBorder(panelBorder, "CHAT", TitledBorder.LEFT, TitledBorder.TOP, new Font("Arial", Font.BOLD, 20), new Color(75, 9, 155));
panel1.setBorder(TitlePanel1);
panel1.setBounds(30, 30, 420, 300);
display.setBounds(20, 50, 380, 200);
panel1.add(display);
display.setBorder(new LineBorder(new Color(92, 138, 138), 1));
panel1.setLayout(null);
LineBorder panelBorder2 = new LineBorder(new Color(75, 9, 155), 2);
TitledBorder TitlePanel2 = new TitledBorder(panelBorder2, "MESSAGE", TitledBorder.LEFT, TitledBorder.TOP, new Font("Arial", Font.BOLD, 20), new Color(75, 9, 155));
panel2.add(send);
panel2.add(userText);
panel2.setBorder(TitlePanel2);
panel2.setBounds(30, 350, 420, 80);
panel2.setLayout(null);
// Backend starts now
userText.setEditable(false);
display.setEditable(false);
userText.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
sendMessage(event.getActionCommand());
userText.setText("");
}
}
);
send.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
sendMessage(userText.getText());
userText.setText("");
}
});
frame.add(panel1);
frame.add(panel2);
frame.setLayout(null);
frame.setSize(500, 500);
frame.setLocationRelativeTo(null);
//frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void startRunning(){
try {
server = new ServerSocket(9999);
while(true) {
try{
waitForConnection();
setupStreams();
whileChatting();
}catch(EOFException eofException){
System.out.println("\ " + eofException.getMessage());
showMessage("\n Server ended the connection!");
}catch(IOException ioException){
ioException.printStackTrace();
}finally{
closeCrap();
}
}
}
catch(Exception exe) {
exe.printStackTrace();
}
}
public void waitForConnection() throws Exception{
showMessage("Waiting for someone to connect!");
connection = server.accept();
showMessage("\n Now Connected");
}
public void setupStreams() throws Exception{
output = new ObjectOutputStream(connection.getOutputStream());
output.flush();
input = new ObjectInputStream(connection.getInputStream());
showMessage("\n The streams are now set up! \n");
}
public void whileChatting() throws IOException{
String message = "You are now connected";
showMessage(message);
ableToType(true);
do{
try{
message = (String) input.readObject();
showMessage("\n" + message);
}catch(ClassNotFoundException classNotFoundException){
showMessage("Unknown data received!");
}
}while(!message.equals("CLIENT - END"));
}
public void closeCrap(){
showMessage("\n Closing Connections... \n");
ableToType(false);
try{
output.close();
input.close();
connection.close();
}catch(IOException ioException){
ioException.printStackTrace();
}
}
private void sendMessage(String message){
try{
output.writeObject("SERVER - " + message);
output.flush();
showMessage("\nSERVER -" + message);
}catch(IOException ioException){
display.append("\n ERROR: CANNOT SEND MESSAGE, PLEASE RETRY");
}
}
private void showMessage(final String text){
SwingUtilities.invokeLater(
new Runnable(){
public void run(){
display.append(text);
}
}
);
}
private void ableToType(final boolean tof){
SwingUtilities.invokeLater(
new Runnable(){
public void run(){
userText.setEditable(tof);
}
}
);
}
/*public static void main(String a[]) {
Server1 s1 = new Server1();
s1.changeVisibility();
s1.startRunning();
} */
}
class Login {
Login()
{
JFrame f= new JFrame("Login");
JButton Login = new JButton("Login");
Login.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
Server1 s1 = new Server1();
s1.changeVisibility();
s1.startRunning();
}
});
JPanel panel=new JPanel();
panel.setLayout(null);
panel.setBounds(20,100,340,250);
Login.setBounds(50, 160, 100, 30);
panel.add(Login);
TitledBorder border = new TitledBorder("LOGIN");
border.setTitleJustification(TitledBorder.LEFT); // Center of panel
border.setTitlePosition(TitledBorder.TOP);
border.setTitleFont(new Font("Arial",Font.BOLD, 20));
border.setTitleColor(new Color(66, 220, 244));
panel.setBorder(border);
f.add(panel);
//f.getGlassPane().setVisible(true);
f.setSize(400,420);
f.setResizable(false);
f.setLayout(null);
f.setLocationRelativeTo(null);
f.setVisible(true);
// Image icon = Toolkit.getDefaultToolkit().getImage("ToolTip.png");
f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
}
public static void main(String args[])
{
new Login();
}
}
单击按钮时无法打开此服务器框架。它不显示GUI组件,并且框架也冻结。当我只运行服务器框架时,它可以正常工作,但我不想在单击按钮时将其打开。我试图弄清楚,但徒劳。有什么建议么?与客户端框架相同。它没有显示GUI组件。 请帮我解决一下这个。我必须制作一个多客户端聊天应用程序。
答案 0 :(得分:0)
因此,基于此...
Login.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
Server1 s1 = new Server1();
s1.changeVisibility();
s1.startRunning();
}
});
还有这个...
public void startRunning(){
try {
server = new ServerSocket(9999);
while(true) {
try{
waitForConnection();
setupStreams();
whileChatting();
}catch(EOFException eofException){
System.out.println("Meny pakra hy issko " + eofException.getMessage());
showMessage("\n Server ended the connection!");
}catch(IOException ioException){
ioException.printStackTrace();
}finally{
closeCrap();
}
}
可以肯定地说,您不了解Swing的事件分发API的工作原理。
本质上,您是在事件调度线程中建立一个无限循环,阻止其处理任何新事件,包括绘画请求。
首先查看Concurrency in Swing了解更多详细信息,然后查看Worker Threads and SwingWorker了解可行的解决方案