當JTextField
處於JScrollPanel
,如果面板已滾動,每當從JComboBox
下拉是在JTextField
之後,文本字段示出了通過下拉菜單。 的JTextField通過JcomboBox的下拉表示滾動
This only發生在內容滾動後(不在應用程序啓動時)。
主要問題是我們該如何解決這個問題? 獎勵積分,如果答案:
- 是不是黑客攻擊
- 解釋爲什麼它擺在首位
事情我已經試過發生:
- 移動滾動窗格外的下拉列表(無變化)
- 將重繪添加到任何ND每個集裝箱我能找到的滾動(無變化)
- 不同的佈局管理器爲滾動窗格的內容(無變化)
代碼示例:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TextFieldShowsThrough{
public static void main(String[] args){
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createScrollDemo());
frame.pack();
// For demonstration purposes
frame.setSize(frame.getWidth() + 100, frame.getHeight() - 100);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static JScrollPane createScrollDemo(){
final Box optionsPanel = Box.createVerticalBox();
optionsPanel.add(createDropDown());
optionsPanel.add(createTextField("Option1"));
optionsPanel.add(createTextField("Option2"));
optionsPanel.add(createTextField("Option3"));
optionsPanel.add(createTextField("Option4"));
optionsPanel.add(createTextField("Option5"));
optionsPanel.add(Box.createVerticalGlue());
JScrollPane result = new JScrollPane(optionsPanel);
// Made attempts to fix here, but to no avail
/*result.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener() {
@Override
public void adjustmentValueChanged(AdjustmentEvent e) {
result.repaint();
}
});*/
return result;
}
public static Box createDropDown(){
Box b = Box.createVerticalBox();
b.setAlignmentX(JLabel.LEFT_ALIGNMENT);
b.add(new JLabel("Language"));
JComboBox combo = new JComboBox(new String[]{"en", "fr", "es"});
combo.setMaximumSize(new Dimension(500, 25));
b.add(combo);
return b;
}
public static Box createTextField(String label){
Box mainBox = Box.createVerticalBox();
mainBox.setOpaque(true);
mainBox.setBackground(new Color((int)(Math.random() * 0x1000000))); // because fun
JLabel jLabel = new JLabel(label);
jLabel.setAlignmentX(JLabel.LEFT_ALIGNMENT);
mainBox.add(jLabel);
Box secondaryBox = Box.createHorizontalBox();
secondaryBox.setAlignmentX(JLabel.LEFT_ALIGNMENT);
TextField tf = new TextField();
tf.setMaximumSize(new Dimension(500, 25));
secondaryBox.add(tf);
mainBox.add(secondaryBox);
return mainBox;
}
}
滑稽你如何能錯過當看別人的代碼時,這個小「J」。易於修復,很好的答案。將盡快接受。 –
我假設你的意思是[this](http://stackoverflow.com/questions/33899843/the-jscrollpane-content-doesnt-refesh-while-scrolling/33904482#33904482)代碼? – MadProgrammer
:是的 - 問題中有問題(刷新的東西?)我無法複製,所以我不能「修復」這個問題,但它確實引發了好奇心。 –