所以基本上我在这里有点困惑,我正在使用 Eclipse 的 WindowsPro Builder 插件,它使所有 JFrame 组件都在自定义的初始化()类中。这给我带来了一个问题,通常我在一开始就定义我的组件,这样我就可以通过我的程序公开访问它们。不,我有第二类,但我无法访问我的组件。例如,我无法弄清楚如何为整个初始化类制作统一的 ActionListener。
我也想从textarea获取输入,但是我该怎么做呢?当一切都超出范围时?正如您所看到的,我调用类 SaveToFile,在该类中我想从文本区域获取输入,但我该怎么做?
import javax.swing.*;
public class FunctionsGUI {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
FunctionsGUI window = new FunctionsGUI();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public FunctionsGUI() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize () {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch(Exception e) {
System.out.println("Error setting native LAF: " + e);
}
frame = new JFrame();
frame.setBounds(100, 100, 571, 531);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
SpringLayout springLayout = new SpringLayout();
frame.getContentPane().setLayout(springLayout);
JTextPane textPane = new JTextPane();
springLayout.putConstraint(SpringLayout.NORTH, textPane, 10, SpringLayout.NORTH, frame.getContentPane());
springLayout.putConstraint(SpringLayout.WEST, textPane, 10, SpringLayout.WEST, frame.getContentPane());
springLayout.putConstraint(SpringLayout.SOUTH, textPane, 462, SpringLayout.NORTH, frame.getContentPane());
springLayout.putConstraint(SpringLayout.EAST, textPane, 545, SpringLayout.WEST, frame.getContentPane());
frame.getContentPane().add(textPane);
frame.setLocationRelativeTo(null);
frame.setTitle("Calcolo");
JMenuBar menuBar = new JMenuBar();
frame.setJMenuBar(menuBar);
JMenu mnFile = new JMenu("File");
menuBar.add(mnFile);
final JMenuItem mntmSave = new JMenuItem("Save");
mntmSave.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
SaveToFile sv = new SaveToFile();
}
});
mnFile.add(mntmSave);
JMenu mnOptions = new JMenu("Options");
menuBar.add(mnOptions);
JMenu mnHelp = new JMenu("Help");
menuBar.add(mnHelp);
final JMenuItem AboutMenu = new JMenuItem("About");
AboutMenu.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(AboutMenu)) {
JDialog dialog = new JDialog();
dialog.setTitle("Search Dialog");
dialog.getContentPane().add(new JLabel("Just a test"));
dialog.setSize(300,300);
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setLocationRelativeTo(frame);
dialog.setVisible(true);
if (e.getSource().equals(mntmSave));
SaveToFile sv = new SaveToFile ();
}
}
});
mnHelp.add(AboutMenu);
}
}
最佳答案
WindowsPro Builder plugin for eclipse, and it makes all the JFrame components in a custom initialize () class.
initialize()
只是一个方法而不是一个类。这里的类是 FunctionsGUI
。
This creates a question for me, normally I define my components in the beginning so I can access them publicly trough my program.
这取决于你在做什么,但这可能是一个糟糕的设计。
No I have a second class, but i cant access my components.
实现一个 getter 来返回您需要的组件(甚至所有组件)。
As you can se i call the class SaveToFile, in that class i want to get the input from the textarea, but how would i do this?
例如,您可以将对 JTextField
的引用传递给构造函数中的 SaveToFile
类。
关于java - 如何在自己的私有(private)void中正确使用JFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14379049/