在JFrame中使用JSplitPane进行分栏后需要手动拖动一下窗体大小才能显示内容

初始化执行后如图1图片说明

拖动之后如图2图片说明

为什么不能初始化显示成图2的样子
代码如下:
package com.yeeda.pollyover;

import java.awt.Dimension;
import java.awt.Toolkit;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSplitPane;

public class PollyoverFrame extends JFrame{

static final int WIDTH=1000;
static final int HEIGHT=800;
JPanel jpanelMain = new JPanel();

PollyoverFrame(){
    super("测试");
    setSize(WIDTH,HEIGHT);

    //激活窗体的关闭按钮
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //设置窗体的初始化位置(屏幕中央)
    Toolkit kit=Toolkit.getDefaultToolkit();
    Dimension screenSize=kit.getScreenSize();
    int width=screenSize.width;
    int height=screenSize.height;
    int x=(width-WIDTH)/2;
    int y=(height-HEIGHT)/2;
    setLocation(x,y);
    setVisible(true);  
    //setResizable(false);

    //设置窗体分割
    final JSplitPane hSplitPane = new JSplitPane();
    JPanel leftPanel = new LeftPanel();
    JPanel mainPanel = new MainPanel();
    hSplitPane.setLeftComponent(leftPanel);
    hSplitPane.setRightComponent(mainPanel);
    hSplitPane.setDividerLocation(200);
    hSplitPane.setOneTouchExpandable(false);
    getContentPane().add(hSplitPane);
}

public static void main(String[] args) {
    PollyoverFrame pollyoverFrame = new PollyoverFrame();
}

}

package com.yeeda.pollyover;

import java.awt.Image;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class LeftPanel extends JPanel {
LeftPanel() {

    //获取图片并设置图片的大小
    ImageIcon image = new ImageIcon(
            LeftPanel.class.getResource("butterfly.jpg"));
    image.setImage(image.getImage().getScaledInstance(40, 40,
            Image.SCALE_DEFAULT));
    //将图片添加到JLabel
    JLabel picLabel = new JLabel(image);
    JLabel picLabel2 = new JLabel("测试");
    add(picLabel);
    add(picLabel2);

}

}

package com.yeeda.pollyover;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class MainPanel extends JPanel {
MainPanel(){
JButton ok=new JButton("搭配");
JButton cancel=new JButton("混搭");
add(ok);
add(cancel);
}
}

查看全部
sky_xin
sky_xin
2015/08/24 11:49
  • swing
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

1个回复