import java.awt.BorderLayout;import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextPane;public class QQChat extends JFrame {
private JTextPane jtpChat1;
private JTextPane jtpChat2;
private Box box; public QQChat() {
super("聊天记录");
box = Box.createVerticalBox();
this.add(box, BorderLayout.CENTER);
jtpChat1 = new JTextPane();
box.add(jtpChat1);
JPanel panel = new JPanel();
panel.setSize(Short.MAX_VALUE, 80);
box.add(panel);
jtpChat2 = new JTextPane();
box.add(jtpChat2);
initBase();
this.setSize(400,400);
this.setLocationRelativeTo(null);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} public void initBase() {
JPanel panel = new JPanel();
JButton jbtSend =new JButton("发送");
panel.add(jbtSend);
JButton jbtClose = new JButton("关闭");
panel.add(jbtClose);
box.add(panel);
}
public static void main(String[]args){
new QQChat();
}}为什么运行的时候 第一个JTextPane 和第二个JTextPane之间的JPanel设置的高度不起作用.
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextPane;public class QQChat extends JFrame {
private JTextPane jtpChat1;
private JTextPane jtpChat2;
private Box box; public QQChat() {
super("聊天记录");
box = Box.createVerticalBox();
this.add(box, BorderLayout.CENTER);
jtpChat1 = new JTextPane();
box.add(jtpChat1);
JPanel panel = new JPanel();
panel.setSize(Short.MAX_VALUE, 80);
box.add(panel);
jtpChat2 = new JTextPane();
box.add(jtpChat2);
initBase();
this.setSize(400,400);
this.setLocationRelativeTo(null);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} public void initBase() {
JPanel panel = new JPanel();
JButton jbtSend =new JButton("发送");
panel.add(jbtSend);
JButton jbtClose = new JButton("关闭");
panel.add(jbtClose);
box.add(panel);
}
public static void main(String[]args){
new QQChat();
}}为什么运行的时候 第一个JTextPane 和第二个JTextPane之间的JPanel设置的高度不起作用.
panel.setSize(Short.MAX_VALUE, 80);
box.add(panel);
===>>>
box.add(Box.createVerticalStrut(80));使用布局管理器时,使用preferredSize计算组件大小。setSize没有实际作用。
box.add(Box.createVerticalStrut(80));