import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Lab7_5 extends JApplet implements ActionListener
{
JLabel idLabel,nameLabel,scoreLabel;
JTextField idField,nameField,scoreField,statusField;
JButton addBtn,minBtn,maxBtn,avgBtn;
JTextArea showData;
String [] id,name= new String[20];
double score[]=new double[20];
int i=0 ;
String str="",str2="",str3="";
public void init()
{
Container container = getContentPane();
container.setLayout(new FlowLayout());
idLabel = new JLabel(" Enter Student ID : ");
container.add(idLabel);
idField = new JTextField(15);
container.add(idField);
nameLabel = new JLabel("Enter Student Name :");
container.add(nameLabel);
nameField = new JTextField(15);
container.add(nameField);
scoreLabel = new JLabel("Enter Student Score :");
container.add(scoreLabel);
scoreField = new JTextField(15);
container.add(scoreField);
addBtn = new JButton("Add");
addBtn.addActionListener(this);
container.add(addBtn);
String blank="";
for(int n=1 ; n<=80;n++);
blank+= " ";
container.add(new JLabel(blank));
showData = new JTextArea(10,26);
container.add(showData);
container.add(new JLabel(blank));
minBtn = new JButton("Minimum");
minBtn.addActionListener(this);
container.add(minBtn);
maxBtn = new JButton("Maximum");
maxBtn.addActionListener(this);
container.add(maxBtn);
avgBtn = new JButton("Average");
avgBtn.addActionListener(this);
container.add(avgBtn);
statusField = new JTextField(26);
container.add(statusField);
}
public void actionPerformed(ActionEvent event)
{
if(event.getSource()==addBtn)
addData();
else if(event.getSource()==minBtn)
minData();
}
public void addData()
{
str=idField.getText();
str2=nameField.getText();
str3=scoreField.getText();
showData.append(str+"\t"+str2+"\t"+str3);
showData.append("\n");
id=str;
name=str2;
score=Double.parseDouble(str3);
i++;
}
public void minData()
{
double min = score[0];
for(int b=0 ; b<id.length;b++)
{
if(min>score)
min=score ;
}
statusField.setText(Double.toString(min));
}
}
ปัญหาคือมันใช้ array เก็บค่า แล้วพอผมใช้ array ผมแสดง setText ไม่ได้ ถ้ามี[] อยู่เหนือ setText แสดงไม่ออกเลย ทำไงดีครับ
่ช่วยดูโค้ด java หน่อยครับ
import java.awt.event.*;
import javax.swing.*;
public class Lab7_5 extends JApplet implements ActionListener
{
JLabel idLabel,nameLabel,scoreLabel;
JTextField idField,nameField,scoreField,statusField;
JButton addBtn,minBtn,maxBtn,avgBtn;
JTextArea showData;
String [] id,name= new String[20];
double score[]=new double[20];
int i=0 ;
String str="",str2="",str3="";
public void init()
{
Container container = getContentPane();
container.setLayout(new FlowLayout());
idLabel = new JLabel(" Enter Student ID : ");
container.add(idLabel);
idField = new JTextField(15);
container.add(idField);
nameLabel = new JLabel("Enter Student Name :");
container.add(nameLabel);
nameField = new JTextField(15);
container.add(nameField);
scoreLabel = new JLabel("Enter Student Score :");
container.add(scoreLabel);
scoreField = new JTextField(15);
container.add(scoreField);
addBtn = new JButton("Add");
addBtn.addActionListener(this);
container.add(addBtn);
String blank="";
for(int n=1 ; n<=80;n++);
blank+= " ";
container.add(new JLabel(blank));
showData = new JTextArea(10,26);
container.add(showData);
container.add(new JLabel(blank));
minBtn = new JButton("Minimum");
minBtn.addActionListener(this);
container.add(minBtn);
maxBtn = new JButton("Maximum");
maxBtn.addActionListener(this);
container.add(maxBtn);
avgBtn = new JButton("Average");
avgBtn.addActionListener(this);
container.add(avgBtn);
statusField = new JTextField(26);
container.add(statusField);
}
public void actionPerformed(ActionEvent event)
{
if(event.getSource()==addBtn)
addData();
else if(event.getSource()==minBtn)
minData();
}
public void addData()
{
str=idField.getText();
str2=nameField.getText();
str3=scoreField.getText();
showData.append(str+"\t"+str2+"\t"+str3);
showData.append("\n");
id=str;
name=str2;
score=Double.parseDouble(str3);
i++;
}
public void minData()
{
double min = score[0];
for(int b=0 ; b<id.length;b++)
{
if(min>score)
min=score ;
}
statusField.setText(Double.toString(min));
}
}
ปัญหาคือมันใช้ array เก็บค่า แล้วพอผมใช้ array ผมแสดง setText ไม่ได้ ถ้ามี[] อยู่เหนือ setText แสดงไม่ออกเลย ทำไงดีครับ