我將actionlistener寫在了一個類里,然后想在另一個類里面去實作bn陣列的監控,我該如何new這個類,求大神請教。
代碼如下。目的是想實作bn的監控
public class ApiHomework extends JFrame {
public String jbn [] = {"MC","MR","MS","M+","M-","←","CE","C","±","√","7","8","9","/","%","4","5","6","*","1/x","1","2","3","-","=","0",".","+"};
JButton []bn=new JButton[jbn.length];
String str="";
public JTextArea jt;
public ApiHomework() {
super("計算機" );
this.setBounds(100, 100, 390, 450);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
JMenuBar menubar=new JMenuBar();
this.setJMenuBar(menubar);
JMenu menu=new JMenu("查看(V)");
JMenu menu1=new JMenu("編輯(E)");
JMenu menu2=new JMenu("幫助(H)");
menubar.add(menu);
menubar.add(menu1);
menubar.add(menu2);
JTextArea jt=new JTextArea( "");
jt.setBounds(30, 5, 305, 50);
jt.setEditable(false);
JPanel jp=new JPanel();
jp.setLayout(null);
for(int i=0;i<jbn.length;i++)
{
bn[i]=new JButton(jbn[i]);
// bn=new MyActionListener(bn, jt);
}
new MyActionListener(bn, jt);
bn[0].setBounds(5, 70, 60, 40);
bn[1].setBounds(80, 70, 60, 40);
bn[2].setBounds(155, 70, 60, 40);
bn[3].setBounds(230, 70, 60, 40);
bn[4].setBounds(305, 70, 60, 40);
bn[5].setBounds(5, 125, 60, 40);
bn[6].setBounds(80, 125, 60, 40);
bn[7].setBounds(155, 125, 60, 40);
bn[8].setBounds(230, 125, 60, 40);
bn[9].setBounds(305, 125, 60, 40);
bn[10].setBounds(5, 180, 60, 40);
bn[11].setBounds(80, 180, 60, 40);
bn[12].setBounds(155, 180, 60, 40);
bn[13].setBounds(230, 180, 60, 40);
bn[14].setBounds(305, 180, 60, 40);
bn[15].setBounds(5, 235, 60, 40);
bn[16].setBounds(80, 235, 60, 40);
bn[17].setBounds(155, 235, 60, 40);
bn[18].setBounds(230, 235, 60, 40);
bn[19].setBounds(305, 235, 60, 40);
bn[20].setBounds(5, 290, 60, 40);
bn[21].setBounds(80, 290, 60, 40);
bn[22].setBounds(155, 290, 60, 40);
bn[23].setBounds(230, 290, 60,40);
bn[24].setBounds(305, 290, 60, 95);
bn[25].setBounds(5, 345, 135, 40);
bn[26].setBounds(155, 345, 60, 40);
bn[27].setBounds(230, 345, 60, 40);
for(int i1=0;i1<bn.length;i1++)
{
jp.add(bn[i1]);
}
jp.add(jt);
this.add(jp);
this.setResizable(false);
this.setVisible(true);
}
}
public class MyActionListener implements ActionListener {
JButton btn;
JButton[] bnte;
JTextArea jte;
public MyActionListener (JButton btn,JTextArea jte) {
this.btn=btn;
this.jte=jte;
}
public MyActionListener(JButton[] bn, JTextArea jt) {
// TODO Auto-generated constructor stub
}
@Override
public void actionPerformed(ActionEvent e) {
for(int i=0;i<27;i++)
if(e.getSource()==btn) {
String str = btn.getText();
jte.append(str);
}
}
}
uj5u.com熱心網友回復:
把 new MyActionListener(bn, jt) 移到 for 的上面在 for 回圈里去一個個addListener
即
MyActionListener ma = new MyActionListener(bn, jt);
for (int i=0; i<jbn.length; i++) {
bn[i]=new JButton(jbn[i]);
bn[i].addActionListener(ma);
}
然后修改MyActionListener的建構式和actionPerformed方法,改成
public MyActionLisrener(JButton[] bn, JTextField jte) {
this.btne=bn;
this.jet=jte;
}
public void actionPerformed(ActionEvent e) {
for(int i=0; i<btne.length; i++) {
if(e.getSource()==btne[i]) {
jte.append(btne[i].getText());
break;
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/225625.html
標籤:Java SE
上一篇:python簡介
下一篇:水帖
