使用接口的JLabel,setText不起作用

时间:2015-05-16 12:51:01

标签: java swing interface jlabel settext

我正在处理一个小项目,并希望显示已打开文件的名称。 问题是,应该显示文件名的JLabel没有重新绘制,我读过它应该自己重新绘制..

这是我的FileChooser代码并获取文件名(我知道我得到了文件的路径,我稍后会格式化它。

     /**
     * Opens a window where the user can select a file.
     *
     * @return Scanner in
     */
    public Scanner openFile() {
        JFileChooser chooser = new JFileChooser();
        Scanner in = null;
        if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
            File selectedInFile = chooser.getSelectedFile();
            try {
                absolutePathOfFile = selectedInFile.getAbsolutePath();
                FileReader fileReader = new FileReader(selectedInFile.getAbsolutePath());
                in = new Scanner(fileReader);
            } catch (FileNotFoundException ex) {
                // File not found
                System.out.println("File not found!!");
            }
        }
        return in;
    }

    public List getList() {
        return listOfEveryVariable;
    }

    @Override
    public String getFileName() {
        return absolutePathOfFile;
    }

这是我的标签的代码

public Component createInfoPanel() {
    infoPanel = new JPanel(new BorderLayout());
    infoPanel.setBackground(Color.LIGHT_GRAY);

    fileInfoPanel = new JPanel(new GridLayout(1,2));
    fileInfoPanel.setBackground(Color.LIGHT_GRAY);
    infoPanel.add(fileInfoPanel, BorderLayout.WEST);

    JLabel fileInfo_1 = new JLabel();
    fileInfo_1.setText("File: ");
    fileInfoPanel.add(fileInfo_1);

    JLabel fileInfo_2 = new JLabel();
    FileName fn = new Datahandler();
    fileInfo_2.setText(fn.getFileName());
    fileInfoPanel.add(fileInfo_2);

    return infoPanel;
}

创建GuiInfoPanel并将其添加到Frame中..

如果我为getFileName()类设置了一个名称并让它返回,那么它就可以工作并显示名称。

问候

这里调用createInfoPanel:

public class GuiFrame extends JFrame {

    private static final int FRAME_WIDTH = 800;
    private static final int FRAME_HIGHT = 600;

    /**
     * The variables for the panels
     */
    private JPanel  mainPanel, menuPanel, plotPanel, scatterplotPanel,
                    histogramPanel, histogram1, histogram2;

    public GuiFrame() {
    setSize(FRAME_WIDTH, FRAME_HIGHT);
    /**
     * Create Objects
     */
    GuiMenuBar mb = new GuiMenuBar();
    GuiInfoPanel ip = new GuiInfoPanel();
    GuiOptionPanel op = new GuiOptionPanel();
    JComponent sp = new Scatterplot();
    /**
     * Create Panels
     */
    createMainPanel();
    mb.createMenuBar();
    ip.createInfoPanel();
    op.createOptionPanel();

    /**
     * Add Panels
     */
    this.add(mainPanel);
    this.setJMenuBar(mb);
    this.add(ip.infoPanel, BorderLayout.SOUTH);
    menuPanel.add(op.optionPanel, BorderLayout.NORTH);
    scatterplotPanel.add(sp, BorderLayout.CENTER);
    }




    private Component createMainPanel(){
        mainPanel = new JPanel(new BorderLayout());

        menuPanel = new JPanel(new BorderLayout());
        mainPanel.add(menuPanel, BorderLayout.WEST);

        plotPanel = new JPanel(new GridLayout(2,1));   
        plotPanel.setBorder(BorderFactory.createLineBorder(Color.black));
        mainPanel.add(plotPanel, BorderLayout.CENTER);

        scatterplotPanel = new JPanel(new BorderLayout());
        scatterplotPanel.setBorder(BorderFactory.createLineBorder(Color.black));
        plotPanel.add(scatterplotPanel);

        histogramPanel = new JPanel(new GridLayout(1,2));
        plotPanel.add(histogramPanel);

        histogram1 = new JPanel(new BorderLayout());
        histogram1.setBorder(BorderFactory.createLineBorder(Color.black));
        histogramPanel.add(histogram1);

        histogram2 = new JPanel(new BorderLayout());
        histogram2.setBorder(BorderFactory.createLineBorder(Color.black));
        histogramPanel.add(histogram2);

        return mainPanel;    
    }
}

我认为只调用一次面板就是问题所在。

1 个答案:

答案 0 :(得分:0)

看起来问题是createInfoPanel()只在您创建屏幕的位置被调用过一次。此时,setText()正在执行您期望的操作,但用户尚未选择文件。我的理由是,如果多次调用createInfoPanel(),最终会在屏幕上添加的标签数量超出预期。

当您从文件选择器中选择一个文件时,createInfoPanel()不会再次被神奇地调用,因此文本永远不会在JLabel上更新。看到调用createInfoPanel()的代码和调用openFile()的代码来验证是这种情况会很有用。

关于你的第二个问题 - 你不能在声明它的地方之外引用JLabel。当您在createInfoPanel()中声明JLabel时,它只是此方法的局部变量。如果你想让它被其他方法访问,你需要使它成为一个类变量,类似于" absolutePathOfFile"变量。所以你可以这样做:

public class GuiInfoPanel {

    private final JPanel infoPanel;
    private final JLabel fileInfo = new JLabel();

    public Component createInfoPanel() {
        infoPanel = new JPanel(new BorderLayout());
        infoPanel.setBackground(Color.LIGHT_GRAY);

        fileInfo.setText("File: ");
        infoPanel.add(fileInfo, BorderLayout.WEST); 

        return infoPanel;
    }

    /**
     * Call this whenever the user picks a new file
     */
    public void setFileInfoText(String filePath){
        fileInfo.setText("File: " + filePath);
    }
} 

编辑:您的最新代码证实了我的怀疑 - createInfoPanel()仅由构造函数调用,因此只会被调用一次。尝试使用上面的代码片段来获取您之后的行为。还有一个附注:您应该编辑原始帖子以包含更多信息,而不是将更新发布为'答案'!

编辑2:请参阅下文,了解一个独立的玩具'这个例子可以帮助你获得你之后的行为。

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class FileSelectionDemo extends JFrame
                               implements ActionListener
{

    public static void main(String[] args)
    {
        FileSelectionDemo demoScreen = new FileSelectionDemo();
        demoScreen.setSize(300, 300);
        demoScreen.setVisible(true);
    }

    public FileSelectionDemo() 
    {
        getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
        getContentPane().add(btnSelectFile);
        getContentPane().add(lblFilePath);

        btnSelectFile.addActionListener(this);
    }

    private final JLabel lblFilePath = new JLabel("");
    private final JButton btnSelectFile = new JButton("Choose File");

    public void selectFile() 
    {
        JFileChooser chooser = new JFileChooser();
        if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) 
        {
            File selectedInFile = chooser.getSelectedFile();
            String absolutePathOfFile = selectedInFile.getAbsolutePath();
            lblFilePath.setText(absolutePathOfFile);
        }
    }

    @Override
    public void actionPerformed(ActionEvent arg0)
    {
        Object source = arg0.getSource();
        if (source == btnSelectFile)
        {
            selectFile();
        }
    }
}