设置JTextArea的字体

3
我希望你能够帮助我在Java中创建文本编辑器。以下是我的工作进度:
```html

我想在Java中创建一个文本编辑器,以下是我目前的进展:

```
package com.thundercrust.applications;

import java.awt.BorderLayout;

public class TextEditor implements ActionListener{

private static String[] fontOptions = {"Serif", "Agency FB", "Arial", "Calibri", "Cambrian", "Century Gothic", "Comic Sans MS", "Courier New", "Forte", "Garamond", "Monospaced", "Segoe UI", "Times New Roman", "Trebuchet MS", "Serif"};
private static String[] sizeOptions = {"8", "10", "12", "14", "16", "18", "20", "22", "24", "26", "28"};

ImageIcon newIcon = new ImageIcon("res/NewIcon.png");
ImageIcon saveIcon = new ImageIcon("res/SaveIcon.png");
ImageIcon openIcon = new ImageIcon("res/OpenIcon.png");
ImageIcon fontIcon = new ImageIcon("res/FontIcon.png");
ImageIcon changeFontIcon = new ImageIcon("res/ChangeFontIcon.png");

JButton New = new JButton(newIcon);
JButton Save = new JButton(saveIcon);
JButton Open = new JButton(openIcon);
JButton changeFont = new JButton(changeFontIcon);

JLabel fontLabel = new JLabel(fontIcon);
JLabel fontLabelText = new JLabel("Font: ");
JLabel fontSizeLabel = new JLabel("Size: ");

JComboBox <String> fontName = new JComboBox<>(fontOptions);
JComboBox <String> fontSize = new JComboBox<>(sizeOptions);

JToolBar tool = new JToolBar();

JTextArea texty = new JTextArea();
JScrollPane scroll = new JScrollPane(texty);

private static final int WIDTH = 1366;
private static final int HEIGHT = WIDTH / 16 * 9;

private static String name = "Text Editor"; 

private static JFrame frame = new JFrame();

public void Display() {
    frame.setTitle(name);
    frame.setSize(WIDTH, HEIGHT);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.setResizable(false);
    frame.setVisible(true);

    New.addActionListener(this);
    New.setToolTipText("Creates a new File");
    Save.addActionListener(this);
    Save.setToolTipText("Saves the current File");
    Open.addActionListener(this);
    Open.setToolTipText("Opens a file");
    changeFont.addActionListener(this);
    changeFont.setToolTipText("Change the Font");

    fontLabel.setToolTipText("Font");

    fontLabelText.setToolTipText("Set the kind of Font");
    fontSizeLabel.setToolTipText("Set the size of the Font");

    tool.add(New);
    tool.addSeparator();
    tool.add(Save);
    tool.addSeparator();
    tool.add(Open);
    tool.addSeparator();
    tool.addSeparator();
    tool.addSeparator();
    tool.add(fontLabel);
    tool.addSeparator();
    tool.add(fontLabelText);
    tool.add(fontName);
    tool.addSeparator();
    tool.add(fontSizeLabel);
    tool.add(fontSize);
    tool.addSeparator();
    tool.add(changeFont);

    JPanel pane = new JPanel();
    pane.setLayout(new BorderLayout());
    pane.add(tool, "North");
    pane.add(scroll, "Center");
    frame.setContentPane(pane);
}

public static void main(String args[]) {
    TextEditor editor = new TextEditor();
    editor.Display();
}

public void actionPerformed(ActionEvent evt) {
    String fontNameSet;
    String fontSizeSetTemp;
    int fontSizeSet;
    Object source = evt.getSource();
    if(source == New) {
        texty.setText("");
    }
    else if(source == changeFont) {
        fontNameSet = (String) fontName.getSelectedItem();
        fontSizeSetTemp = (String) fontSize.getSelectedItem();
        fontSizeSet = Integer.parseInt(fontSizeSetTemp);
        System.out.println(fontNameSet + fontSizeSet);
        scroll.setFont(new Font(fontNameSet, fontSizeSet, Font.PLAIN));
    }

}
}

我遇到的问题是设置 JTextArea 字体。当我在实际程序中点击 changeFont 按钮时,没有任何反应。我该怎么办?


6
您正在更改JScrollPane组件中名为scroll的字体。请注意,更改不会反映在子元素上,因此您还必须为文本区域设置字体。 - BackSlash
1个回答

4
new Font(fontNameSet, fontSizeSet, Font.PLAIN)

这个代码中参数的顺序错误了。应该是:

new Font(fontNameSet, Font.PLAIN, fontSizeSet)

此外,正如评论中提到的那样:
scroll.setFont(new Font(fontNameSet, fontSizeSet, Font.PLAIN));

Should be:

texty.setFont(new Font(fontNameSet, Font.PLAIN, fontSizeSet));

1
不错的发现!@ThunderCrust:还要考虑使用deriveFont(),可以在这里看到。 - trashgod

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接