使用JButton增加/减少textArea中的字体大小

Sob*_*lic 3 java fonts swing jbutton

我正在使用Java创建一个便利贴应用程序.

我想做什么: 我想在textArea每次点击增加大小时增加文本的大小.我会明白如何做相反的事情.

短代码:

        JButton incButton = new JButton("+");
        fontFrame.add(incButton);
        incButton.addActionListener(new fontIncAction());
        JButton DecButton = new JButton("-");
        fontFrame.add(DecButton);

        //textArea.setFont( Font("Serif", Font.PLAIN, fz));
    }
}

private class fontIncAction implements ActionListener{
    public void actionPerformed(ActionEvent e){

        textArea.setFont(new Font("Serif",Font.PLAIN,20));
    }
}
Run Code Online (Sandbox Code Playgroud)

cam*_*ckr 11

为了使代码更通用,您可以在ActionListener中执行以下操作:

Font font = textArea.getFont();
float size = font.getSize() + 1.0f;
textArea.setFont( font.deriveFont(size) );
Run Code Online (Sandbox Code Playgroud)