JRadioButton单选按钮代码1

直接上代码吧。

public class JRadioButtonTest extends JApplet{
public void init() {
Container contentPane = getContentPane();


contentPane.add(new PrintRangePanel(100, 101));
}
}
class PrintRangePanel extends JPanel {
JRadioButton printAll, printRange;
JLabel startPage, endPage;
JTextField startField, endField;


public PrintRangePanel(int start, int end) {
ButtonGroup group= new ButtonGroup();


printAll = new JRadioButton("Print All");
printRange = new JRadioButton("Print Range");


startPage = new JLabel("Start Page:");
endPage = new JLabel("End Page:");


startField = new JTextField(Integer.toString(start));
endField = new JTextField(Integer.toString(end));


add(printAll); 
add(printRange);
add(startPage);
add(startField);
add(endPage); 
add(endField);


printRange.setSelected(true);


group.add(printAll);
group.add(printRange);


printRange.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent event) {
if(printRange.isSelected()) {
startField.setEnabled(true);
endField.setEnabled (true); 
startPage.setEnabled (true); 
endPage.setEnabled (true); 

startField.requestFocus();
}
}
});

printAll.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent event) {
if(printAll.isSelected()) {
startField.setEnabled(false);
endField.setEnabled (false);
startPage.setEnabled (false);
endPage.setEnabled (false);

startPage.repaint();
endPage.repaint();
}
}
});
}
}



你可能感兴趣的:(swing)