使用按钮刷新 java 程序

Refresh java program with Button

我正在尝试制作一个刷新按钮,当我单击该按钮时,它基本上会重新启动程序。我不知道我应该怎么做。

我已经放置了我决定使用的图形用户界面来完成此操作。任何和所有帮助将不胜感激。

package pdfView;

import javax.swing.*;
import java.awt.*;

public class View extends JFrame {
    public View() {
        super("PDF Viewer");
        setLookAndFeel();
        setSize(500, 125);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        FlowLayout flo = new FlowLayout();
        setLayout(flo);
        JTextField Search = new JTextField ("Search", 29);
        JButton Search1 = new JButton("Search");
        //this is where i have the button 
        JButton ReFresh = new JButton("ReFresh");
        add(Search);
        add(Search1);
        add(ReFresh);

        setVisible(true);
    }

    private void setLookAndFeel() {
        try {
            UIManager.setLookAndFeel(
                    "com.sun.java.squing.plaf.nimbus.NimbusLookAndFeel"
                    );
        } catch (Exception exc){

        }
    }

    public static void main(String[] args) {
        View pdf = new View();
    }

} 

首先,您必须将 actionListener 分配给 ReFresh Jbutton。

您可以将接口 ActionListener 实现到 class,然后像这样覆盖 actionPerformed() 方法

public class View extends JFrame implements ActionListener{

private JButton ReFresh;

public View() {
    super("PDF Viewer");
    setLookAndFeel();
    setSize(500, 125);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    FlowLayout flo = new FlowLayout();
    setLayout(flo);
    JTextField Search = new JTextField ("Search", 29);
    JButton Search1 = new JButton("Search");
    //this is where i have the button 
    ReFresh = new JButton("ReFresh");
    ReFresh.addActionListener(this);
    add(Search);
    add(Search1);
    add(ReFresh);

    setVisible(true);
}

private void setLookAndFeel() { //right way for nimbus: http://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/nimbus.html
   try {
        for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
             if ("Nimbus".equals(info.getName())) {
                  UIManager.setLookAndFeel(info.getClassName());
                  break;
             }
         }
   }
   catch (Exception e) {
       e.printStackTrace();
   }
}

@Override
public void actionPerformed(ActionEvent e)
{
    if(e.equals(ReFresh))
    {
        super.repaint();
    }
}}

public static void main(String[] args) {
    View pdf = new View();
}

或者你可以像这样对 addActionListener 进行内联赋值

ReFresh.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                super.repaint();
            }
        });

你可以试试这些方法refresh/reloadJFrame,

invalidate();
validate();
repaint();

您也可以使用 dispose(); 然后 new View(); 创建新的 JFrame,但在此序列中它将关闭 window 并创建新的。

或者您甚至可以尝试 setVisible(false); 然后 setVisible(true);

推荐前3个

刷新或重启是什么意思?

你的意思是:

  1. 让应用保持原样,只更新它显示的内容?
  2. 真的重启应用程序吗?

更新应用程序显示的内容

您首先需要确定什么实际应该导致您的应用程序刷新。您已经谈到了 Button。激活类似按钮的机制称为 Action。您可以使用 ActionListener 手动执行这些操作,或者您可以扩展 AbstractAction,这是我推荐的。扩展 AbstractAction 允许您在 UI 上的多个位置使用相同的逻辑操作。看看典型的应用程序,它们通过菜单、工具栏、弹出菜单和键盘快捷键提供 Cut/Copy/Paste。在 Java 中实现此目的的最简单方法是通过扩展 AbstractAction.

使用 Action

更新应用程序需要调用的方法是 invalidate()validate()repaint()

重新启动应用程序

所以你想再次 运行 到 main()?这实际上不是必需的,除非您有一个支持自我更新的应用程序。即使这样,有时也可以通过巧妙地使用 ClassLoader.

来避免这种情况。

关于您的代码的更多注释

扩展反模式的使用

我不会扩展 JFrame 只是为了在屏幕上显示 window。扩展使用是一种反模式。您无需扩展 JFrame 即可在屏幕上显示 JFrame 并执行您想要的操作。

引用静态成员

我会通过它们的原始声明来引用常量。 IE。我会通过 WindowConstants.EXIT_ON_CLOSE 引用 EXIT_ON_CLOSE,而不是 JFrame.EXIT_ON_CLOSE

打字错误

您的 UIManager.setLookAndFeel() 代码中有错字。搜索 swing,您会看到拼写错误。

异常信息

您实际上可能想使用 exc.printStackTrace() 将异常打印到 stderr 而不是完全忽略它,因为当您在 LaF class 名称中有拼写错误时,就像您一样,并且您不打印异常,您实际上可能不知道出了什么问题。

小部件构建的顺序和 UIManager.setLookAndFeel()

UIManager.setLookAndFeel() 的顺序和通过 super(...) 的有效 new JFrame() 并不能保证整个 UI 都会在 Nimbus 中,它的一部分可能仍然是在金属中。为了安全起见,我建议在构建第一个小部件之前设置 LaF。据我所知,不能保证在组件构建后更改 LaF 会产生影响,除非您告诉 UIManager 更新 LaF。另请参阅 UIManager:

文档中的引用

Once the look and feel has been changed it is imperative to invoke updateUI on all JComponents. The method SwingUtilities.updateComponentTreeUI(java.awt.Component) makes it easy to apply updateUI to a containment hierarchy. Refer to it for details. The exact behavior of not invoking updateUI after changing the look and feel is unspecified. It is very possible to receive unexpected exceptions, painting problems, or worse.

http://docs.oracle.com/javase/8/docs/api/javax/swing/UIManager.html

setSize() 对比 pack()InsetsBorder

的帮助下

与其手动设置大小,不如尝试使用 InsetsBorderJFrame.pack() 以获得 window 的合理布局。手动设置大小假设您非常了解屏幕分辨率和用户的字体大小。 pack() 方法根据内容执行自动大小计算。 InsetsBorder 允许您在组件周围创建一些 space 和边框,即使有一些设计或标签,这样它们就不会被紧紧地挤在 window 中,但是很好 spaced.