【问题标题】:Text not showing up in Jframes in my GUI我的 GUI 中的 Jframe 中没有显示文本
【发布时间】:2015-04-27 19:14:19
【问题描述】:

所以我已经断断续续地研究这个程序几天了,它是一个基本的客户端服务器聊天室。我试图让它在你启动客户端时,弹出的 gui 在端口号、服务器名和文本框JTextFields 中包含文本。昨天是这种情况,但我已经改变了一些东西,现在 gui 出现在文本字段中没有文本。该代码位于在 try catch 块的开头运行的 displaysettings 方法中。有人知道为什么它不起作用吗?

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

【问题讨论】:

  • 你用过调试器吗?
  • 您没有像应有的那样将任何 Swing 代码发布到事件调度线程。不确定这是否是实际问题,因为我没有检查整个事情。创建一个MCVE,谁知道呢,您自己可能会偶然发现这个问题。如果您没有找到它,请将您的 MCVE 编辑到您的帖子中。我强烈建议多学习一点 Swing,以及研究代码设计,这样您就不会将所有内容混合在一起,从而无法孤立地测试事物
  • 我建议将 main 方法中的所有代码包装到传递给 SwingUtilities.invokeAndWait 的 Runnable 中,以便在 EDT 上构建 UI。见docs.oracle.com/javase/tutorial/uiswing/concurrency/…

标签: java swing user-interface jtextfield


【解决方案1】:

Swing 除非被告知,否则组件不会更新。设置文本后,调用:

textbox.repaint();

强制 GUI 更新。

【讨论】:

  • 请注意,我告诉你两者都做只是因为我不知道在这个场合使用哪个,所以我同时使用。如果有人编辑的是正确的,我很乐意接受编辑。
  • revalidate 用于在已经可见的容器中添加/删除组件时。 repaint 只是请求重绘管理器重绘所有内容,而不考虑组件层次结构。更不用说,在设置组件的文本时不需要重新验证,只有在添加、删除或更改大小时才需要重新验证。不能说这是一个正确的答案;你测试了吗?
  • @VinceEmigh 所以repaint 是吗?
  • 应该不需要调用 repaint() - 调用 setText 应该调用适当的侦听器来重绘组件(假设调用是在 EDT 上 - 在原始代码中不是)跨度>
  • 类名应以大写字符开头,并且类中的单词也应大写。 (clientry2 应该是 ClientTry2)。如果这是一个测试班,不要紧,遵守标准!摆脱所有静态变量。文本字段是静态的,但您添加文本字段的面板不是静态的???我不知道为什么你认为有些应该是静态的而不是其他的?再次,保持一致。
猜你喜欢
  • 2016-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-29
  • 1970-01-01
相关资源
最近更新 更多