【问题标题】:Creating Own Gui Font Class as of Notepad从记事本开始创建自己的 Gui 字体类
【发布时间】:2018-08-04 11:58:24
【问题描述】:

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

因此,我正在创建自己的课程。 为此,我正在使用包含各种 listItems(如 PLAIN、BOLD、ITALIC)的 listItems,然后将此值设置为 textField,就像在记事本中发生的那样。

我的问题是 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.BOLD\PLAIN\ITALIC

我能做什么???

【问题讨论】:

  • 仔细格式化你的源代码
  • 您的问题到底是什么?我什至读过你的 cmets,但我仍然不明白你想问什么。

标签: java swing fonts


【解决方案1】:

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

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

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

font.deriveFont(...); 

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

【讨论】:

    【解决方案2】:

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

    我建议使用粗体/斜体复选框和字体大小微调器。然后确定正确的字体可以简单地如下完成。

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

    这是一个Minimal, Complete, and Verifiable example,它显示了它的使用方法(请在以后以这种形式发布代码)。

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

    【讨论】:

    • 我只是想问一下 textField 中写的任何内容都应该用作字体样式,如 Font.tft.getText()
    • 该评论并没有加深我对问题的理解。你有运行我发布的代码吗?您要添加自己的 MCVE 吗?
    猜你喜欢
    • 2014-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-07
    • 1970-01-01
    • 2021-07-28
    • 2021-10-11
    • 1970-01-01
    相关资源
    最近更新 更多