具有textarea的Java GridLayout滚动窗格

时间:2019-07-01 13:15:11

标签: java swing textarea scrollpane

我想在框架的一部分中使用抽象布局,并在其上标记按钮。如果要在本节底部使用gridbaglayout,则需要具有文本区域和滚动窗格。但是我无法合并textarea和滚动条

公共类test1扩展了JFrame {

private JPanel contentPane;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                test1 frame = new test1();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public test1() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 486, 451);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JButton btnNewButton = new JButton("New button");
    btnNewButton.setBounds(21, 23, 89, 23);
    contentPane.add(btnNewButton);

    Panel panel = new Panel();
    panel.setBounds(21, 83, 426, 319);
    contentPane.add(panel);
    GridBagLayout gbl_panel = new GridBagLayout();
    gbl_panel.columnWidths = new int[]{0, 0};
    gbl_panel.rowHeights = new int[]{0, 0};
    gbl_panel.columnWeights = new double[]{1.0, Double.MIN_VALUE};
    gbl_panel.rowWeights = new double[]{1.0, Double.MIN_VALUE};
    panel.setLayout(gbl_panel);

    JTextArea textArea = new JTextArea();
    textArea.setLineWrap(true);
    textArea.setWrapStyleWord(true);
    GridBagConstraints gbc_textArea = new GridBagConstraints();
    gbc_textArea.fill = GridBagConstraints.BOTH;
    gbc_textArea.gridx = 0;
    gbc_textArea.gridy = 0;
    panel.add(textArea, gbc_textArea);

    JTextArea comments = new JTextArea(10, 60); 
    JScrollPane sp3  = new JScrollPane(comments, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    JViewport vp  = sp3.getViewport();
    JTextArea comments1 = (JTextArea)vp.getView();
    sp3.setViewportView(new JTextPane());

0 个答案:

没有答案