0
我遇到了一些麻煩。我試圖從繪製方法(圖形g)中繪製字符串(toString()),我有兩個主要問題,我相信:在JPanel上繪製字符串
1)當Repaint被調用時,它似乎沒有響應。
2)當我嘗試從一個obj調用。在一個構造函數中,我得到一個'Applet'沒有初始化的錯誤。
這可能是一個初學者的錯誤,但任何幫助真的很感激..這是我第一次在這個網站上發佈。
這裏是我有問題的applet代碼:
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.util.*;
import java.lang.*;
/**
*
*
*/
public class GradeBookApp extends JApplet
implements ActionListener
{
private JRadioButton jrbadd,jrbdel,jrbupdate,jrbsortgrade,jrbsortname;
private JPanel select,drawselect,infocore;
private JButton operate;
private JLabel nameprint;
private int numstu;
private Student[] list; // Not set in init..
public void init(){
JPanel select = new JPanel();
select.setLayout(new GridLayout(2,3));
JPanel drawselect = new JPanel();
drawselect.setBackground(Color.pink);
JPanel infocore = new JPanel();
infocore.setBackground(Color.red);
jrbadd = new JRadioButton("Add Student ");
select.add(jrbadd);
jrbdel = new JRadioButton("Delete Student ");
select.add(jrbdel);
jrbupdate = new JRadioButton("Update Student ");
select.add(jrbupdate);
jrbsortgrade = new JRadioButton("Sort: Grade");
select.add(jrbsortgrade);
jrbsortname = new JRadioButton("Sort: Name");
select.add(jrbsortname);
operate = new JButton("Start... ");
operate.addActionListener(this);
operate.setBackground(Color.magenta);
select.add(operate);
ButtonGroup gr = new ButtonGroup();
gr.add(jrbadd);
gr.add(jrbdel);
gr.add(jrbupdate);
gr.add(jrbsortgrade);
gr.add(jrbsortname);
Container c= getContentPane();
c.add(select,BorderLayout.NORTH);
c.add(drawselect,BorderLayout.CENTER);
c.add(infocore,BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent e){
if(operate.isSelected()){
repaint();
}
}
public void StudentList(){
list = new Student[30];
numstu = 9;
list[0] = new Student("Moba Fett",77.5,'M',1981);
list[1] = new Student("Marion Silver",45.3,'F',2000);
list[2] = new Student("Tom Highlander",99.1,'M',1986);
list[3] = new Student("Ross Scott",74.2,'M',1971);
list[4] = new Student("Jon Smith",53.1,'M',1945);
list[5] = new Student("Jade Cerion",100.0,'F',2007);
list[6] = new Student("Jorge Bush",0,'M',2008);
list[7] = new Student("Tom Fulp",89.2,'M',1981);
list[8] = new Student("Rose DimWitt",0.2,'F',1912);
list[9] = new Student("Arya Stark",91.9,'F',8763);
}
public void delete(){
}
public void update(){
}
public void add(){
}
public void sortgrade(){
}
public void sortname(){
}
public void paint (Graphics g){
super.paint(g);
int i = 0; // local
int x = 100; // local
int y = 100; // local
// numstu called from private list...9
for(i=0;i<=numstu;i++){
if(list[i].GetGPA()>65){
g.setColor(Color.green);
g.drawString(list[i].toStr(),x,y);
}
else if(list[i].GetGPA()<65){
g.setColor(Color.red);
g.drawString(list[i].toStr(),x,y);
}
y=y+10;
}
}
}
,這裏是構造函數:這裏
public class Student
{
private String name;
private double gpa;
private char gender;
private int year;
public Student(){
name = "John Doe";
gpa = 100;
gender = 'u';
year = 1972;
}
public Student(String n,double gp,char g,int y){
name=n;
gpa=gp;
gender=g;
year=y;
}
public String GetName(){
return name;
}
public double GetGPA(){
return gpa;
}
public char GetGender(){
return gender;
}
public int GetYear(){
return year;
}
public void ChangeName(String n){
name = n;
}
public void ChangeGpa(double gp){
gpa = gp;
}
public void ChangeGender(char g){
gender = g;
}
public void ChangeYear(int y){
year = y;
}
public String toStr(){
String f = name.substring(0,name.indexOf(" "));
String l = name.substring(name.indexOf(" "), name.length());
String n =(l+","+f+" "+gender+" "+year+" "+gpa);
return n;
}
private int find(int amountofstudents,Student[] list){
return 1337; // unf.
}
}