【问题标题】:Refresh java program with Button用 Button 刷新 java 程序
【发布时间】:2015-03-05 20:33:31
【问题描述】:

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

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

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();
    }

} 

【问题讨论】:

  • 当您的按钮被点击时,您需要使用事件处理程序来执行代码。然后你就可以执行你的刷新代码了。

标签: java swing button refresh listener


【解决方案1】:

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

您可以为类实现接口 ActionListener,并像这样覆盖 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();
            }
        });

您可以尝试这些方法来刷新/重新加载 JFrame,

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

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

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

我推荐前 3 个。

【讨论】:

  • 谢谢我在理解动作监听器时遇到了很多麻烦,这对我很有帮助。 :)
【解决方案2】:

刷新或重启是什么意思?

你的意思是:

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

更新应用程序显示的内容

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

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

重新启动应用程序

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

关于您的代码的更多注释

通过扩展反模式使用

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

引用静态成员

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

错字

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

异常信息

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

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

UIManager.setLookAndFeel() 的序列和有效的new JFrame() 通过super(...) 并不能保证整个 UI 将在 Nimbus 中,部分用户界面可能仍在 Metal 中。为了安全起见,我建议在构建第一个小部件之前设置 LaF。据我记得,并不能保证在组件构建后更改 LaF 会产生效果,除非您告诉 UIManager 更新 LaF。另请参阅UIManager 文档中的此引用:

一旦改变了外观和感觉,就必须在所有 JComponent 上调用 updateUI。 SwingUtilities.updateComponentTreeUI(java.awt.Component) 方法可以轻松地将 updateUI 应用于包含层次结构。有关详细信息,请参阅它。未指定在更改外观后不调用 updateUI 的确切行为。很可能会收到意外的异常、绘制问题或更糟的情况。

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

setSize()pack()InsetsBorder 的帮助下

您可能希望使用InsetsBorderJFrame.pack() 而不是手动设置大小,以获得合适的窗口布局。手动设置大小假定您对屏幕分辨率和用户的字体大小有很多了解。 pack() 方法根据内容自动计算大小。 InsetsBorder 允许您在组件周围创建一些空间和边框,即使有一些设计或标签,这样它们就不会被紧紧地挤在窗口中,而是很好地间隔。

【讨论】:

  • 感谢您提供的所有有用信息。这对我帮助很大。
猜你喜欢
  • 2011-06-29
  • 2012-05-03
  • 2015-11-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-23
  • 2010-09-16
相关资源
最近更新 更多