JTextPane位于JFrame中的JPanel中的JScrollPane中

Chr*_*ris 3 java swing jpanel jscrollpane layout-manager

我想要做的就是让JTextPane在JPanel中占用尽可能多的空间.对于我正在使用的UpdateInfoPanel:

public class UpdateInfoPanel extends JPanel {
private static final long serialVersionUID = 257125249175323679L;

private JTextPane textPane;

public UpdateInfoPanel(){
    textPane = new JTextPane();
    textPane.setBackground(Color.DARK_GRAY);
    textPane.setEditable(false);
    textPane.setMargin(null);
    textPane.setContentType("text/html");
    textPane.setText("<html><body style=\"font: Arial; color: white; padding: 5px; padding-top: 0;\"><h1 style=\"padding-top: 0;\">News</h1></body></html>");
    JScrollPane scrollPane = new JScrollPane(textPane);
    add(scrollPane);
    setBackground(Color.DARK_GRAY);
    textPane.setBorder(BorderFactory.createLineBorder(Color.BLACK));
}
}
Run Code Online (Sandbox Code Playgroud)

对于我的框架,我只需创建一个这样的实例并将其放在BorderLayout.CENTER中.目前这是它的外观.白色边框是滚动窗格,黑色是文本窗格.我想要做的是让边框位于面板的边缘. ![] [1]

bas*_*mes 5

你在设置面板的布局吗?

    setLayout(new BorderLayout());
    add(scrollPane,BorderLayout.CENTER);
Run Code Online (Sandbox Code Playgroud)

如果你不需要textpane的边框,请textPane.setBorder(null);
不要扩展JPanel.您可以在创建时创建JTextPane.