2

写真のような構造になっています。

両方のパネルで、GridBagLayout が使用されます。問題は、スクロールペイン内のテキスト フィールドが完全に表示されないことです。親パネルは、ボタンが表示されるようにだけ引き伸ばされますが、スクロール バーが表示されると、テキスト フィールドと重なるだけです。これを修正する簡単な解決策はありますか (カスタム/優先/最小の高さの設定に対処したくない)?

パネル構造 :

ここに画像の説明を入力

問題 :

ここに画像の説明を入力

わかりました、これがSSCCEです

public class Main {
JFrame frame;
private JPanel mainPanel;
private JButton button1;
private JButton button2;
private JTextField someTextTextField;


public static void main(String[] args) {
    Main main = new Main();
    main.show();
}

private void show() {
    frame = new JFrame("Frame");
    frame.setContentPane(mainPanel);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();

    frame.setVisible(true);
}


{

    $$$setupUI$$$();
}

/**
 * Method generated by IntelliJ IDEA GUI Designer
 * >>> IMPORTANT!! <<<
 * DO NOT edit this method OR call it in your code!
 *
 * @noinspection ALL
 */
private void $$$setupUI$$$() {
    mainPanel = new JPanel();
    mainPanel.setLayout(new GridBagLayout());
    mainPanel.setPreferredSize(new Dimension(209, 30));
    final JPanel panel1 = new JPanel();
    panel1.setLayout(new GridBagLayout());
    GridBagConstraints gbc;
    gbc = new GridBagConstraints();
    gbc.gridx = 1;
    gbc.gridy = 0;
    gbc.weightx = 0.5;
    gbc.weighty = 1.0;
    gbc.fill = GridBagConstraints.BOTH;
    mainPanel.add(panel1, gbc);
    button1 = new JButton();
    button1.setText("Button");
    gbc = new GridBagConstraints();
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    panel1.add(button1, gbc);
    button2 = new JButton();
    button2.setText("Button");
    gbc = new GridBagConstraints();
    gbc.gridx = 1;
    gbc.gridy = 0;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    panel1.add(button2, gbc);
    final JScrollPane scrollPane1 = new JScrollPane();
    scrollPane1.setAlignmentX(0.0f);
    gbc = new GridBagConstraints();
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.weightx = 0.5;
    gbc.weighty = 1.0;
    gbc.fill = GridBagConstraints.BOTH;
    mainPanel.add(scrollPane1, gbc);
    someTextTextField = new JTextField();
    someTextTextField.setText("some text");
    scrollPane1.setViewportView(someTextTextField);
}

/**
 * @noinspection ALL
 */
public JComponent $$$getRootComponent$$$() {
    return mainPanel;
}

}

4

1 に答える 1

1

JTextAreaの代わりに a を試してみてくださいJTextField。また、カスタム/優先/最小サイズを設定したくない場合は、JTextArea.setRows(int rows)メソッドを使用できます。それが役立つことを願っています。

于 2012-09-22T18:15:06.407 に答える