使用 JButton 问题增加文本字体值

increase text font value with JButton issue

我必须创建一个界面,允许用户 increase/decrease 一段文本的大小并显示该文本的当前字体大小值。

我有两个按钮,增加和减少。 我有两个标签。一个标签的文本 "X" 需要在每次按下按钮时更改大小。另一个标签必须显示当前字体大小值 "X"。

我已经成功地为文本实现了increase/decrease方法,但是我无法让文本的值在点击后增加。增加时文本的值只允许用户增加一次。我希望程序能够在每次激活按钮时将其增加 5。

我相信我必须以某种方式存储字体大小的新值并使用新值来允许我 increase/decrease 甚至更多。

如果有人能告诉我如何做到这一点,或者给出解决方案,将不胜感激。

package lab3;
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class FontSize extends JFrame{


JButton increase, decrease;
JLabel sizeX, sizeValue;


public static void main (String[]args){

    FontSize changeFont = new FontSize();
    changeFont.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    changeFont.setTitle("Increase/Decrease Font Size");
    changeFont.setSize(900,700);
    changeFont.setVisible(true);
    changeFont.setLayout(new GridLayout(2,2));

}


public FontSize(){

    increase = new JButton("increase");
    increase.setBackground(Color.white);
    increase.setFont(increase.getFont().deriveFont(30.0f));
    add(increase);

    decrease = new JButton("decrease");
    decrease.setBackground(Color.white);
    decrease.setFont(decrease.getFont().deriveFont(30.0f));
    add(decrease);

    sizeX = new JLabel("X", SwingConstants.CENTER);
    sizeX.setBackground(Color.yellow);
    sizeX.setFont(sizeX.getFont().deriveFont(30.0f));
    add(sizeX);

    int temp = sizeX.getFont().getSize();
    sizeValue = new JLabel("",SwingConstants.CENTER);
    sizeValue.setText(String.valueOf(temp));
    sizeValue.setBackground(Color.yellow);
    sizeValue.setFont(sizeValue.getFont().deriveFont(30.0f));
    add(sizeValue);

    event e = new event();
    increase.addActionListener(e);
    decrease.addActionListener(e);


 }
public class event implements ActionListener {

    public void actionPerformed(ActionEvent e){


        String operation = e.getActionCommand();
        int temp = sizeX.getFont().getSize();
        int temp2 = sizeValue.getFont().getSize();


        if(operation.equals("increase"))
            {                       
                temp = temp + 5;
                sizeX.setFont(new Font("Arial", Font.PLAIN, temp));

                temp2 = temp2 + 5;
                sizeValue.setText(String.valueOf(temp2));

            } 
        else if(operation.equals("decrease"))
            {
                temp = temp - 5;
                sizeX.setFont(new Font("Arial", Font.PLAIN, temp));

                temp2 = temp2 - 5;
                sizeValue.setText(String.valueOf(temp2));

            }

      }
  }

}

int temp2 = sizeValue.getFont().getSize(); 不是您要更改的字体大小,而是用于呈现标签的字体大小。

尝试使用更像...

        String operation = e.getActionCommand();
        int temp = sizeX.getFont().getSize();

        if (operation.equals("increase")) {
            temp = temp + 5;
            sizeX.setFont(new Font("Arial", Font.PLAIN, temp));

            sizeValue.setText(String.valueOf(temp));

        } else if (operation.equals("decrease")) {
            temp = temp - 5;
            sizeX.setFont(new Font("Arial", Font.PLAIN, temp));

            sizeValue.setText(String.valueOf(temp));

        }

您可能还需要在 actionPerformed 方法的末尾调用 revalidate();repaint(); 来强制刷新,但没有我也可以正常工作

同样,您可以像...

        Font font = sizeX.getFont();

        if (operation.equals("increase")) {
            font = font.deriveFont(font.getSize() + 5f);
        } else if (operation.equals("decrease")) {
            font = font.deriveFont(font.getSize() - 5f);
        }
        sizeX.setFont(font);
        sizeValue.setText(NumberFormat.getNumberInstance().format(font.getSize()));

这允许您保持标签最初使用的字体,但 increases/decreases 它的大小,而且还依赖于实际 Font 大小来更新显示,而不是依靠计算价值观...

真正简单的修复:在原始代码的 64 处,您不小心试图将变量 temp2 计算为它的字体大小,而不是实际文本的大小。我附上了经过稍微重构和更正的代码版本。

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

public class FontSize extends JFrame implements ActionListener {
    private JButton increase, decrease;
    private JLabel sizeX, sizeValue;

    public static void main (String[]args) {
        FontSize changeFont = new FontSize();
        changeFont.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        changeFont.setTitle("Increase/Decrease Font Size");
        changeFont.setSize(900,700);
        changeFont.setVisible(true);
        changeFont.setLayout(new GridLayout(2,2));
    }

    public FontSize(){
        increase = new JButton("increase");
        increase.setBackground(Color.white);
        increase.setFont(increase.getFont().deriveFont(30.0f));
        add(increase);

        decrease = new JButton("decrease");
        decrease.setBackground(Color.white);
        decrease.setFont(decrease.getFont().deriveFont(30.0f));
        add(decrease);

        sizeX = new JLabel("X", SwingConstants.CENTER);
        sizeX.setBackground(Color.yellow);
        sizeX.setFont(sizeX.getFont().deriveFont(30.0f));
        add(sizeX);

        int temp = sizeX.getFont().getSize();
        sizeValue = new JLabel("",SwingConstants.CENTER);
        sizeValue.setText(String.valueOf(temp));
        sizeValue.setBackground(Color.yellow);
        sizeValue.setFont(sizeValue.getFont().deriveFont(30.0f));
        add(sizeValue);

        increase.addActionListener(this);
        decrease.addActionListener(this);
    }
    public void actionPerformed(ActionEvent e) {
        String operation = e.getActionCommand();
        int temp = sizeX.getFont().getSize();
        int temp2 = Integer.parseInt(sizeValue.getText());

        if(operation.equals("increase")) {                       
            temp += 5;
            sizeX.setFont(new Font("Arial", Font.PLAIN, temp));

            temp2 += 5;
            sizeValue.setText(String.valueOf(temp2));

        } else if(operation.equals("decrease")) {
            temp -= 5;
            sizeX.setFont(new Font("Arial", Font.PLAIN, temp));

            temp2 -= 5;
            sizeValue.setText(String.valueOf(temp2));
        }
    }
}

希望对您有所帮助,祝您好运。