语法问题~
- Java code
import java.awt.*;import java.awt.event.*;public class BujucOne extends Frame implements ActionListener{ Panel p=new Panel(); Button bf=new Button("第一个"); Button bl=new Button("最后一个"); Button bn=new Button("下一个"); Button bp=new Button("上一个"); Button bg=new Button("搜索"); TextField tf=new TextField(); CardLayout cl=new CardLayout(); public BujucOne() { this.setTitle("CardLayout"); this.setLayout(null); this.add(p); p.setLayout(cl); for(int i=1;i<=10;i++){ Button btemp=new Button("按钮"+i); p.add(btemp,""+i);//这句话,把for循环里的每个button都添加到面板p中,里面两个参数有点不明白//对象名btemp本来是用于标识某一对象的,但这是循环所以每个对象的名称(引用)不就都成了btemp?//""+i应该是i.toString()的意思,可是这里每个button对象的对象名(或者称引用名)分别是什么?btemp1,btemp2,...? } p.setBounds(10,40,100,100); this.add(bf); bf.addActionListener(this); bf.setBounds(120,40,60,20); this.add(bl); bl.addActionListener(this); bl.setBounds(120,70,60,20); this.add(bn); bn.addActionListener(this); bn.setBounds(120,100,60,20); this.add(bp); bp.addActionListener(this); bp.setBounds(120,130,60,20); this.add(bg); bg.addActionListener(this); bg.setBounds(60,160,40,20); this.add(tf); tf.setBounds(20,160,40,60); this.setBounds(200,200,400,380); this.setVisible(true); } public void actionPerformed(ActionEvent e){ if(e.getSource()==bn){ cl.next(p); } if(e.getSource()==bp){ cl.previous(p); } if(e.getSource()==bf){ cl.first(p); } if(e.getSource()==bl){ cl.last(p); } if(e.getSource()==bg){ cl.show(p,tf.getText().trim()); tf.setText(""); } } public static void main(String args[]){ new BujucOne(); }}[解决办法]
public void add(Component comp,
Object constraints)将指定的组件添加到此容器的尾部。同时通知布局管理器使用指定的 constraints 对象将组件添加到此容器的布局中。这是一个适用于 addImpl(java.awt.Component, java.lang.Object, int) 的便捷方法。
///////////////////////////////
你在for循环中的btemp不能被引用了,什么btemp1,btemp2都不行,要是想要能引用,必须在类中声明.如果
想在ActionListener中响应,可以靠Button.getLabel()来区别各Button,就是"按键1","按键2"之类的
[解决办法]
给我分 我教你
[解决办法]
很长时间没摸过swing了