Java JTextField通过JComboBox和x27显示;s在滚动后下拉
当Java JTextField通过JComboBox和x27显示;s在滚动后下拉,java,swing,jcombobox,Java,Swing,Jcombobox,当JTextField位于JScrollPanel中时,如果面板已滚动,则每当JComboBox的下拉列表位于JTextField上方时,文本字段将通过下拉列表显示 这仅在内容被滚动后发生(而不是在应用程序启动时) 主要问题是我们如何解决这个问题? 如果答案为: 他不是黑客 首先解释为什么会发生这种情况 我尝试过的事情: import java.awt.*; import java.awt.event.*; import javax.swing.*; public class TextFi
JTextField
位于JScrollPanel
中时,如果面板已滚动,则每当JComboBox
的下拉列表位于JTextField
上方时,文本字段将通过下拉列表显示
这仅在内容被滚动后发生(而不是在应用程序启动时)
主要问题是我们如何解决这个问题?
如果答案为:
- 他不是黑客
- 首先解释为什么会发生这种情况
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;
}
}
- 将下拉列表移出滚动窗格(无更改)
- 在scroll上找到的所有容器中添加重绘(无更改)
- 滚动窗格内容的不同布局管理器(无更改)
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;
}
}
这是因为您在一个轻量级容器中使用了一个
java.awt.TextField
,它是一个重量级组件。JComboBox
使用的弹出窗口也可以是一个轻量级组件
AWT组件不能很好地与Swing组件配合使用,它们存在z顺序问题
更改TextField=newtextfield()
toJTextField=newjtextfield()代码>
您还应该避免使用setPreferred/Minimum/MaximumSize
(有关更多详细信息,请参阅),而是使用布局约束和大小调整提示(如JTextField
的列
属性),这是因为您使用的是java.awt.TextField
,这是一个很重的组件,在一个重量轻的容器内。JComboBox
使用的弹出窗口也可以是一个轻量级组件
AWT组件不能很好地与Swing组件配合使用,它们存在z顺序问题
更改TextField=newtextfield()
toJTextField=newjtextfield()代码>
您还应该避免使用setPreferred/Minimum/MaximumSize
(有关更多详细信息,请参阅),而是使用布局约束和大小调整提示(如JTextField
的columns
属性)有趣的是,在查看其他人的代码时,您可能会错过这个小“J”。简单的解决方法,很好的回答。将尽快接受。我想你是指代码吗?:是的-问题中有问题(刷新内容?)我无法重现,所以我无法“修复”这个问题,但它确实激发了好奇心。有趣的是,当你看别人的代码时,你会错过那个小“J”。简单的解决方法,很好的回答。将尽快接受。我想你是指代码?:是的-问题中有问题(刷新内容?)我无法重现,因此我无法“修复”问题,但它确实激发了好奇心。