【问题标题】:How to update Jlabel Text in Swing如何在 Swing 中更新 Jlabel 文本
【发布时间】:2018-02-19 15:08:05
【问题描述】:

我有一个简单的 GUI 文件,在这里: I want to update the Label text each time when new file is selected 但是当我选择任何文件时,它与现有的 Jlabel 文本重叠,所以,请帮助我如何更新我的 JLabel 文本。

这是我的代码:

protected static void excelButtonAction(){
    excelReturnVal = fc.showOpenDialog(excelButton);
    if(excelReturnVal==JFileChooser.APPROVE_OPTION){                
        FileValidation.excelFileValidation(fc); 
        System.out.println(FileValidation.getName() );
        if(status==JFileChooser.CANCEL_OPTION){

        }else{
          fileName=FileValidation.getName();

          FileValidation.updatemylabel(fileName);
          excelFileName = new JLabel(fileName);
          excelFileName.setText(fileName);
          excelFileName.setBounds(140, 67, 350, 30);
          excelFileName.setFont(new Font("Myriad Pro",Font.PLAIN,10));
          panel.add(excelFileName);
          panel.revalidate();
          panel.repaint();
        }
    } else{
        System.out.println("Open command cancelled by user." + newline);
    }
}    

public static void updatemylabel(String exfileName){
    excelFileName = new JLabel(fileName);
    excelFileName.setText(fileName);
    JFileChooser chooser = new JFileChooser();
    chooser.addPropertyChangeListener(new PropertyChangeListener() {


        public void propertyChange(PropertyChangeEvent evt) {
            if(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY.equals(evt.getPropertyName())){
                JFileChooser chooser = (JFileChooser) evt.getSource();
                File oldFile = (File) evt.getOldValue();
                File newFile = (File) evt.getNewValue();
                File curFile = chooser.getSelectedFile();
            }else if(JFileChooser.SELECTED_FILES_CHANGED_PROPERTY.equals(evt.getPropertyName())){
                 JFileChooser chooser = (JFileChooser)evt.getSource();
                    File[] oldFiles = (File[])evt.getOldValue();
                    File[] newFiles = (File[])evt.getNewValue();
                    File[] files = chooser.getSelectedFiles();
            }

        }
    });
    excelFileName = new JLabel(fileName);
    excelFileName.setText(fileName);
    excelFileName.setBounds(140, 67, 350, 30);
    excelFileName.setFont(new Font("Myriad Pro",Font.PLAIN,10));
    panel.add(excelFileName);
    panel.revalidate();
    panel.repaint();

    existingText=exfileName;


    }

如果需要任何进一步的信息来解决我的问题,请告诉我。 提前感谢您的合作。

【问题讨论】:

标签: java swing jlabel


【解决方案1】:

您的代码每次都会创建一个新的JLabel 实例。您需要创建一次实例,将其存储在类的字段中,并在需要更新时调用setText()

【讨论】:

  • 如果您能详细解释一下,将不胜感激,虽然我也尝试过这种方式,但没有运气
  • 您到底尝试了什么,“没有运气”到底是什么意思?
  • @Ravi,静态方法的使用告诉我你需要重新设计你的代码。您不应该使用静态方法。我建议您首先阅读 How to Use Text Areas 上的 Swing 教程中的部分。它向您展示了如何创建 JTextArea 的单个实例,然后根据需要以不同的方法更新文本。因此,请从教程中下载TextDemo 代码并了解其工作原理,然后修复您的代码。
【解决方案2】:

您可以查看以下内容以更好地理解 java 中的标签:

setText

public void setText(字符串文本)

定义此组件将显示的单行文本。如果 text 的值为 null 或空字符串,则不显示任何内容。 此属性的默认值为 null。

这是一个 JavaBeans 绑定属性。

【讨论】:

    猜你喜欢
    • 2013-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多