简体   繁体   English

使按钮保持点击状态

[英]Making a Button Stay Clicked

How do I make it so a button stay clicks after the user has exited the program? 我如何做到这一点,以便用户退出程序后按钮保持点击状态?

Let's say I have this option(as a JButton) that sets the JTextArea's text to red. 假设我有这个选项(作为JButton),它将JTextArea's文本设置为红色。 The user clicks the button, then exits the program. 用户单击按钮,然后退出程序。 When the user opens the program back up, the text is red - meaning the button is still clicked. 当用户备份打开程序时,该文本为红色-表示仍单击该按钮。

If you don't get that example, let me show you another one: 如果您没有得到该示例,那么让我向您展示另一个示例:

I have a JButton , and a JLabel . 我有一个JButton和一个JLabel When the button is clicked, it changes the background of the JLabel to blue. 单击该按钮时,它将JLabel的背景更改为蓝色。 The user clicks the button, then exits the program. 用户单击按钮,然后退出程序。 When the user opens the program back up, the button is already clicked, meaning the JLabel 's background color is blue. 当用户备份打开程序时,该按钮已经被单击,这意味着JLabel的背景色是蓝色。

Is there a way I can do that? 有办法吗?

Edit: I would prefer if I could do it without loading external files. 编辑:如果我不加载外部文件就可以这样做。

Edit 2: I like the idea of using Preference . 编辑2:我喜欢使用Preference的想法。 But, could you give me an examplre for me for one of the examples above? 但是,您能为我举例说明上述示例之一吗? I'm kind of a beginner in Java, so I have a lot of questions. 我是那种在Java 初学者 ,所以我有很多的问题。 Like, does the code go in the actionPerformed of the button? 就像,代码是否放在按钮的actionPerformed中? And, how could I save different kinds of information( JTextarea foreground as red) with Preferences ? 并且,如何使用Preferences保存不同类型的信息( JTextarea前景为红色)?

You should use Preferences for this. 您应该为此使用“首选项”。 That explanation is a little complex. 这种解释有点复杂。 But basically it saves values that can be retrieved between runs. 但基本上,它保存可以在两次运行之间检索的值。

Here's a tutorial of how to use this with a JFileChooser. 这是有关如何与JFileChooser一起使用的教程。 Here's an example I wrote: 这是我写的一个例子:

package com.sandbox;

import java.util.prefs.Preferences;

public class Sandbox {

    public static void main(String[] args) {
        Preferences prefs = Preferences.userNodeForPackage(Sandbox.class);
        Integer counter = Integer.valueOf(prefs.get("counter", "0"));
        System.out.println(counter);
        counter++;
        prefs.put("counter", String.valueOf(counter));
    }
}

The first time you run this, it'll print "0". 第一次运行时,它将显示“ 0”。 The next time you run it, it'll print "1". 下次运行它时,它将显示“ 1”。

Edit 2: I like the idea of using Preference. 编辑2:我喜欢使用首选项的想法。 But, could you give me an examplre for me for one of the examples above? 但是,您能为我举例说明上述示例之一吗? I'm kind of a beginner in Java, so I have a lot of questions. 我是Java的初学者,所以我有很多问题。 Like, does the code go in the actionPerformed of the button? 就像,代码是否放在按钮的actionPerformed中? And, how could I save different kinds of information(JTextarea foreground as red) with Preferences? 而且,如何使用首选项保存不同类型的信息(JTextarea前景为红色)?

Do it like this: 像这样做:

private static Preferences prefs = Preferences.userNodeForPackage(className.class);
private JFrame frame;
private JTextArea textArea;

public void init()
{
    frame = new JFrame();
    textArea = new JTextArea();

    // ...

    String storedValue = prefs.get("textAreaColor", null);
    if(storedValue != null)
    {
        Color color = new Color(Integer.parseInt(storedValue));
        textArea.setForeground(color);
    }

    frame.addWindowListener(new WindowAdapter()
    {
        @Override
        public void windowClosed(WindowEvent e)
        {
            Color color = textArea.getForeground();
            int rgb = color.getRGB();
            prefs.put("textAreaColor", Integer.toString(rgb));
        }
    });

    // ...
}

Save the settings to an external file. 将设置保存到外部文件。 Then (try to) read that file when the program starts and apply the settings. 然后(尝试)在程序启动时读取该文件并应用设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM