import java.awt.*; import java.awt.event.*; import javax.swing.*; public class MyDemo extends JFrame { private JLabel label; private JPanel buttonPanel; private ButtonGroup buttonGroup; private JRadioButton blod,italic; public MyDemo() { super("MyDemo"); setSize(300,100); setLocation(350,260); try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch(Exception e) {} Container container = getContentPane(); container.setLayout(new FlowLayout()); label = new JLabel("This is test Label!"); label.setFont(new Font("Serif",Font.PLAIN,14)); container.add(label); buttonGroup = new ButtonGroup(); blod = new JRadioButton("粗体",false); italic = new JRadioButton("斜体",false); blod.setFont(new Font("Serif",Font.PLAIN,14)); italic.setFont(new Font("Serif",Font.PLAIN,14)); buttonGroup.add(blod); buttonGroup.add(italic); RadioButtonHandler handler = new RadioButtonHandler(); blod.addItemListener(handler); italic.addItemListener(handler); buttonPanel = new JPanel(); buttonPanel.add(blod); buttonPanel.add(italic); container.add(buttonPanel,BorderLayout.SOUTH); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args) { MyDemo application = new MyDemo(); } private class RadioButtonHandler implements ItemListener { public void itemStateChanged(ItemEvent event) { if(blod.isSelected()) label.setFont(new Font("Serif",Font.BOLD,14)); else if(italic.isSelected()) label.setFont(new Font("Serif",Font.ITALIC,14)); } } } 2011-02-22 21:38:23