简体   繁体   English

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

[英]Creating Own Gui Font Class as of Notepad

I am creating a Notepad in java.我正在用java创建一个记事本。 I need help as we know there is a Font chooser option in notepad.我需要帮助,因为我们知道记事本中有一个字体选择器选项。 I want to add that option in my notepad as well but FontChooser class is also not available.我也想在记事本中添加该选项,但 FontChooser 类也不可用。

Therefore i am creating my own class.因此,我正在创建自己的课程。 For that i am using listItems which contains various listItems Like PLAIN,BOLD,ITALIC and then set this value to the textField like in notepad happens.为此,我使用 listItems,其中包含各种 listItems,如 PLAIN、BOLD、ITALIC,然后将此值设置为 textField,就像在记事本中一样。

My question is there is setFont() method in java and i am using it like in this way我的问题是 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())));
    }
}

look at temp==list2看看temp==list2

I will have to check again and again tft2.eqaulsIgnoreCase() what othere i can do in the setFont(list1.getSelectedItem(),list2.getSelectedItem(),list3.getSelectedItem()) i cant do list2.getSelectedItem() because of Font.BOLD\\PLAIN\\ITALIC我将不得不一次又一次地检查tft2.eqaulsIgnoreCase()我可以在setFont(list1.getSelectedItem(),list2.getSelectedItem(),list3.getSelectedItem())做什么我不能做list2.getSelectedItem()因为字体.BOLD\\PLAIN\\ITALIC

What can i doo???我能做什么???

Any time any of the option change I think you just want to create a new Font using all of the current options.任何时候任何选项发生变化,我认为您只想使用所有当前选项创建一个新Font Then you can just use the following constructor:然后你可以只使用以下构造函数:

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

The other option is if you have a base Font, then you can apply one attribute to the Font by using:另一种选择是,如果您有基本字体,则可以使用以下方法将一个属性应用于字体:

font.deriveFont(...); 

This will allow you to change one property at a time.这将允许您一次更改一个属性。 Read the API for the proper parameters to use for the attribute you want to change.阅读 API 以了解要用于要更改的属性的正确参数。

I cannot say I fully understand the question, but the code snippet shown has aspects to it that seem tortuous.我不能说我完全理解这个问题,但显示的代码片段有一些看起来很曲折的方面。 Especially asking for the style and font size as text fields (it also precludes using both bold & italic at the same time).特别是要求将样式和字体大小作为文本字段(它还排除同时使用粗体斜体)。

I'd recommend to use check boxes for bold / italic and a spinner for font size.我建议使用粗体/斜体复选框和字体大小的微调器。 Then determining the correct font can be done as simply as follows.然后可以简单地如下确定正确的字体。

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

Here is a Minimal, Complete, and Verifiable example that shows how it might be used (please post code in this form in future).这是一个最小、完整且可验证的示例,展示了如何使用它(请在未来以这种形式发布代码)。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM