JTextpane 无法正常显示, 把整个JFrame都占满了


我本来希望JFrame 按这样显示 图片描述
可是当我在绿色的panel中加入JTexpane并附上文字之后就变成了这样
图片描述
下面是我打代码


 public static void main(String[] args){
        JFrame frame = new JFrame();
        frame.setLayout(new GridBagLayout());
        JPanel groupListPanel = new JPanel();


        // init component
        JPanel personListPanel = new JPanel();
        JPanel showPanel = new JPanel(new BorderLayout());
        groupListPanel.setBackground(Color.red);
        personListPanel.setBackground(Color.yellow);
        showPanel.setBackground(Color.green);
        // add a textpane
        JTextPane pane = new JTextPane();
        // setText
        pane.setText("亩产万斤亩产万斤亩产万斤亩产万斤亩产万斤亩产万斤\n亩产万斤亩产万斤亩产万\n斤亩产万斤亩\n产万斤亩产万斤亩产万斤亩产万斤亩产万斤亩产万斤亩产万斤亩\n产万斤亩产万斤\n亩产万斤斤亩产万斤亩产万斤亩产万斤亩产万斤亩产万斤亩产斤亩产万斤亩产万斤亩产万斤亩产万斤亩产万斤亩产斤亩产万斤亩产万斤亩产万斤亩产万斤亩产万斤亩产斤亩产万斤亩产万斤亩产万斤亩产\n万斤亩产万斤亩产");
        //pane.setText("afjiwof\nwjfie\nw\n");
        showPanel.add(pane, BorderLayout.CENTER);
        JPanel ListPanel = new JPanel(new FlowLayout()); 
        ListPanel.add(groupListPanel);
        ListPanel.add(personListPanel);

        frame.add(groupListPanel, new GridBagConstraints(0,1,1, 1,0.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.VERTICAL, new Insets(0, 0, 0, 0),0,0));
        frame.add(personListPanel, new GridBagConstraints(1,1,1,1,0.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.VERTICAL, new Insets(0, 0, 0, 0),0,0));
        //frame.add(new JPanel(), new GridBagConstraints(2, 1, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
        frame.add(showPanel, new GridBagConstraints(2, 1, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0,0,0,0), 0, 0));

        groupListPanel.setPreferredSize(new Dimension(144, 507));
        personListPanel.setPreferredSize(new Dimension(144, 507));
        frame.setPreferredSize(new Dimension(674, 582));
        frame.setSize(new Dimension(674, 582));
        frame.setVisible(true);
    }

求各位帮忙!!

java swing java-ee

jinzo007 11 years, 11 months ago

添加文字那行后面加上这几句:


 groupListPanel.setMinimumSize(new Dimension(150, 480));
    personListPanel.setMinimumSize(new Dimension(150, 480));
    showPanel.setMinimumSize(new Dimension(300, 480));

夏沫丶水月 answered 11 years, 11 months ago

Your Answer