从记事本开始创建自己的 Gui 字体类



我正在用java创建一个记事本。我需要帮助,因为我们知道记事本中有一个字体选择器选项。我也想在我的记事本中添加该选项,但字体选择器类也不可用。

因此,我正在创建自己的类。 为此,我正在使用包含各种列表项的列表项,例如纯体,粗体,斜体,然后将此值设置为文本字段,就像记事本中发生的那样。

我的问题是 java 中有 setFont() 方法,我以这种方式使用它

public void itemStateChanged(ItemEvent e)
{
List temp=(List)e.getSource();
if(temp==list1)
{
tft.setText(list1.getSelectedItem());
tft6.setFont(new     Font(list1.getSelectedItem(),,Integer.parseInt(tft4.getText())));
}
else if(temp==list2)
{
tft2.setText(list2.getSelectedItem());
if(tft2.getText().equalsIgnoreCase("BOLD"))
{
tft6.setFont(new     Font(list1.getSelectedItem(),Font.BOLD,Integer.parseInt(tft4.getText())));
}
else if(tft2.getText().equalsIgnoreCase("ITALIC"))
{
tft6.setFont(new     Font(list1.getSelectedItem(),Font.ITALIC,Integer.parseInt(tft4.getText())));           }
else if(tft2.getText().equalsIgnoreCase("PLAIN"))
{
tft6.setFont(new Font(list1.getSelectedItem(),Font.PLAIN,Integer.parseInt(tft4.getText())));    
}
}
else if(temp==list3)
{
tft4.setText(list3.getSelectedItem());
tft6.setFont(new Font(list1.getSelectedItem(),Font.BOLD,Integer.parseInt(tft4.getText())));
}
}

看看temp==list2

我将不得不一次又一次地检查tft2.eqaulsIgnoreCase()setFont(list1.getSelectedItem(),list2.getSelectedItem(),list3.getSelectedItem())中我可以做什么,因为字体而我不能做list2.getSelectedItem().粗体\纯体\斜体

我能做什么???

每当任何选项更改时,我认为您只想使用当前所有选项创建一个新Font。然后,您可以只使用以下构造函数:

Font(String name, int style, int size);

另一种选择是,如果您有基本字体,则可以使用以下方法将一个属性应用于字体:

font.deriveFont(...); 

这将允许您一次更改一个属性。阅读 API,了解要用于要更改的属性的正确参数。

我不能说我完全理解这个问题,但显示的代码片段有一些看起来曲折的方面。特别是要求样式和字体大小作为文本字段(它也排除了同时使用体和斜体)。

我建议使用粗体/斜体复选框和字体大小的微调器。然后可以按如下方式确定正确的字体。

private Font getFont() {
String name = fontFamilyBox.getSelectedItem().toString();
int style = Font.PLAIN;
if (boldCheckBox.isSelected()) {
style += Font.BOLD;
}
if (italicCheckBox.isSelected()) {
style += Font.ITALIC;
}
int size = fontSizeModel.getNumber().intValue();
return new Font(name, style, size);
}

下面是一个最小、完整且可验证的示例,显示了如何使用它(请以后以这种形式发布代码)。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.EmptyBorder;
public class FontChooserPad {
private JComponent ui = null;
JComboBox<String> fontFamilyBox;
JCheckBox boldCheckBox = new JCheckBox("Bold");
JCheckBox italicCheckBox = new JCheckBox("Italic");
SpinnerNumberModel fontSizeModel = new SpinnerNumberModel(20, 6, 120, 1);
JTextArea editArea = new JTextArea(
"The quick brown fox jumps over the lazy dog.", 4, 40);
FontChooserPad() {
initUI();
}
public final void initUI() {
if (ui!=null) return;
ui = new JPanel(new BorderLayout(4,4));
ui.setBorder(new EmptyBorder(4,4,4,4));
JPanel controls = new JPanel();
ui.add(controls, BorderLayout.PAGE_START);
String[] fontFamilies = GraphicsEnvironment.
getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
fontFamilyBox = new JComboBox<>(fontFamilies);
controls.add(new JLabel("Font"));
controls.add(fontFamilyBox);
controls.add(boldCheckBox);
controls.add(italicCheckBox);
JSpinner sizeSpinner = new JSpinner(fontSizeModel);
controls.add(sizeSpinner);
editArea.setWrapStyleWord(true);
editArea.setLineWrap(true);
ui.add(new JScrollPane(editArea));
ActionListener fontActionListener = (ActionEvent e) -> {
changeFont();
};
boldCheckBox.addActionListener(fontActionListener);
italicCheckBox.addActionListener(fontActionListener);
fontFamilyBox.addActionListener(fontActionListener);
fontFamilyBox.setSelectedItem(Font.SERIF);
ChangeListener fontChangeListener = (ChangeEvent e) -> {
changeFont();
};
sizeSpinner.addChangeListener(fontChangeListener);
changeFont();
}
private void changeFont() {
editArea.setFont(getFont());
}
private Font getFont() {
String name = fontFamilyBox.getSelectedItem().toString();
int style = Font.PLAIN;
if (boldCheckBox.isSelected()) {
style += Font.BOLD;
}
if (italicCheckBox.isSelected()) {
style += Font.ITALIC;
}
int size = fontSizeModel.getNumber().intValue();
return new Font(name, style, size);
}
public JComponent getUI() {
return ui;
}
public static void main(String[] args) {
Runnable r = () -> {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception useDefault) {
}
FontChooserPad o = new FontChooserPad();
JFrame f = new JFrame(o.getClass().getSimpleName());
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationByPlatform(true);
f.setContentPane(o.getUI());
f.pack();
f.setMinimumSize(f.getSize());
f.setVisible(true);
};
SwingUtilities.invokeLater(r);
}
}

最新更新