首页上一页 1 下一页尾页 1 条记录 1/1页
请教:帮忙给改一下,谢谢。
发表在Java图书答疑
2010-05-31
是否精华
是
否
版块置顶:
是
否
public class CheckBoxTest extends JFrame{
/**
* @param args
*/
private JPanel jpanel1=new JPanel();
private JPanel jpanel2=new JPanel();
private JTextArea jt=new JTextArea(3,10);
private JCheckBox jc1=new JCheckBox("1");
private JCheckBox jc2=new JCheckBox("2");
private JCheckBox jc3=new JCheckBox("3");
public CheckBoxTest() {
// TODO Auto-generated constructor stub
Container c=getContentPane();
setSize(400, 200);
setVisible(true);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
setTitle("复选框 的使用");
c.setLayout(new BorderLayout());
c.add(jpanel1,BorderLayout.NORTH);
final JScrollPane jsc=new JScrollPane(jt);
jpanel1.add(jsc);
c.add(jpanel2,BorderLayout.SOUTH);
[font color=#FF0000] jpanel2.add(jc1);
jc1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(jc1.isSelected())
jt.append("复选框1被选中\n");
}
});
jpanel2.add(jc2);
jc2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(jc2.isSelected())
jt.append("复选框2被选中\n");
}
});
jpanel2.add(jc3);
jc3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(jc3.isSelected())
jt.append("复选 框3被选中\n");
}
});[/font]
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new CheckBoxTest();
}
}
红字部分,我想改成用一个内部类,然后在三个复选框jc1、jc2、jc3分别调用这个内部类,试改几次都不满意,我想中间的三个if语句能不能通过调用个参数什么的,合成一条语句,呵呵。请帮忙给改一下。谢谢。
我改成这样了:
{
前面省略了
jpanel2.add(jc1);
jc1.addActionListener(new jcaction());
jpanel2.add(jc2);
jc2.addActionListener(new jcaction());
jpanel2.add(jc3);
jc3.addActionListener(new jcaction());
}
class jcaction implements ActionListener(){
public void actionPerformed(ActionEvent e){
if(jc1.isSelected()){
jt.append("复选框1被选中\n");
}
if(jc2.isSelected()){
jt.append("复选框2被选中\n");
}
if(jc3.isSelected()){
jt.append("复选 框3被选中\n");
}
}
}
/**
* @param args
*/
private JPanel jpanel1=new JPanel();
private JPanel jpanel2=new JPanel();
private JTextArea jt=new JTextArea(3,10);
private JCheckBox jc1=new JCheckBox("1");
private JCheckBox jc2=new JCheckBox("2");
private JCheckBox jc3=new JCheckBox("3");
public CheckBoxTest() {
// TODO Auto-generated constructor stub
Container c=getContentPane();
setSize(400, 200);
setVisible(true);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
setTitle("复选框 的使用");
c.setLayout(new BorderLayout());
c.add(jpanel1,BorderLayout.NORTH);
final JScrollPane jsc=new JScrollPane(jt);
jpanel1.add(jsc);
c.add(jpanel2,BorderLayout.SOUTH);
[font color=#FF0000] jpanel2.add(jc1);
jc1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(jc1.isSelected())
jt.append("复选框1被选中\n");
}
});
jpanel2.add(jc2);
jc2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(jc2.isSelected())
jt.append("复选框2被选中\n");
}
});
jpanel2.add(jc3);
jc3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(jc3.isSelected())
jt.append("复选 框3被选中\n");
}
});[/font]
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new CheckBoxTest();
}
}
红字部分,我想改成用一个内部类,然后在三个复选框jc1、jc2、jc3分别调用这个内部类,试改几次都不满意,我想中间的三个if语句能不能通过调用个参数什么的,合成一条语句,呵呵。请帮忙给改一下。谢谢。
我改成这样了:
{
前面省略了
jpanel2.add(jc1);
jc1.addActionListener(new jcaction());
jpanel2.add(jc2);
jc2.addActionListener(new jcaction());
jpanel2.add(jc3);
jc3.addActionListener(new jcaction());
}
class jcaction implements ActionListener(){
public void actionPerformed(ActionEvent e){
if(jc1.isSelected()){
jt.append("复选框1被选中\n");
}
if(jc2.isSelected()){
jt.append("复选框2被选中\n");
}
if(jc3.isSelected()){
jt.append("复选 框3被选中\n");
}
}
}