如何从我的内部类引用“display”变量,以在我的JLabel中的外部类中使用

时间:2017-07-02 23:52:38

标签: java user-interface jlabel non-static

我想在我的第四个JPanel中输入文本,每次按Enter键以使用文本更新第一个JPanel。我为返回键添加了一个keylistener,它在内部类AddSong中运行一个函数,该函数接受一个文本的arraylist,并返回一个字符串。我想在第一个quadrent中的JPanel上显示该字符串,但它不起作用。现在我相信它正在删除第一个JPanel,而不是更新。我很困惑,因为这是我第一次遇到关键事件,因为我是编程新手。

以下是代码:

public class MusicPlayer extends JFrame{

  private static final int WIDTH = 400;
  private static final int HEIGHT = 400;
  private JLabel songList,songPlaying,c;
  private JTextField enterSong;
  private AddSong input;

  public MusicPlayer(){

     //Text box enterSong, input passed to actionListener
     input = new AddSong();
     enterSong = new JTextField(10);
     enterSong.addActionListener(input);

     //Numbers the quadrents
     songList = new JLabel(input.printSongs(input.songList),SwingConstants.CENTER);
     songPlaying = new JLabel("2",SwingConstants.CENTER);
     c = new JLabel("c",SwingConstants.CENTER);

     //Makes and sets size of pane
     Container pane = getContentPane();
     pane.setLayout(new GridLayout(2,2));

     //Add JLabels to panes in the container
     pane.add(songList);
     pane.add(songPlaying);
     pane.add(c);
     pane.add(enterSong);




     //KeyListener - return
     addKeyListener(new KeyListener() {
        @Override
        public void keyPressed(KeyEvent ke){}
        @Override
        public void keyReleased(KeyEvent ke){}
        @Override
        public void keyTyped(KeyEvent ke){
           if(ke.getKeyCode()==KeyEvent.VK_ENTER){
              input.printSongs(input.songList);
           }
        }
     });
     add(songList);
     pack();

     setTitle("Andrew's Music Player");
     setSize(WIDTH,HEIGHT);
     setVisible(true);
     setDefaultCloseOperation(EXIT_ON_CLOSE);

  }

  public class AddSong implements ActionListener{

     //List of Songs
     ArrayList<String> songList = new ArrayList<String>();
     String display;

     public AddSong(){
        display = "";
     }
     public void actionPerformed(ActionEvent e){

        String song;
        //get the text
        song = enterSong.getText();

        //add current text string to list
        songList.add(song);

        //print songlist
        System.out.print(printSongs(songList));

     }

     public String printSongs(ArrayList<String> songList){
        display = "";
        for(int i = 0; i < songList.size(); i++){
           display += songList.get(i) + "\n";
        }
        return display;
     }
  }

  public static void main(String args[]){
     MusicPlayer boi = new MusicPlayer();
  }

}

2 个答案:

答案 0 :(得分:0)

要访问类的变量,您需要创建一个类的变量,该变量包含该变量的具体值。另一方面,addSong.display尝试在没有cocrete实例的情况下访问变量。这仅适用于静态变量。

您已经在几行之后创建了addSong的实例     input = new addSong();

从此实例中,您可以访问display变量。那么问题是,如果它已经有正确的值,或者你需要先执行一些代码才能正确初始化该值。

请在Java中仅使用以大写字母开头的类名,并尝试使用名词作为类名。

答案 1 :(得分:0)

欢迎来到SO。拥有一个动作侦听器,您可以删除键侦听器。
请注意评论:

public class MusicPlayer extends JFrame{

    private static final int WIDTH = 400;
    private static final int HEIGHT = 400;
    private JLabel songsList,songPlaying,c;
    private JTextField enterSong;
    private AddSong input;

    public MusicPlayer(){

        //Text box enterSong, input passed to actionListener
        input = new AddSong();
        enterSong = new JTextField(10);
        enterSong.addActionListener(input);

        //Numbers the quadrents
        songsList = new JLabel("No songs in list",SwingConstants.CENTER);
        songPlaying = new JLabel("2",SwingConstants.CENTER);
        c = new JLabel("c",SwingConstants.CENTER);

        //Makes and sets size of pane
        Container pane = getContentPane();
        pane.setLayout(new GridLayout(2,2));

        //Add JLabels to panes in the container
        pane.add(songsList);
        pane.add(songPlaying);
        pane.add(c);
        pane.add(enterSong);

        //adding a key listenr to the Jframe is not need
        //the  enterSong.addActionListener(input); does the work

        add(songsList);
        pack();

        setTitle("Andrew's Music Player");
        setSize(WIDTH,HEIGHT);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

    }

    public class AddSong implements ActionListener{

        //List of Songs
        ArrayList<String> songList = new ArrayList<>();

        public AddSong(){ }

        @Override
        public void actionPerformed(ActionEvent e){

            String song;
            //get the text
            song = enterSong.getText();

            //add current text string to list
            songList.add(song);

            String songListAsString = printSongs(songList);

            //update display with new song
            songsList.setText(songListAsString);

            //clear text field
            enterSong.setText("");
        }

        public String printSongs(ArrayList<String> songList){

            //using string builder is more efficient
            //to have multiple lines in a JLabel you have to use
            //html tags
            //a JList would be more appropriiate
            StringBuilder sb = new StringBuilder("<html>");

            for(int i = 0; i < songList.size(); i++){
                sb.append("<p>")
                  .append(songList.get(i))
                  .append("</p>");
            }

            sb.append("</html>");
            return sb.toString();
        }
    }

    public static void main(String args[]){
        MusicPlayer boi = new MusicPlayer();
    }
}