2013-05-01 68 views
0

正如你们可以看到我在将面板添加到框架中时遇到麻烦......我知道这可能是一件容易的事情......但我无法看到它......如果你们可以指引我走向正确的方向:)如何让这个面板作为一个内部类来工作?

import java.util.List; 
import javax.swing.*; 

/** 
* 
* @author Doohickey 
*/ 
public class StudentInfo { 

    /** 
    * @param args the command line arguments 
    */ 
    private String firstName, lastName; 
    private String birthday, studentID; 
    private String pictureFileLocation; 
    private char gender; 
    private List<String> enrolledPapers; 

    public StudentInfo(String studentID){ 
     this.studentID = studentID; 
    } 

    public StudentInfo(String studentID, String firstName, String lastName, 
      String birthday, char gender){ 

     this.studentID = studentID; 
     this.firstName = firstName; 
     this.lastName = lastName; 
     this.birthday = birthday; 
     this.gender = gender; 
    } 

    public void addPaper(String paper){ 
     enrolledPapers.add(paper); 
    } 

    public List<String> getEnrolledPapers(){ 
     return enrolledPapers; 
    } 

    public void setPictureFromFileName(String fileName){ 
     pictureFileLocation = fileName; 
    } 

    public String getPictureFileName(){ 
     return pictureFileLocation; 
    } 

    public String getFirstName(){ 
     return firstName; 
    } 

    public String getLastName(){ 
     return lastName; 
    } 

    public String getBirthday(){ 
     return birthday; 
    } 

    public String getStudentID(){ 
     return studentID; 
    } 

    public char getGender(){ 
     return gender; 
    } 

    public JComponent getStudentPanel(){ 
     return null; 
    } 

    public class StudentPanel extends JPanel{ 
     public StudentPanel(){ 

      JTextField stFirstName = new JTextField("First name: "+firstName); 
      JTextField stLastName = new JTextField("Last name: "+ lastName); 
      JComboBox stBirth = new JComboBox(); 
      JRadioButton stGender = new JRadioButton(); 
      JPanel stPanel = new JPanel(); 

      stPanel.add(stFirstName); 
      stPanel.add(stLastName); 
      stPanel.add(stBirth); 
      stPanel.add(stGender); 

      add(stPanel); 
     }  
    } 

    public static void main(String[] args) { 
     // TODO code application logic here 
     JFrame frame = new JFrame("Student info"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().add(new StudentPanel()); 
     frame.pack();          //pack frame 
     frame.setVisible(true); 
    } 
} 

回答

2

您试图从静态上下文中创建一个非静态类。

试图让你内板静态...

public static class StudentPanel extends JPanel { 

除非这只是一些测试,我会避免方式是混合的类。你没有增加任何价值StudentInfo信息类

+0

没有任何其他方式来做到这一点? 因为如果我这样做,那里的变量不会工作 – drigonlac 2013-05-01 04:45:59

+0

而不是直接依赖'StudentInfo'的'StudentPanel','StudentInfo'应该被传递给'StudentPanel'。这使得'StudentPanel'可重复使用 – MadProgrammer 2013-05-01 04:51:23

+0

嗡嗡声我觉得我明白了生病给它一个尝试的欢呼声 – drigonlac 2013-05-01 05:03:54

相关问题