簡體   English   中英

在 JEditorPane 中更改字體

[英]Changing font in JEditorPane

Java 是我的第一語言,我主要使用它創建 CLI 應用程序,但在 2 年前為 C# 放棄了它,但選擇了它,因為它似乎最容易制作 GUI。我正在開發一個名為 MakeText 的簡單記事本盜版,主要區別:黑暗的主題。 我想制作一個工具欄按鈕來更改字體...(使用 IntelliJ idea UI builder )這是帶有工具欄按鈕的編輯頁面的代碼:

package GUI;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class EditingPage
{
    JFrame EditingFrame;
    public JPanel EditPanel;
    public JEditorPane MainEditor;
    private JPanel ButtonPanel;
    private JButton fontSizeButton;

    public EditingPage() {
        fontSizeButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                FontDialouge dialog = new FontDialouge();
                dialog.pack();
                dialog.setVisible(true);
            }
        });
    }
}

這是字體對話框的代碼:

package GUI;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class FontDialouge extends JDialog
{
    private JPanel contentPane;
    private JButton buttonOK;
    private JTextField FontSizeTextBox;

    public FontDialouge()
    {
        setContentPane(contentPane);
        setModal(true);
        getRootPane().setDefaultButton(buttonOK);

        buttonOK.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onOK();
            }
        });
    }

    private void onOK()
    {
        String inputFontSize = FontSizeTextBox.getText();

        //Converting string to int
        try
        {
            int OutputFontSize = Integer.parseInt(inputFontSize);
            System.out.println("Successfully set the font!");
            EditingPage editingPage = new EditingPage();
            editingPage.MainEditor.setFont(new Font("JetBrains Mono", Font.PLAIN, OutputFontSize));
        }
        catch (NumberFormatException nfe)
        {
            JOptionPane.showMessageDialog(null, "Not a valid size !");
            System.out.println("ERROR! not a valid size! ERROR: " + nfe);
        }
    }

}

但是由於某種原因,當我單擊“確定”按鈕時,它不會設置字體並且是相同的。 我很長一段時間沒有真正使用 java 很抱歉,如果這是一個非常基本的問題,另外,如果您想要所有 src bc,這里的數量還不夠,那么這里有一個 GitHub 頁面。

onOK()方法中,在 class FontDialouge中,您正在創建EditingPage的新實例。 這與創建FontDialouge的實例FontDialouge 您正在更改不同EditPanel的字體。

EditPanel的引用傳遞給FontDialouge

package GUI;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class FontDialouge extends JDialog
{
    private JPanel contentPane;
    private JButton buttonOK;
    private JTextField FontSizeTextBox;
    private JEditorPane editor; // ADDED THIS

    public FontDialouge(JEditorPane editor) // ADDED PARAMETER
    {
        this.editor = editor; // INITIALIZE CLASS MEMBER
        setContentPane(contentPane);
        setModal(true);
        getRootPane().setDefaultButton(buttonOK);

        buttonOK.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onOK();
            }
        });
    }

    private void onOK()
    {
        String inputFontSize = FontSizeTextBox.getText();

        //Converting string to int
        try
        {
            int OutputFontSize = Integer.parseInt(inputFontSize);
            System.out.println("Successfully set the font!");
            editor.setFont(new Font("JetBrains Mono", Font.PLAIN, OutputFontSize));
        }
        catch (NumberFormatException nfe)
        {
            JOptionPane.showMessageDialog(null, "Not a valid size !");
            System.out.println("ERROR! not a valid size! ERROR: " + nfe);
        }
    }
}

在方法actionPerformed()中,在 class EditingPage中,更改FontDialouge構造函數的調用。

public void actionPerformed(ActionEvent e) {
    FontDialouge dialog = new FontDialouge(MainEditor);
    dialog.pack();
    dialog.setVisible(true);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM