1

我创建了一个聊天面板并添加到 Jframe,但该面板未显示。但是我在聊天面板中的 sop 显示在控制台中。任何人请让我知道可能是什么问题

我的框架

public class MyFrame extends JFrame {

MyPanel chatClient;
  String input;

public MyFrame() {

    input = (String)JOptionPane.showInputDialog(null, "Name:", "Connect to chat                  
server", JOptionPane.QUESTION_MESSAGE, null,null, "Test");
input=input.trim();
chatClient = new MyPanel("localhost",input);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(chatClient);

 }
  public static void main(String...args){
    new MyFrame();
  }
}

我的面板:

public class MyPanel extends JPanel{
  ChatClient chatClient;

  public MyPanel(String host, String uid) {
    chatClient= new ChatClient(host,uid);
    add(chatClient.getChatPanel());
    this.setVisible(true);
  }
}

聊天面板:

public class ChatClient  {

  Client client;
  String name;
  ChatPanel chatPanel;
  String hostid;

  public ChatClient(String host,String uid){
    client = new Client();
    client.start();
    System.out.println("in constructor");
    Network.register(client);

    client.addListener(new Listener(){

      public void connected(Connection connection){
        System.out.println("in client connected method");
        Network.RegisterName registerName = new Network.RegisterName();
        registerName.name=name;
        client.sendTCP(registerName);
      }

      public void received(Connection connection,Object object){
        System.out.println("in client received method");

          if (object instanceof Network.UpdateNames) {
                  Network.UpdateNames updateNames = (Network.UpdateNames)object;
                  //chatFrame.setNames(updateNames.names);

                  System.out.println("got it message");
                  return;
          }

          if (object instanceof Network.ChatMessage) {
                  Network.ChatMessage chatMessage = (Network.ChatMessage)object;
                  //chatFrame.addMessage(chatMessage.text);
                    System.out.println("send it message");
                  return;
          }

      }



    }); // end of listner

    name=uid.trim();
    hostid=host.trim();
    chatPanel = new ChatPanel(hostid,name);

    chatPanel.setSendListener(new Runnable(){

      public void run(){
        Network.ChatMessage chatMessage = new Network.ChatMessage();
        chatMessage.chatMessage=chatPanel.getSendText();
        client.sendTCP(chatMessage);
      }
    });

    new Thread("connect"){
      public void run(){
        try{
          client.connect(5000, hostid,Network.port);
        }catch(IOException e){
          e.printStackTrace();
        }
      }
    }.start();

  }//end of constructor

  static public class ChatPanel extends JPanel{

    CardLayout cardLayout;
    JList messageList,nameList;
    JTextField sendText;
    JButton sendButton;
    JPanel topPanel,bottomPanel,panel;

    public ChatPanel(String host,String user){
      setSize(600, 200);
      this.setVisible(true);
      System.out.println("Chat panel "+host+"user: "+user);
      {
        panel = new JPanel(new BorderLayout());
        {
          topPanel = new JPanel(new GridLayout(1,2));
          panel.add(topPanel); 
          {
            topPanel.add(new JScrollPane(messageList=new JList()));
            messageList.setModel(new DefaultListModel());
          } 
          {
            topPanel.add(new JScrollPane(nameList=new JList()));
            nameList.setModel(new DefaultListModel());
          }
          DefaultListSelectionModel disableSelections = new DefaultListSelectionModel() {
                                                          public void setSelectionInterval (int index0, int index1) {
                                                          }
                                                  };

          messageList.setSelectionModel(disableSelections);
          nameList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        } 
        {
          bottomPanel = new JPanel(new GridBagLayout());
          panel.add(bottomPanel,BorderLayout.SOUTH);

          bottomPanel.add(sendText=new JTextField(),new GridBagConstraints(0,0,1,1,1,0,GridBagConstraints.CENTER,GridBagConstraints.BOTH,new Insets(0,0,0,0),0,0));
          bottomPanel.add(sendButton=new JButton(),new GridBagConstraints(1,0,1,1,0,0,GridBagConstraints.CENTER,0,new Insets(0,0,0,0),0,0));
        }

      }

      sendText.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
          sendButton.doClick();
        }
      });

    }


    public void setSendListener (final Runnable listener) {
            sendButton.addActionListener(new ActionListener() {
                    public void actionPerformed (ActionEvent evt) {
                            if (getSendText().length() == 0) return;
                            listener.run();
                            sendText.setText("");
                            sendText.requestFocus();
                    }
            });
    }

    public String getSendText () {
            return sendText.getText().trim();
    }

    public void setNames (final String[] names) {
      EventQueue.invokeLater(new Runnable(){
        public void run(){
          DefaultListModel model = (DefaultListModel)nameList.getModel();
          model.removeAllElements();
          for(String name:names)
            model.addElement(name);
        }
      });

    }

    public void addMessage (final String message) {
            EventQueue.invokeLater(new Runnable() {
                    public void run () {
                            DefaultListModel model = (DefaultListModel)messageList.getModel();
                            model.addElement(message);
                            messageList.ensureIndexIsVisible(model.size() - 1);
                    }
            });
    }

  }

  public JPanel getChatPanel(){
    return chatPanel;
  }

}

编辑 1

public class ChatPanel {
  ChatP caht;
  public ChatPanel1() {
    caht=new ChatP();
  }
  static class ChatP extends JPanel{
    JPanel panel;
    public ChatP(){
      panel = new JPanel();
      panel.add(new JLabel("hi from chat panel"));
    }
  }

  public JPanel getChatPanel(){
    return caht;
  }
}
4

3 回答 3

4

打电话pack()。拼凑在一起的代码仍然有很多问题和奇怪的方面,但显示..

在此处输入图像描述

import java.awt.*;
import javax.swing.*;

public class MyFrame extends JFrame {

    MyPanel chatClient;
    String input;

    public MyFrame() {
        chatClient = new MyPanel("localhost","input");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        add(chatClient);

        pack();
        // Do last!
        setVisible(true);
    }
    public static void main(String...args){
        new MyFrame();
    }
}

class MyPanel extends JPanel{

    public MyPanel(String host, String uid) {
        ChatPanel chatPanel = new ChatPanel();
        add(chatPanel.getChatPanel());
    }
}

class ChatPanel {
    ChatP caht;

    public ChatPanel() {
        caht=new ChatP();
    }

    static class ChatP extends JPanel{
        public ChatP(){
            add(new JLabel("hi from chat panel"));
        }
    }

    public JPanel getChatPanel(){
        return caht;
    }
}

笔记

不包括电池。该来源是诊断问题并提出解决方案所需的极简代码。为了尽快获得更好的帮助,请发布SSCCE

  • 不要扩展框架或面板,只需保留对每个的引用。
  • 在 EDT 上启动和更新 GUI。有关更多详细信息,请参阅Swing 中的并发。
于 2012-06-29T09:12:12.957 回答
2

setVisible(true)(在您的框架上)应该是您的最后一行,或者您必须revalidate()在将组件添加到框架后调用框架内容窗格。

于 2012-06-29T09:12:40.563 回答
2

差不多好了。你反过来得到了这段代码。

setVisible(true);
add(chatClient);

它应该是:

add(chatClient);
pack();
setVisible(true);

另外,请注意pack()确保您的框架已准备好可见的调用。

于 2012-06-29T09:19:00.420 回答