我创建了一个可在计算机和平板电脑上使用的程序。问题是当您按下JComboboxes时出现的选项在平板电脑上变得非常小。你怎么能轻松地在选项之间找到一个小差距?或者是否有其他替代解决方案来解决这个问题? 感谢您的帮助和贡献。
答案 0 :(得分:0)
您可以为组合框定义自己的列表单元格渲染器,并让该自定义渲染器在所有四个边上都提供填充。
这是一个小程序,展示了如何。虽然您无法在列表中的项目之间放置空格,但列表渲染器将允许您在列表中的数据项周围放置填充,这样可以更容易选择。如果您想要更多或更少,可以使用setBorder()来改变填充。
import java.awt.Component;
import javax.swing.BorderFactory;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.ListCellRenderer;
class CustomComboBoxRenderer extends JTextField implements ListCellRenderer
{
public CustomComboBoxRenderer()
{
this.setBorder(BorderFactory.createCompoundBorder( this.getBorder(),
BorderFactory.createEmptyBorder(15, 0, 10, 0)));
}
@Override
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
if (isSelected) {
setBackground(list.getSelectionBackground());
setForeground(list.getSelectionForeground());
} else {
setBackground(list.getBackground());
setForeground(list.getForeground());
}
this.setText(value.toString());
return this;
}
}
public class Gui extends JFrame{
JPanel panel = new JPanel();
String[] data = new String[]{"A","B","C","D","E","F"};
ComboBoxModel model = new DefaultComboBoxModel(data);
JComboBox combo = new JComboBox(model);
CustomComboBoxRenderer renderer = new CustomComboBoxRenderer();
public Gui()
{
combo.setRenderer(renderer);
JPanel content = new JPanel();
content.add(combo);
setContentPane(content);
pack();
setLocation(300,300);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public static void main(String[] args)
{
new Gui();
}
}