我必须创建一个界面,允许用户增加/减小一段文本的大小并显示该文本的当前字体大小值。
我有两个按钮,增加和减少。 我有两个标签。一个标签包含文本“X”,每次按下按钮时该文本都需要更改大小。另一个标签必须显示当前字体大小值“X”。
我已经成功实现了文本的增加/减少方法,但是我无法在单击后使文本的值增加。文本值增加时仅允许用户增加一次。我希望程序能够在每次激活按钮时将其增加 5。
我相信我必须以某种方式存储字体大小的新值并使用新值来允许我增加/减少更多。
如果有人能告诉我如何做到这一点,或展示解决方案,我将不胜感激。
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));
}
}
}
}
最佳答案
修复确实很简单:在大约 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));
}
}
}
希望这对您有所帮助,祝您好运。
关于java - 通过 JButton 问题增加文本字体值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29136198/