[英]How to pass JSpinner value from Java class to another Java class
我對 Java 的了解很差,尤其是在課程方面。 我有一個 class,它有一個public static JSpinner
和SpinnerNumberModel(2, 2, 4, 1)
。 更新 JSpinner 中的值(2、3 或 4)后,按下 JButton(單獨的類)后,必須將其傳遞給同一個 JButton。 但是只傳遞我已經設置為默認值的值,即2。在搜索所有可能的論壇后,我意識到問題可能出在static
修飾符上。 但是,如果沒有這個修飾符,就會出現“Non-static field 'JSpinner_name' cannot be referenced from a static context”的錯誤。 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();
}
}
我需要從 JSpinner 向其傳遞值的 class 代碼:
public class saveSettingsButton extends JButton {
public void main(){
//some code
addActionListener(e -> {
int errorChecking = 0;
switch ((int) settingsPanel.playersCount.getValue()) {
//some code
});
}
}
我想將settingsPanel.playersCount.getValue()
傳遞給 saveSettingsButton class,但如上所述,僅傳遞預定義值。 我試圖創建一個方法,但仍然無法正常工作(也許我是方法的菜鳥)。 如何在另一個 class 中提供來自 JSpinner 的更新值?
Java 教程通常希望用戶“擴展”用戶界面類以使用它們。 這在理論上很棒,但在實踐中,通常會導致過度殺戮,尤其是對於初學者而言。
這是一個使用您的組件的簡單程序,它允許按鈕從您的微調器中讀取值。
我添加了評論來解釋應用程序的每個組件在做什么以及為什么這樣做。
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.