2012-08-28 25 views
2

我具有您可以在圖片中看到的結構。Java Swing。 ScrolPane中的TextField不完全可見

對於這兩個面板,都使用GridBagLayout。 問題是滾動窗格內的文本字段不完全可見。 父面板只爲使按鈕變得可見,但當滾動條出現時,它只是與文本字段重疊。 有沒有一個簡單的解決方案來解決這個問題(不想處理設置自定義/首選/最小高度)?

面板結構:

enter image description here

問題:

enter image description here

好的,這是一個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; 
} 

}

+1

嘗試調整GridBagConstraint的沉重的數值,用於佈置滾動窗格 – MadProgrammer

+0

嗯..是樹是真的嗎?也就是說,你的面板是一個渲染/編輯組件嗎?反正最好表現出SSCCE所以我們可以看到你實際上是在做什麼 – kleopatra

+0

沒有,這是IntelliJ IDEA的圖形用戶界面編輯器(第一屏) – Bax

回答

1

嘗試使用JTextArea而不是JTextField。如果您不想設置自定義/首選/最小尺寸,則可以使用JTextArea.setRows(int rows)方法。我希望有所幫助。