package com.vampire;import java.awt.*;import javax.swing.*;public class CharClient extends JFrame{
private static final long serialVersionUID = -989848674198876434L; public CharClient(){
setTitle("CharClient");
Container rq = getContentPane();
setVisible(true);
setBounds(300,100,260,350);
setLayout(new BorderLayout());

JPanel p1 = new JPanel(new BorderLayout());
JTextField wbk = new JTextField();//文本框
p1.add(BorderLayout.CENTER,wbk);
JButton an = new JButton("发送");   //按纽
p1.add(BorderLayout.EAST,an);
rq.add(BorderLayout.SOUTH,p1);

JTextArea wby = new JTextArea("欢迎使用CharClient\n",60,0);//文本域
wby.setLineWrap(true);
JScrollPane p2 = new JScrollPane(wby);
rq.add(BorderLayout.CENTER,p2);
}

public static void main(String[] args){
new CharClient();
}
}
不知道为什么程序运行后只能改变面板大小才能显示内容....求解....
还有本人自学新手....程序有哪些写的不好的地方求指教...谢谢了!!!!

解决方案 »

  1.   

    去看API...有setSize(), setPreferredSize(),setMaximumSize(), setMinimumSize()等,都可以满足你的需求
      

  2.   

    把 setVisible()方法放在最后调用就可以解决这个问题了,就是在new CharClient().setVisible(true),已测试可行。
    你在一些组件还为初始化的时候就调用setVisible()方法会对后期的一些组件属性的设置造成一些影响
      

  3.   

    楼主的add方法写错了
    p1.add(BorderLayout.CENTER,wbk);
    JButton an = new JButton("发送"); //按纽
    p1.add(BorderLayout.EAST,an);
    rq.add(BorderLayout.SOUTH,p1);
    rq.add(BorderLayout.CENTER,p2);
    应为
    p1.add(wbk, Borderlayout.CENTER);
    JButton an = new JButton("发送"); //按纽
    p1.add(an, BorderLayout.EAST);
    rq.add(p1, BorderLayout.SOUTH);rq.add(p2, BorderLayout.CENTER);
      

  4.   

    一般先把界面布置好后,最后用
    setVisible(true);
      

  5.   

    3L说了
    LZ把setVisible(true)放在构造方法的最后就可以了
    因为setVisible就会把窗体画到屏幕上,所以后面添加的组件如果不发生重绘(repaint)事件的话,就有可能显示不了,所以LZ一开始看到的组件没有显示,但是修改面板大小会发生重绘事件,所以组建会被重新画出来,所以LZ就能看到组建显示了
      

  6.   


    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import javax.swing.*;public class ChatClientFrame extends JFrame {
      
      private JTextArea textArea;
      private Action send;
      private JTextField inputField;
      public ChatClientFrame(String title) {
        
        super(title);
        
        textArea = new JTextArea();
        textArea.setEditable(false);
        textArea.setLineWrap(true);
        
        send = new AbstractAction("Send") {      @Override
          public void actionPerformed(ActionEvent e) {
            
            String message = inputField.getText();
            inputField.setText("");
            // ... send message logic
          }
        };
        
        inputField = new JTextField();
        
        layoutComponents();
        pack();
      }
      
      private void layoutComponents() {
        
        textArea.setPreferredSize(new Dimension(500, 500));
        JScrollPane center = new JScrollPane(textArea);
        center.setBorder(BorderFactory.createCompoundBorder(
                center.getBorder(),
                BorderFactory.createEmptyBorder(5, 5, 5, 5)));
        
        FlowLayout layout = new FlowLayout(FlowLayout.LEFT);
        JPanel south = new JPanel(layout);
        
        JButton button = new JButton(send);
        int width = center.getPreferredSize().width - 
                    button.getPreferredSize().width -
                    (layout.getHgap() * 3);
        
        int height = button.getPreferredSize().height;
        
        inputField.setPreferredSize(new Dimension(width, height));
        
        south.add(inputField);
        south.add(button);
        
        getContentPane().add(center, BorderLayout.CENTER);
        getContentPane().add(south, BorderLayout.SOUTH);
      }
      
      public static void main(String[] args) {
        
        SwingUtilities.invokeLater(new Runnable() {      @Override
          public void run() {
            
            JFrame f = new ChatClientFrame("Chat Client");
            f.setLocationRelativeTo(null);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setVisible(true);
          }
        });
      }
    }