Java:Swing 组件未重新绘制

标签 java swing repaint

我正在使用Java网站上的一些示例代码,文件选择器似乎得到了我想要的文件,但是当我尝试更新gui中的jframe和其他组件时,我曾经调用文件选择器,什么也没有变化。我已经尝试了很多建议的修复来更新内容,但似乎没有任何效果。顺便说一句,我的大多数组件都是静态的......

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.awt.event.*;

public class GuiTester extends JFrame {

    private static String fileName = "Input File: Please select a file";
    //Create a file chooser
    private static final JFileChooser fc = new JFileChooser();
    private static JButton inputSelectorButton;
    private static JButton outputSelectorButton;
    private static JFrame frame = new JFrame( "Gui Tester" );
    private static JPanel panel = new JPanel();
    private static JLabel inputFile = new JLabel( fileName );


    public static void main(String[] args) {
        go();
    }


    private static void go() {
        inputSelectorButton = new JButton ( "Select Input File" );
        outputSelectorButton = new JButton ( "Select Output File" );
        Font bigFont = new Font( "sans", Font.BOLD, 22 );
        Font smallFont = new Font( "sans", Font.PLAIN, 9 );
        panel.setLayout(new BoxLayout( panel, BoxLayout.Y_AXIS ) );
        JLabel description0 = new JLabel(" ");        
        JLabel description6 = new JLabel(" ");
        JLabel inputFile = new JLabel( fileName );
        inputFile.setFont( smallFont );
        inputSelectorButton.addActionListener( new inputSelectorListener() );
        JButton startButton = new JButton ( "GO!" );

        panel.add(description0);
        panel.add(description6);
        panel.add( inputFile );
        panel.add( inputSelectorButton );
        panel.add( outputSelectorButton );
        panel.add( startButton );

        frame.getContentPane().add( BorderLayout.CENTER, panel );
        inputFile.setAlignmentX(JComponent.CENTER_ALIGNMENT);
        inputSelectorButton.setAlignmentX(JComponent.CENTER_ALIGNMENT);
        outputSelectorButton.setAlignmentX(JComponent.CENTER_ALIGNMENT);
        startButton.setAlignmentX(JComponent.CENTER_ALIGNMENT);
        frame.setSize(370,400);
        panel.setSize(370,400);

        frame.setVisible(true);
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    }


    public static class inputSelectorListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == inputSelectorButton) {
                int returnVal = fc.showOpenDialog( panel );

                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    File file = fc.getSelectedFile();
                    if ( file.exists() )
                        fileName = file.getPath();
                    else
                        fileName = "File not found, please select a file";
                    System.out.println(fileName);
                    inputFile.setText( fileName );
                    inputFile.validate();
                    inputFile.repaint();
                    panel.validate();
                    panel.repaint();
                    frame.validate();
                    frame.repaint();
                } else {
                    System.out.println("Open command cancelled by user.");
                }
            }
        }
    }

}

最佳答案

我没有看到你添加的任何地方

  • 输入文件
  • 面板

任何可显示的内容。

您还在隐藏 inputFile

您创建一个静态引用...

private static JLabel inputFile = new JLabel(fileName);

然后在 go 中,您创建一个本地引用...

JLabel inputFile = new JLabel(fileName);

这意味着您在 actionPerformed 方法中使用的引用与屏幕上的引用不同。

不要依赖static来解决引用问题,这会更快地咬你,然后你才能意识到......

您可能想看看:

关于Java:Swing 组件未重新绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21566396/

相关文章:

java - 如何使渐变背景颜色更平滑

java - 每次更新时移动 JLabel 的位置

java - JPype 头痛

java - Spring Batch在步骤内访问作业参数

java - 卡夫卡 "Login module not specified in JAAS config"

java - netbeans 中缺少 defaultCloseOperation 选项

java - Liferay DXP 7.2监听器启动失败(干净版)

java - 如何在 JColorChooser 中增加色板的大小?

java - 面板/按钮和重新绘制令人头痛

java - Repaint() 不工作,我不知道为什么