因此,基本上,我在这里有点困惑,我使用WindowsPro Builder插件进行蚀,它使所有JFrame组件都位于自定义的initialize()类中。这给我提出了一个问题,通常我一开始就定义了我的组件,这样我就可以通过我的程序公开访问它们。不,我有第二堂课,但是我无法访问我的组件。例如,我无法弄清楚如何为整个初始化类创建统一的ActionListener。

我也想从textarea获取输入,但是我该怎么做呢?当一切都超出范围时?如您所知,我将类称为SaveToFile,在该类中,我想从textarea中获取输入,但是我该怎么做?

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

}
}

最佳答案

我想告诉你一种方法。在SaveToFile类中有一个可以接受String的构造函数,如下所示。将文本窗格中的文本作为String传递给此构造函数或方法。甚至一种方法都是好的。但是,请同时使用这两个。

public class SaveToFile {

    public SaveToFile(String textinput) {
        System.out.println(textinput);
    }

    // if you prefer to have a differrent method do like below.

    public void doSomething(String textinput) {
            System.out.println(textinput);
    }
}


现在将您的听众更改为如下所示,

final JMenuItem mntmSave = new JMenuItem("Save");
    mntmSave.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            SaveToFile sv = new SaveToFile(textPane.getText());
        }
});


如果您不想要构造函数,

final JMenuItem mntmSave = new JMenuItem("Save");
        mntmSave.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                  SaveToFile sv = new SaveToFile();
                  sv.doSomething(textPane.getText());
            }
});


我正在做的是将文本从文本面板发送到SaveToFile类。在那里您可以使用此字符串。确保将JTextPane声明为final,如下所示

final JTextPane textPane = new JTextPane();


现在,您从textpane中获得了文本,该文本位于SaveToFile类的另一个类中。正如我所说的,它只是“一种方式而不是这种方式”。

10-07 20:53