如何在 Java 中右對齊 ComboBox 中的文字
要右對齊 JComboBox 中的文字,請使用以下程式碼
ComponentOrientation.RIGHT_TO_LEFT
以下是對齊 ComboBox 中文字的示例
示例
import java.awt.Component;
import java.awt.ComponentOrientation;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JList;
public class SwingDemo extends JFrame {
public SwingDemo() {
JComboBox<String> combo = new JComboBox<String>();
combo.setRenderer(new MyListCellRenderer());
combo.addItem("One");
combo.addItem("Two");
combo.addItem("Three");
combo.addItem("Four");
combo.addItem("Five");
getContentPane().add(combo, "North");
setSize(600, 400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new SwingDemo().setVisible(true);
}
}
class MyListCellRenderer extends DefaultListCellRenderer {
@Override
public Component getListCellRendererComponent(
JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
c.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
return c;
}
}輸出

廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP