简体   繁体   English

如何将 JSpinner 值从 Java class 传递给另一个 Java class

[英]How to pass JSpinner value from Java class to another Java class

I have very poor knowledge of Java, especially in terms of classes.我对 Java 的了解很差,尤其是在课程方面。 I have a class that has a public static JSpinner with SpinnerNumberModel(2, 2, 4, 1) .我有一个 class,它有一个public static JSpinnerSpinnerNumberModel(2, 2, 4, 1) After updating the value in the JSpinner (2, 3 or 4), after pressing the JButton (separate class), it must be passed to the same JButton.更新 JSpinner 中的值(2、3 或 4)后,按下 JButton(单独的类)后,必须将其传递给同一个 JButton。 But only the value that I have already set as default is passed, ie 2. After searching all possible forums, I realized that the problem might be in the static modifier.但是只传递我已经设置为默认值的值,即2。在搜索所有可能的论坛后,我意识到问题可能出在static修饰符上。 However, without this modifier, the error "Non-static field 'JSpinner_name' cannot be referenced from a static context" appears.但是,如果没有这个修饰符,就会出现“Non-static field 'JSpinner_name' cannot be referenced from a static context”的错误 The code of the class in which the JSpinner is located: JSpinner所在的class的代码:

public class settingsPanel extends JPanel {
    public static JSpinner playersCount = new JSpinner(new SpinnerNumberModel(2, 2, 4, 1));
    
    public void main(){

        //some code with JSpinner change listener

        add(playersCount);
        saveSettingsButton saveSettingsButton = new saveSettingsButton();
        add(saveSettingsButton);
        saveSettingsButton.main();
    }
}

The class code to which I need to pass values from JSpinner:我需要从 JSpinner 向其传递值的 class 代码:

public class saveSettingsButton extends JButton {
    public void main(){

        //some code

        addActionListener(e -> {
            int errorChecking = 0;
            switch ((int) settingsPanel.playersCount.getValue()) {

                //some code

        });
    }
}

I want to pass settingsPanel.playersCount.getValue() to the saveSettingsButton class, but as I described above, only the predefined value is passed.我想将settingsPanel.playersCount.getValue()传递给 saveSettingsButton class,但如上所述,仅传递预定义值。 I tried to create a method but still not working (maybe i'm a noob at methods).我试图创建一个方法,但仍然无法正常工作(也许我是方法的菜鸟)。 How to make available updated value from JSpinner in another class?如何在另一个 class 中提供来自 JSpinner 的更新值?

Java tutorials often expect users to "extend" user interface classes to use them. Java 教程通常希望用户“扩展”用户界面类以使用它们。 This is great in theory, but in practice, often leads to over kill, especially for first-timers.这在理论上很棒,但在实践中,通常会导致过度杀戮,尤其是对于初学者而言。

Here's a simple program using your components which allows the button to read values from your spinner.这是一个使用您的组件的简单程序,它允许按钮从您的微调器中读取值。

I've added comments to explain what each component of the app is doing and why it's doing it.我添加了评论来解释应用程序的每个组件在做什么以及为什么这样做。

package com.company;

import javax.swing.*;

public class MyApp {
    // Declare the UI components we plan on using
    // Note: Without "private" or "public" modifiers
    //   they'll default to "package private", which
    //   is a sane default.
    JFrame mainFrame;
    JPanel settingsPanel;
    JButton saveSettingsButton;
    JSpinner playersCount;

    public MyApp() {
        // Initialize our UI components
        mainFrame = new JFrame("My App");
        settingsPanel = new JPanel();
        saveSettingsButton = new JButton("Click Me");
        playersCount = new JSpinner(new SpinnerNumberModel(2, 2, 4, 1));

        // Add any listeners (such a "click" listener)
        saveSettingsButton.addActionListener(e -> {
            int errorChecking = 0;
            // The button can read the spinner because they're both declared within the same scope (same class)
            switch ((int) playersCount.getValue()) {
                default:
                        System.out.println((int)playersCount.getValue());
            };
        });

        // Add UI components to panel
        settingsPanel.add(playersCount);
        settingsPanel.add(saveSettingsButton);

        // Add panel to frame/window
        mainFrame.add(settingsPanel);

        // Show our UI
        mainFrame.pack(); // Automatically set size of frame to fit components
        mainFrame.setLocationRelativeTo(null); // Center frame on primary screen
        mainFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); // Close the app when this window is closed
        mainFrame.setVisible(true);

        // Perform our initial action
        saveSettingsButton.doClick();
    }

    // This is the main entry point into the application when run
    // By default, applications are NOT synchronized onto the UI thread (known as the "Event Dispatch Thread")
    // UIs that aren't synchronized can flicker, crash and have weird behavior
    // SwingUtilities has two functions to synchronize, we'll use "invokeLater()" for this purpose. 
    public static void main(String ... args) {
        SwingUtilities.invokeLater(() -> new MyApp()); // invokeLater(): UI should always be created on EDT!
    }


}

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

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