【问题标题】:Change JLabel value of void GUI constructor from external method从外部方法更改 void GUI 构造函数的 JLabel 值
【发布时间】:2013-08-11 19:25:11
【问题描述】:

我正在尝试创建一个方法,以便在我的 creatAndShowGUI() 方法中更改 JLabel 的值。我要做的是在导入文件后更改一些JLabels的值(根据文件信息)。

private static void createAndShowGUI(){
    // Create and set up window
    final JFrame frame = new JFrame("SongApp");


    // Display the window
    frame.setSize(400, 150);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // Set flowlayout
    frame.getContentPane().setLayout(new FlowLayout());
    try{
        UIManager.setLookAndFeel(
                UIManager.getSystemLookAndFeelClassName());      
    } catch(Exception e){
        System.err.println("Cannot set look and feel: " + e);
    }

    JButton loadFile = new JButton("Choose a new file ...");
    JLabel songLength = new JLabel("0:00");
    JLabel songName = new JLabel("No song selected yet");

    loadFile.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){

            createFileChooser(frame);

        }
    });

    frame.getContentPane().add(loadFile);
    frame.getContentPane().add(songLength);
    frame.getContentPane().add(songName);
}

我的方法需要是无效的,因为我稍后会在我的 main 方法中调用它

    javax.swing.SwingUtilities.invokeLater(new Runnable(){
        public void run(){
            createAndShowGUI();                
        }
    });

在此之后我创建了一个方法如下:

private static void changeWindowInformation(String filename, int time) {

    createAndShowGUI().songName.setText(filename);
    // code to check time of song and return it in format xx:xx
    createAndShoGUI().songLength.setText(time);

}

该方法在 createFileChooser 方法中调用,该方法在单击 JButton 后在 createAndShowGUI() 方法中调用。

我看到一条错误消息:无法取消引用 void。我是 Java 编程的新手,我真的不知道我做错了什么。我应该将我的框架的创建放在我的代码中的其他地方(可能在我的主要方法下?)还是我没有以正确的方式访问 JLabel?我试图将方法调用放在我的 loadFile.addActionListener 方法中,并让它只返回字符串,这样我就可以访问 loadFile.addActionListener 中的 JLabels,但我仍然收到相同的错误消息。

如果您需要任何进一步的信息,请告诉我。

已经感谢您的帮助!!

【问题讨论】:

  • 您不能从另一个方法访问在一个方法中声明的局部变量。将变量设为类变量,然后对类变量进行更改,而不是声明局部变量。

标签: java swing user-interface static-methods settext


【解决方案1】:

您应该创建自己的继承 JFrame 的 Frame-class 并将更改的标签添加为类成员,因为 gparyani 已经评论过

您不能从另一个方法访问在一个方法中声明的局部变量。

例如:

public class MyFrame extends JFrame {
  private JLabel songName, songLength;

  public MyFrame() {
    super("SongApp");
    songLength = new JLabel("0:00");
    songName = new JLabel("No song selected yet");
    JButton loadFile = new JButton("Choose a new file ...");

    //other UI settings

    loadFile.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent e){
        createFileChooser(this);
      }
    });

    getContentPane().add(loadFile);
    getContentPane().add(songLength);
    getContentPane().add(songName);
  }

  public void changeWindowInformation(String filename, int time) {
    this.songName.setText(filename);
    this.songLength.setText(yourFormattedTime); 
  }
}

您还可以将此类的引用传递给您的createFileChooser()

public void createFileChooser(MyFrame callingFrame) {
  //your filechooser logic

  callingFrame.changeWindowInformation(filename, time);
}

如果createFileChooser() 是MyFrame 的一个方法,则不需要传递MyFrame 的实例,因为changeWindowInformation 和createFileChooser 将是同一个实例的成员,因此它们共享相同的类成员。 在这种情况下,您的代码应该是:

public void createFileChooser() {
  //your filechooser logic

  changeWindowInformation(filename, time);
}

使用它,您可以将整个 createAndShowGUI()-call 替换为:

javax.swing.SwingUtilities.invokeLater(new Runnable(){
    public void run(){
        MyFrame frame = new MyFrame();
        frame.setVisible(true);               
    }
});

【讨论】:

  • 1+ 提供信息丰富的答案,一个比另一个更干净。
  • 我现在的名字是 damryfbfnetsi,而不是以前的 gparyani。 (猜测我的新名字背后的含义完全是另一回事,但这无关紧要。)
【解决方案2】:

使歌曲名称标签成为该类的成员变量。在 createAndShowGUI() 方法之外声明它,然后你就可以直接访问它了,像这样:

m_songName.setText()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-16
    • 2011-05-01
    相关资源
    最近更新 更多