文本未显示在GUI中的Jframe中

时间:2015-04-27 19:14:20

标签: java swing user-interface jtextfield

所以我已经开始使用这个程序几天了,这是一个基本的客户端服务器聊天室。我尝试使用它,以便在启动客户端时,弹出的gui在portnumber,servername和textbox JTextFields中包含文本。昨天是这样的,但是我改变了一些东西,现在gui在文本字段中没有文字。该代码位于displaysettings方法中,该方法在try catch块的开头运行。任何人都知道它为什么不工作?

import java.net.*;
import java.util.*;
import java.io.*;
import java.awt.event.*;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class clienttry2 extends JFrame implements ActionListener {
    JPanel panel1 = new JPanel();
    JPanel panel2 = new JPanel();
    JPanel panel3 = new JPanel();
    JButton send = new JButton("Send");
    JButton connect = new JButton("Connect");
    JLabel server = new JLabel("Server");
    JLabel port = new JLabel("Port");
    JButton disconnect = new JButton("Disconnect"); 
    static JTextField servername = new JTextField(10);
    static JTextField portnumber = new JTextField(10);
    static JTextField textbox = new JTextField(40);
    JTextArea chatbox = new JTextArea(20,45);

    static Boolean isconnected = false;
    static Boolean sending = false;
    static Socket server1;
    static ObjectInputStream in;
    static ObjectOutputStream out;

    public clienttry2(){
        setTitle("Chat Room");
        setLayout(new java.awt.FlowLayout());
        setSize(600,500);
        panel1.add(chatbox); //has all the chats
        panel2.add(textbox); //area to type new messages into
        panel2.add(send); send.addActionListener(this);//send button
        panel3.add(server);
        panel3.add(servername);
        panel3.add(port);       
        panel3.add(portnumber);
        panel3.add(connect); connect.addActionListener(this);
        panel3.add(disconnect); disconnect.addActionListener(this); disconnect.setEnabled(false);
        add(panel1);
        add(panel2);
        add(panel3);
    }

    public static void main(String[] args)throws Exception {
        Client display = new Client();
        display.setVisible(true);

        try{
            displaysettings();
            connect();
            setup();
            String message;
            message = (String) in.readObject();
            System.out.println(message);
        } catch (UnknownHostException e1) {
            e1.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }

    private static void displaysettings() {
        portnumber.setText("3333");
        servername.setText("localhost");
        textbox.setText("This is where you type your messages to send to others on the server!");
    }

    private static void connect() throws UnknownHostException, IOException {
        server1 = new Socket("localhost", 3333);
    }

    private static void setup() throws IOException {
        out = new ObjectOutputStream(server1.getOutputStream());
        out.flush();
        in = new ObjectInputStream(server1.getInputStream());
    }

    public void actionPerformed(ActionEvent e){
        if(e.getSource()==connect)
        {
            System.out.println("connected");
            isconnected = true;
            connect.setEnabled(false);
            disconnect.setEnabled(true);
        }

        if(e.getSource()==send)
        {
            System.out.println("sending chat");
            sending = true;
        }
        if(e.getSource()==disconnect)
        {
            try {
                server1.close();
                out.close();
                isconnected = false;
                connect.setEnabled(true);
                disconnect.setEnabled(false);

            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }

1 个答案:

答案 0 :(得分:-2)

除非告知,否则

Swing组件不会更新。设置文本后,请致电:

textbox.repaint();

强制GUI更新。