package test;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionListener;
import javax.swing.*;
public class RadioButtonFrame extends JFrame
{
private JPanel buttonPanel;
private ButtonGroup group;
private JLabel label;
private static final int SIZE = 36;
public RadioButtonFrame()
{
label = new JLabel("hzy is good.");
label.setFont(new Font("hzy", Font.BOLD, SIZE));
add(label, BorderLayout.NORTH);
buttonPanel = new JPanel();
group = new ButtonGroup();
addRadioButton("Small", 8);
addRadioButton("Medium", 12);
addRadioButton("Large", 18);
addRadioButton("Bigest", 36);
add(buttonPanel, BorderLayout.SOUTH);
pack();
}
public void addRadioButton(String name, int size)
{
boolean selected = size == SIZE;
var button = new JRadioButton(name, selected);
group.add(button);
buttonPanel.add(button);
ActionListener listener = event ->
label.setFont(new Font("hzy", Font.PLAIN, size));
button.addActionListener(listener);
}
public static void main(String[] args)
{
EventQueue.invokeLater(()->
{
var frame = new RadioButtonFrame();
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//窗口关闭,应用程序结束
frame.setVisible(true); //显示窗口
});
}
}