如何从我的内部 class 引用 "display" 变量以在我的 JLabel 中的外部 class 中使用

How do I reference the "display" variable from my inner class, to use in the outer class in my JLabel

我想在我的第四个 JPanel 中输入文本,每次我按回车键都会用文本更新第一个 JPanel。我为 return 键添加了一个 keylistener,它在内部 class AddSong 中运行一个函数,该函数接受一个文本数组列表,并且 returns 是一个字符串。我想在第一象限的 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();
  }

}

要访问 class 的变量,您需要创建它的一个实例,其中包含该变量的具体值。另一方面,addSong.display 尝试在没有具体实例的情况下访问变量。这只允许用于静态变量。

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

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

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

欢迎来到 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();
    }
}