强制绘制jframe?

时间:2011-11-22 14:05:22

标签: java swing client

过去几天我一直试图自己解决这个问题,但我似乎无法找到解决方案......我如何强制绘制JFrame及其中的所有内容?我有一个聊天程序(客户端/服务器方法),服务器类和客户端是相同的代码?但我似乎无法让客户端成为一个节目!

import java.awt.event.ActionEvent;

public class Chat extends JFrame implements Runnable, ActionListener, WindowListener{



    private static final long serialVersionUID = 1L;
    private JPanel contentPane;
    private JTextField line;

    // TCP Components
    private Socket channel = null;

    private JScrollPane scrollPane;
    private String toBeSent="";
    private final String END_CHAT_SESSION = new Character((char)0).toString();
    private PrintWriter out;
    private boolean channelIsStillOpen = true;
    private Socket channel;
    private static JTextArea chatText;


   public Chat(Socket channel) {

        this.channel=channel;

        addWindowListener(this);

        setTitle("Remote Administrator");
        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);

        line = new JTextField();
        line.setBounds(17, 207, 289, 40);
        line.setColumns(10);

        JButton send = new JButton("Send");

        send.addActionListener(this);

        send.setBounds(312, 214, 105, 25);
        contentPane.setLayout(null);

        scrollPane = new JScrollPane();
        scrollPane.setBounds(17, 5, 399, 196);
        scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        contentPane.add(scrollPane);

        chatText = new JTextArea();
        chatText.setLineWrap(true);
        chatText.setEditable(false);
        chatText.setEnabled(false);
        scrollPane.setViewportView(chatText);
        contentPane.add(line);
        contentPane.add(send);

    }

    @Override
    public void run() {

        try {
            channel=new Socket(address, port);
            System.out.println("Chat Connection accepted");
        } catch (IOException e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        }

        BufferedReader in = null;
        try {
            in = new BufferedReader(new InputStreamReader(channel.getInputStream()));
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        out = null;
        try {
            out = new PrintWriter(channel.getOutputStream(),true);
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        String s="";

        do {
            // Send data
            if (toBeSent.length()!= 0) {
               out.println(toBeSent);
               toBeSent="";
            }

            // Receive data
            try {
                if (in.ready()) {
                    s = in.readLine();
                    if ((s != null) &&  (s.length() != 0) && !s.equals(END_CHAT_SESSION)) {
                          chatText.append("INCOMING: " + s + "\n");
                    }
                    if(s.equals(END_CHAT_SESSION)){
                        chatText.append("Client disconnected.");
                    }
                 }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
      } while(!s.equals(END_CHAT_SESSION));

        channelIsStillOpen=false;

        line.setEditable(false);
        line.setEnabled(false);

        if(channel!=null){
            try {
                channel.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            channel=null;
        }

    }

    @Override
    public void windowActivated(WindowEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void windowClosed(WindowEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void windowClosing(WindowEvent arg0) {

        if(channelIsStillOpen){
            out.println(END_CHAT_SESSION);
        }

        dispose();

    }

    @Override
    public void windowDeactivated(WindowEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void windowDeiconified(WindowEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void windowIconified(WindowEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void windowOpened(WindowEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void actionPerformed(ActionEvent arg0) {

        String s = line.getText();

        if (!s.equals("")) {
           chatText.append("OUTGOING: " + s + "\n");

           // Send the string
           toBeSent=s;
        }

        line.setText("");

    }
}

编辑:我甚至编辑了代码,以便使用的类始终相同!没有!

1 个答案:

答案 0 :(得分:0)

我认为你在构造函数的末尾错过了这个:

this.setVisible(true);

另外,你为什么不设置? setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);,为什么要保持应用程序的打开?