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();
}
}
不知道为什么程序运行后只能改变面板大小才能显示内容....求解....
还有本人自学新手....程序有哪些写的不好的地方求指教...谢谢了!!!!
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();
}
}
不知道为什么程序运行后只能改变面板大小才能显示内容....求解....
还有本人自学新手....程序有哪些写的不好的地方求指教...谢谢了!!!!
你在一些组件还为初始化的时候就调用setVisible()方法会对后期的一些组件属性的设置造成一些影响
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);
setVisible(true);
LZ把setVisible(true)放在构造方法的最后就可以了
因为setVisible就会把窗体画到屏幕上,所以后面添加的组件如果不发生重绘(repaint)事件的话,就有可能显示不了,所以LZ一开始看到的组件没有显示,但是修改面板大小会发生重绘事件,所以组建会被重新画出来,所以LZ就能看到组建显示了
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);
}
});
}
}