Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从记事本开始创建自己的Gui字体类_Java_Swing_Fonts - Fatal编程技术网

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

Java 从记事本开始创建自己的Gui字体类,java,swing,fonts,Java,Swing,Fonts,我正在用java创建一个记事本。我需要帮助,因为我们知道记事本中有一个字体选择器选项。我也想在记事本中添加该选项,但FontChooser类也不可用 因此,我正在创建自己的类。 为此,我使用listItems,它包含各种listItems,如普通、粗体、斜体,然后将此值设置为文本字段,如记事本中的 我的问题是java中有setFont方法,我就是这样使用它的 public void itemStateChanged(ItemEvent e) { List temp=(List)e.get

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

因此,我正在创建自己的类。 为此,我使用listItems,它包含各种listItems,如普通、粗体、斜体,然后将此值设置为文本字段,如记事本中的

我的问题是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 在setFontlist1.getSelectedItem,list2.getSelectedItem,list3.getSelectedItem中我还能做什么?我不能做list2.getSelectedItem,因为Font.BOLD\PLAIN\ITALIC


我能做什么?

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

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);
    }
}

仔细格式化你的源代码你的问题是什么?我甚至读了你的评论,但我还是不明白你想问什么。我只是想问,文本字段中写的任何内容都应该用作fontstyle,比如Font.tft.getText。该评论并不能提高我对问题的理解。你运行我发布的代码了吗?您是否要添加自己的MCVE?