📄 fontsetdemo.java
字号:
//JCheckBox、JComboBox、JList和JRadioButton组件综合演示
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class FontSetDemo extends JFrame implements ActionListener,ItemListener,ListSelectionListener
{
JPanel pnlMain;
JLabel lblTest;
JList lstSize;
JRadioButton rbtnRed,rbtnGreen;
JCheckBox chkBold,chkItalic;
JButton btnExit;
JComboBox cmbType;
ButtonGroup grpColor;
public FontSetDemo()
{
super("组件综合演示");
lblTest=new JLabel("测试文字");
grpColor=new ButtonGroup();
rbtnRed=new JRadioButton("红色");
grpColor.add(rbtnRed);
rbtnRed.setSelected(true);
rbtnRed.addItemListener(this);
rbtnGreen=new JRadioButton("绿色");
grpColor.add(rbtnGreen);
rbtnGreen.addItemListener(this);
chkBold=new JCheckBox("加粗");
chkBold.addItemListener(this);
chkItalic=new JCheckBox("倾钭");
chkItalic.addItemListener(this);
lstSize=new JList();
lstSize.addListSelectionListener(this);
// lstSize.addItemListener(this);
// for (int i=10;i<30;i+=2)
// lstSize.add(String.valueOf(i),lstSize);
btnExit=new JButton("退出");
btnExit.addActionListener(this);
pnlMain=new JPanel();
pnlMain.add(lblTest);
pnlMain.add(rbtnRed);
pnlMain.add(rbtnGreen);
pnlMain.add(chkBold);
pnlMain.add(chkItalic);
pnlMain.add(lstSize);
pnlMain.add(btnExit);
this.setContentPane(pnlMain);
setSize(400,300);
setVisible(true);
}
public void valueChanged(ListSelectionEvent e)
{
}
public void actionPerformed(ActionEvent evt)
{
if (evt.getSource()==btnExit)
System.exit(0);
}
public void itemStateChanged(ItemEvent evt)
{
if (evt.getSource()==rbtnRed)
lblTest.setForeground(Color.RED);
if (evt.getSource()==rbtnGreen)
lblTest.setForeground(Color.GREEN);
int intBold=(chkBold.isSelected()?Font.BOLD:Font.PLAIN);
int intItalic=(chkItalic.isSelected()?Font.ITALIC:Font.PLAIN);
int intSize=Integer.parseInt((String)lstSize.getSelectedValue());
lblTest.setFont(new Font("宋体",intBold+intItalic,intSize));
}
public static void main(String args[])
{
new FontSetDemo();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -