我想有一個JComboBox與右對齊。我怎樣才能做到這一點? 之前有人說:「你可以設置一個渲染器到JComboBox,它可以是JLabel#setHorizontalAlignment(JLabel.RIGHT)」,但我不知道我該怎麼做?有什麼辦法來正確對齊JCombobox中的文本
7
A
回答
13
之前有人說:「你可以將渲染器設置爲JComboBox中它可以是一個JLabel JLabel的有#setHorizontalAlignment(JLabel.RIGHT) 「
是的,默認的renederer是JLabel,因此您不需要創建自定義渲染器。你可以使用:
((JLabel)comboBox.getRenderer()).setHorizontalAlignment(JLabel.RIGHT);
6
好了,你可以用ListCellRenderer做的,是這樣的:
import java.awt.Component;
import java.awt.ComponentOrientation;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.SwingUtilities;
public class ComboboxDemo extends JFrame{
public ComboboxDemo(){
JComboBox<String> comboBox = new JComboBox<String>();
comboBox.setRenderer(new MyListCellRenderer());
comboBox.addItem("Hi");
comboBox.addItem("Hello");
comboBox.addItem("How are you?");
getContentPane().add(comboBox, "North");
setSize(400, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private static class MyListCellRenderer extends DefaultListCellRenderer {
@Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
Component component = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
component.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
return component;
}
}
public static void main(String [] args){
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new ComboboxDemo().setVisible(true);
}
});
}
}
+0
謝謝梅拉曼 –
+0
我不能選擇方向*中心*!只從左到右,從右到左 – shareef
0
這個工作對我好,短
comboFromDuration.setRenderer(new DefaultListCellRenderer() {
@Override
public void paint(Graphics g) {
setHorizontalAlignment(DefaultListCellRenderer.CENTER);
setBackground(Color.WHITE);
setForeground(Color.GRAY);
setEnabled(false);
super.paint(g);
}
});
,避免每次油漆的制定者(圖形)調用,你也可以使用匿名的構造塊:
comboFromDuration.setRenderer(new DefaultListCellRenderer() {
{
setHorizontalAlignment(DefaultListCellRenderer.CENTER);
setBackground(Color.WHITE);
setForeground(Color.GRAY);
setEnabled(false);
}
});
相關問題
- 1. 爲什麼我的文本在wxPython中沒有正確對齊?
- 2. 有什麼辦法可以將寫入文本文件的文本對齊嗎?
- 3. 爲什麼是文本對齊:正確;不正常工作?
- 4. 將UITextView中的文本對齊到左上角的正確方法是什麼?
- 5. HTML文本沒有正確對齊
- 6. 有沒有什麼辦法使用ColumnText垂直對齊iText(iTextSharp)文本?
- 7. 有沒有辦法確保所有文本對齊:中心文本確實居中?
- 8. text-align:center;給我和文本對齊:正確;爲什麼?
- 9. 正確對齊文本中圈
- 10. 如何正確對齊文本與文本對齊「證明」
- 11. 無法正確對齊PIL文本的文本
- 12. 在EditText中輸入的文本沒有正確對齊
- 13. 爲什麼這個按鈕在html中沒有正確對齊?
- 14. 有什麼辦法來產生PHP PSS補齊簽名?
- 15. 文本在打印介質中沒有正確對齊中心
- 16. 如何正確對齊訪問文本字段中的文本?
- 17. 如何正確對齊此文本?
- 18. 如何正確對齊頁腳文本
- 19. 如何正確對齊文本框?
- 20. HTML文本不對齊正確
- 21. 如何正確對齊文本?
- 22. 不能正確對齊文本 - 引導
- 23. 什麼是使用Bootstrap Grid對齊內容的正確方法?
- 24. 有什麼辦法來動態加載本地JS文件?
- 25. HTML li無法正確對齊文字
- 26. 爲什麼我的文字不正確對齊?
- 27. 爲什麼我的文字不能正確對齊?
- 28. 將數據提供給JComboBox的正確方法是什麼?
- 29. 填充JComboBox的正確方法是什麼?
- 30. 有什麼辦法讓這些跨度元素垂直對齊?
+1,你的回答比我的好,謝謝你的提供。 – UDPLover
Idk ...我認爲更好的答案仍然是你的兩個答案的組合。特別是,您的答案對我更有用,因爲我使用自己的自定義渲染器。另一方面,我可以看到如果你只是在模型中使用普通的字符串數組(或者對於組合框中的對象使用toString()就足夠好),那麼這樣做會更有用。 – searchengine27