我会在Java中制作一个小的rft文本编辑器,我会在我的组合框中以该字体显示不同类型的字体。原因是因为用户可以看到该字体。
我知道你可以使用C#的组合框项目。但我对Java并不了解。
任何人都可以帮助我吗?
修改
答案 0 :(得分:3)
听起来您需要为组合框实现自定义渲染器。 请参阅此处的java教程:http://docs.oracle.com/javase/tutorial/uiswing/components/combobox.html#renderer
即。这样的事情(假设你的组合框中的对象是字体):
class CustomRenderer implements ListCellRenderer
{
@Override
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
JLabel label = new JLabel();
label.setFont(((Font) value).deriveFont(12f));
label.setText(((Font) value).getFontName());
return label;
}
}
答案 1 :(得分:1)
java中字体选择的自定义组合框
这是代码:
package stack;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
public class CustomComboBox {
JComboBox fontComboBox;
JFrame frame;
String fontName[];
Integer array[];
public CustomComboBox() {
JFrame.setDefaultLookAndFeelDecorated(true);
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
fontName = ge.getAvailableFontFamilyNames();
array = new Integer[fontName.length];
for(int i=1;i<=fontName.length;i++) {
array[i-1] = i;
}
fontComboBox = new JComboBox(array);
ComboBoxRenderar renderar = new ComboBoxRenderar();
fontComboBox.setRenderer(renderar);
frame.getContentPane().setLayout(new FlowLayout());
frame.getContentPane().add(fontComboBox);
frame.pack();
frame.setVisible(true);
}
public class ComboBoxRenderar extends JLabel implements ListCellRenderer {
@Override
public Component getListCellRendererComponent(JList list,
Object value,
int index,
boolean isSelected,
boolean cellHasFocus) {
int offset = ((Integer)value).intValue() - 1 ;
String name = fontName[offset];
setText(name);
setFont(new Font(name,Font.PLAIN,20));
return this;
}
}
public static void main(String args[]) {
new CustomComboBox();
}
}