📄 jradiobuttontest.java
字号:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class JRadioButtonTest extends JFrame {
private JTextField field;
private Font plainFont,boldFont,italicFont,boldItalicFont;
private JRadioButton plainButton,boldButton,italicButton,
boldItalicButton;
private ButtonGroup radioGroup;
/**
* Launch the application
* @param args
*/
public static void main(String args[]) {
try {
JRadioButtonTest frame = new JRadioButtonTest();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the frame
*/
public JRadioButtonTest() {
super("JRadioButton Test");
Container container = getContentPane();
container.setLayout(new FlowLayout());
field = new JTextField("Watch the font style change",23);
container.add(field);
//create radio buttons
plainButton = new JRadioButton("Plain",true);
container.add(plainButton);
boldButton = new JRadioButton("Bold",false);
container.add(boldButton);
italicButton = new JRadioButton("Italic",false);
container.add(italicButton);
boldItalicButton = new JRadioButton("BoldItalic",false);
container.add(boldItalicButton);
//register events for JRadioButton
RadioButtonHandler handler = new RadioButtonHandler();
plainButton.addItemListener(handler);
boldButton.addItemListener(handler);
italicButton.addItemListener(handler);
boldItalicButton.addItemListener(handler);
//create logical relationShip between JRadioButtons
radioGroup = new ButtonGroup();
radioGroup.add(plainButton);
radioGroup.add(boldButton);
radioGroup.add(italicButton);
radioGroup.add(boldItalicButton);
//Creat font objects
plainFont = new Font("Serif",Font.PLAIN,14);
boldFont = new Font("Serif",Font.BOLD,14);
italicFont = new Font("Serif",Font.ITALIC,14);
boldItalicFont = new Font("Serif",Font.BOLD+Font.ITALIC,14);
field.setFont(plainFont);
setSize(800,600);
setVisible(true);
}
private class RadioButtonHandler implements ItemListener
{
public void itemStateChanged(ItemEvent event)
{
if(event.getSource()==plainButton)
field.setFont(plainFont);
if(event.getSource()==boldButton)
field.setFont(boldFont);
if(event.getSource()==italicButton)
field.setFont(italicFont);
if(event.getSource()==boldItalicButton)
field.setFont(boldItalicFont);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -