请教一个JAVA的问题:
电脑怎么判断我是在哪个文本框进行了操作呢?
我是这样做的:
for(int k=0;k<z;k++){ //z是全局变量,就是文本框的数目
credit[k]=new JTextField(10);
cr[k]=new JLabel("需求量:");
credit[k].addActionListener(new CreditListener());
}
private class CreditListener implements ActionListener{
public void actionPerformed(ActionEvent e){
int w,ammount;
try{
Object source=e.getSource();
for(int k=0;k<z;k++){
if(source instanceof credit[k])
w=k;
}
ammount=Integer.parseInt(e.getActionCommand());
}catch(NumberFormatException ne){}
}
我运行后,它说credit为不能解析符号。
请问:怎么改啊?
谢谢!
电脑怎么判断我是在哪个文本框进行了操作呢?
我是这样做的:
for(int k=0;k<z;k++){ //z是全局变量,就是文本框的数目
credit[k]=new JTextField(10);
cr[k]=new JLabel("需求量:");
credit[k].addActionListener(new CreditListener());
}
private class CreditListener implements ActionListener{
public void actionPerformed(ActionEvent e){
int w,ammount;
try{
Object source=e.getSource();
for(int k=0;k<z;k++){
if(source instanceof credit[k])
w=k;
}
ammount=Integer.parseInt(e.getActionCommand());
}catch(NumberFormatException ne){}
}
我运行后,它说credit为不能解析符号。
请问:怎么改啊?
谢谢!
for(int k=0;k<z;k++)
{
if(source ==credit[k])
{
w=k;
break;
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class Bank{
public static void main(String[] args){
Banker b;
int clients,n;
try{
n=Integer.parseInt(args[0]);
int[] capitals=new int[n];
for(int i=0;i<n;i++)
capitals[i]=Integer.parseInt(args[i+1]);
clients=Integer.parseInt(args[n+1]);
b=new Banker(n,capitals,clients);
for(int j=0;j<clients;j++)
for(int k=0;k<n;k++)
b.startCreditLine(Integer.parseInt(args[n+2+j]),j,k);
b.pack();
b.setVisible(true);
}catch(NumberFormatException e){}
}
}public class Banker extends JFrame{
int clients,currentClient;
int []capitals,availables;
int[][]max,granted,need;
int z;
public Banker(int y,int []startCapital,int numClients){
z=y;
for(int m=0;m<z;m++){
capitals[m]=startCapital[m];
availables[m]=capitals[m];
}
clients=numClients;
currentClient=-1;
max=new int [clients][z];
granted=new int [clients][z];
need=new int [clients][z];
for(int i=0;i<clients;i++){
for(int j=0;j<y;j++){
max[i][j]=0;
granted[i][j]=0;
need[i][j]=0;
}
}
JTextField[] credit;
JLabel[] cr;
JTextField client=new JTextField(10);
client.addActionListener(new ClientListener());
JLabel cl=new JLabel("进程数: ");
for(int k=0;k<z;k++){
credit[k]=new JTextField(10);
cr[k]=new JLabel("需求量:");
credit[k].addActionListener(new CreditListener());
}
JPanel p=new JPanel();
GridLayout family=new GridLayout(z+1,2);
p.setLayout(family);
p.add(cl);
p.add(client);
for(int n;n<z;n++){
p.add(cr[n]);
p.add(credit[n]);
}
setContentPane(p);
}
private class CreditListener implements ActionListener{
public void actionPerformed(ActionEvent e){
int w,ammount;
try{
Object source=e.getSource();
for(int k=0;k<z;k++){
if(source==credit[k])
w=k;
}
ammount=Integer.parseInt(e.getActionCommand());
grant(ammount,currentClient,w);
if(!safe()){
grant(-ammount,currentClient,w);
System.out.println("没有分配第"+w+"种资源"+ammount+"给"+currentClient+"进程");
System.out.println("第"+w+"种资源可以利用的数量为"+availables[w]);
}else{
System.out.println("已经分配第"+w+"种资源"+ammount+"给"+currentClient+"进程");
System.out.println("可以利用的第"+w+"种资源为"+availables[w]);
}
}catch(NumberFormatException ne){}
}
}
private class ClientListener implements ActionListener{
public void actionPerformed(ActionEvent e){
try{
currentClient=Integer.parseInt(e.getActionCommand());
System.out.println("进程:"+currentClient);
int i;
for(i=0;i<z;i++)
System.out.println("第"+i+"种资源的最大需求量为:"+max[currentClient][i]);
System.out.println("第"+i+"种资源分配到:"+granted[currentClient][i]);
System.out.println("第"+i+"种资源还需要:"+need[currentClient][i]);
System.out.println("第"+i+"种资源可以利用数目为:"+availables[i]);
}catch(NumberFormatException ne){}
}
}
public void startCreditLine(int x,int client,int capital){
max[client][capital]=x;
need[client][capital]=x;
}
private boolean safe(){
boolean[] finished=new boolean[clients];
int[]workingCapital;
for(int k=0;k<z;k++)
workingCapital[k]=availables[k];
int candidate=0;
int t;
for(int a=0;a<z;a++)
t+=need[currentClient][a];
if(t==0) return true;
for(int q=0;q<z;q++){
if(need[currentClient][q]<0)
System.out.println("你申请的第"+q+"种资源树超量了!");
return false;
}
for(int r=0;r<z;r++){
if(workingCapital[r]<0)
System.out.println("第"+r+"种资源数不能满足你的要求!");
return false;
}
for(int s=0;s<clients;s++)
finished[s]=false;
candidate=nextCandidate(finished,workingCapital);
while(candidate<clients){
finished[candidate]=true;
for(int u=0;u<z;u++)
workingCapital[u]=workingCapital[u]+need[candidate][u];
candidate=nextCandidate(finished,workingCapital);
}
return (allSatisfied(finished));
}
private int nextCandidate(boolean[] f,int[] wc){
for(int i=0;i<f.length;i++){
int w=z;
for(int j=0;j<z;j++){
if(f[i]==false&&need[i][j]<=wc[j])
w=w-1;
}
if(w==0) return i;
}
return f.length;
}
private boolean allSatisfied(boolean[] f){
for(int i=0;i<f.length;i++){
if(!f[i]) return false;
}
return true;
}
private void grant(int x,int client,int c){
granted[client][c]=granted[client][c]+x;
availables[c]=availables[z]-x;
need[client][c]=need[client][c]-x;
}
}运行的结果是:
Banker.java:55: 不能解析符号
符号:变量 credit
位置:类 in Banker.CreditListener
if(source==credit[k])
^
1 个错误谁可以帮我看看,谢谢!!!我的QQ号码是 6097035
我们交个朋友,共同学习JAVA吧?