简单的文本编辑器,能够更改字体

时间:2018-01-28 15:39:15

标签: java swing

我目前正在处理简单的文本编辑器。但我卡住了。 我把我的程序分成了三个不同的类:

  • 记事本 - 负责创建gui的主要类。
  • ButtonPanel - 创建按钮,动作侦听器,jcomboboxes等。
  • WriteArea - 创建我们可以写的区域。

到目前为止,在ButtonPanel类中创建JComboBox,您可以在其中选择字体类型并监听事件,并在事件发生时,从JComboBox中分配'currentFont'值。但我无法将更新的'currenFont'发送到WriteArea,我的问题是如何刷新此类以获取currentFont变量。

顺便说一句。 如果你看到另一个错误或只是糟糕的编码,不要害羞并写下:D

If var:duedate starts with Wed

or var:duedate starts with Thurs

or var:duedate starts with Fri

 add 5 days to currentitem:duedate (output to var:newdate)
else if var:dueDate starts with Sat

 add 4 days to currentitem:duedate (output to var:newdate)
else

 add 3 days to currentitem:duedate (output to var:newdate)
set currentItem:duedate to var:newdate

和ButtonPanel.java

package SimpleNotepad;

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

public class Notepad extends JFrame {

    public static void main(String[] args){
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Notepad();
            }
        });
    }

    public Notepad(){
        ButtonPanel buttonPanel = new ButtonPanel();
        WriteArea writeArea = new WriteArea(buttonPanel);

        //setSize(800, 600);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        add(buttonPanel, BorderLayout.NORTH);
        add(writeArea);
        setJMenuBar(buttonPanel.menuBar);
        pack();
        setLocationRelativeTo(null);
        setVisible(true);

    }
}

WriteArea.java

package SimpleNotepad;

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

public class ButtonPanel extends JPanel {

    JMenuBar menuBar;
    private String currentFont = "Arial";
    private String currentStyle;
    private int currentSize;

    private String[] fontTypes = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
    private String[] fontStyles = {"Plain", "Bold", "Italic"};
    private Integer[] fontSizes = {9, 10, 11, 12, 13, 14};

    public ButtonPanel(){
        menuBar = new JMenuBar();
        JMenu menu = new JMenu("File");
        menuBar.add(menu);
        JMenuItem menuOpen = new JMenuItem("Open");
        JMenuItem menuZapisz = new JMenuItem("Save");
        menu.add(menuOpen);
        menu.add(menuZapisz);

        Box theBox = Box.createHorizontalBox();

        JComboBox fontList = new JComboBox(fontTypes);
        JComboBox fontStyle = new JComboBox(fontStyles);
        JComboBox fontSize = new JComboBox(fontSizes);

        fontList.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JComboBox comboBox = (JComboBox) e.getSource();
                currentFont = (String)comboBox.getSelectedItem();
                System.out.println(currentFont);
            }
        });

        JButton fontColor = new JButton("Font Color");
        JButton backgroundColor = new JButton("Background Color");

        theBox.add(fontList);
        theBox.add(fontStyle);
        theBox.add(fontSize);
        theBox.add(fontColor);
        theBox.add(backgroundColor);
        add(theBox);

    }

    public String getCurrentFont() {
        System.out.println(currentFont);
        return currentFont;
    }

}

1 个答案:

答案 0 :(得分:0)

我不知道此解决方案是否非常优雅,但它适用于我:让WriteArea类扩展KeyListener并将textArea添加到此KeyListener。然后,您可以在keyPressed方法中设置字体。每次按下一个键并且WriteArea被聚焦时,这将检查所选字体(如果要在其中键入内容,则需要聚焦)。这里有变化的部分:

public class WriteArea extends JPanel implements KeyListener{
//...
     textArea.addKeyListener(this);
//...
}

@Override
public void keyPressed(KeyEvent arg0) {
    if (this.getFont().getFontName() != buttonPanel.getCurrentFont()) {
    textArea.setFont(new Font(buttonPanel.getCurrentFont(), Font.ITALIC, 12));
    }
}

注意:当然只适用于字体系列,但您应该能够扩展它以适应大小和样式。你的代码对我来说似乎很干净,我也不知道你可以按照你的方式投射ActionEvent