【问题标题】:Change the font style, size in a Java swing appliation在 Java swing 应用程序中更改字体样式、大小
【发布时间】:2020-01-16 04:29:19
【问题描述】:

我正在用 java swing 编写一个电子邮件应用程序。我希望用户能够在写电子邮件时更改字体,但我不知道该怎么做。 我创建了一个包含所有字体的 JComboBox。

我想我应该使用getSelectedItem() 并向JComboBox 添加一个actionListener 以将此信息传递给JTextArea?还是有其他方法? 这是我的代码:

String[] fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();

        JComboBox comboBox = new JComboBox(fonts);// create a combo box with the array
        comboBox.setFont(new Font("Times New Roman", Font.PLAIN, 12));// set the font
        comboBox.setBounds(21, 6, 193, 25);// set size and location
        add(comboBox);

我应该如何让所有文本字段根据组合框中的选定项更改字体?

【问题讨论】:

  • “我想我应该使用 getSelectedItem() 并向 JComboBox 添加一个 actionListener 以将此信息传递给 JTextArea?” - 这将是一个很好的起点,期待,JTextArea 只支持纯文本。你必须使用JTextPane,但它变得更加复杂
  • 如果您希望文本的不同部分具有不同的字体,那么您需要按照@MadProgrammer 的建议使用JTextPane(或JEditorPane)。但是,如果您想更改 all 文本的字体(在 JTextArea 中),则有方法 setFont

标签: java swing jcombobox


【解决方案1】:

在这里,您可以找到您所要求的工作示例(如果我理解正确的话),嵌入在一个简单的 main 方法中。

为了简单起见,我只使用了一个组合框来选择字体名称,但最好放置两个其他组合以使字体大小和样式(BOLD、PLAIN、ITALIC)也可选择.

基本上是的,最常见的方法是将actionListener绑定到comboBox,以便在comboBox有用户交互时更改字体。当然这不是唯一的方法,您甚至可以监听按下的键盘键来触发动作,或者实现一些其他方式来识别用户更改字体的意图,但据我所知,我在例子是最简单的方法。

package fontchooser;

import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class ComboFont {

    // Put this in other two combo-boxes if you want to make these selectable by user
    public static int default_size = 16;
    public static int default_style = Font.PLAIN;

    public static void main(String[] args) {
        String[] fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();

        GridBagLayout layout = new GridBagLayout();
        layout.columnWidths = new int[] {400};
        layout.rowHeights = new int[] {100,300};

        JFrame container = new JFrame();
        container.setLayout(layout);
        container.setBounds(150,150,400,400);
        container.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JComboBox<String> comboFontNames = new JComboBox<String>(fonts);
        JTextArea textArea = new JTextArea();
        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setViewportView(textArea);

        GridBagConstraints comboContraints = new GridBagConstraints();
        comboContraints.gridx = 0;
        comboContraints.gridy = 0;
        // This only set font to display on combo
        comboFontNames.setFont(new Font("Times New Roman", Font.PLAIN, 12));
        container.add(comboFontNames, comboContraints);

        GridBagConstraints scrollerContraints = new GridBagConstraints();
        scrollerContraints.gridx = 0;
        scrollerContraints.gridy = 1;
        scrollerContraints.gridwidth = 400;
        scrollerContraints.fill = GridBagConstraints.BOTH;
        container.add(scrollPane, scrollerContraints);

        // Variant of action listener with lambda (since java 8)
        comboFontNames.addActionListener((e) -> {

            String selectedFamilyName = (String)comboFontNames.getSelectedItem();
            Font selectedFont = new Font(selectedFamilyName, default_style, default_size);

            textArea.setFont(selectedFont);
            textArea.repaint();
        });

        container.setVisible(true);
    }
}

我希望这是您所需要的!再见

阿莱西奥

【讨论】:

    猜你喜欢
    • 2011-01-19
    • 2017-07-23
    • 2012-05-11
    • 1970-01-01
    • 1970-01-01
    • 2012-12-11
    • 2020-10-20
    • 1970-01-01
    • 2011-08-31
    相关资源
    最近更新 更多