我将从解释下面的程序应该做什么开始。好吧,当您单击按钮时,文本会随着文本的宽度和/或高度稳步增加。以这种方式,整个文本始终可见。
问题是,当缩放按钮不会增加显示区域时,只需单击几下即可开始隐藏。程序本身应该增加显示区域,以便所有内容始终可见。
屏幕发布它的外观和应该如何。
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.*;
import javax.swing.*;
public class Main {
public static void main(String ... args) {
JFrame frame = new JFrame("JFrame");
final GridLayout gd = new GridLayout(0,1);
frame.setLayout(gd);
final JPanel panel1 = new JPanel();
panel1.setLayout(new FlowLayout());
JPanel panel2 = new JPanel();
panel2.setLayout(new FlowLayout());
final JButton btn1 = new JButton("Button 1");
final JButton btn2 = new JButton("Button 2");
btn1.addActionListener( new ActionListener() {
int fontSize = 14;
public void actionPerformed(ActionEvent e){
Font font = new Font("Arial", Font.PLAIN, fontSize);
btn1.setFont(font);
fontSize++;
}
});
btn2.addActionListener( new ActionListener() {
int fontSize = 14;
public void actionPerformed(ActionEvent e){
Font font = new Font("Arial", Font.PLAIN, fontSize);
btn2.setFont(font);
fontSize++;
}
});
panel1.add(btn1);
panel2.add(btn2);
frame.add(panel1);
frame.add(panel2);
//frame.setPreferredSize(new Dimension(200, 100));
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}