0

したがって、基本的に私はここで少し混乱しています.Eclipse用のWindowsPro Builderプラグインを使用しており、すべてのJFrameコンポーネントをカスタムのinitialize()クラスに作成しています. これは私に疑問を投げかけます。通常、最初にコンポーネントを定義して、プログラムを通じてパブリックにアクセスできるようにします。いいえ、2 番目のクラスがありますが、コンポーネントにアクセスできません。たとえば、初期化クラス全体に対して統一された ActionListener を作成する方法がわかりません。

また、テキストエリアから入力を取得したいのですが、どうすればよいですか? すべてが範囲外の場合は? ご覧のとおり、クラス 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);

}
}
4

2 に答える 2

4

Eclipse 用の WindowsPro Builder プラグインであり、カスタムの initialize () クラスですべての JFrame コンポーネントを作成します。

initialize()クラスではなく単なるメソッドです。ここのクラスはFunctionsGUI.

これは私に疑問を投げかけます。通常、最初にコンポーネントを定義して、プログラムを通じてパブリックにアクセスできるようにします。

何をしているかにもよりますが、これは悪い設計である可能性があります。

いいえ、2 番目のクラスがありますが、コンポーネントにアクセスできません。

必要なコンポーネント (またはすべてのコンポーネント) を返すゲッターを実装します。

ご覧のとおり、クラス SaveToFile を呼び出します。そのクラスでは、テキストエリアから入力を取得したいのですが、どうすればよいでしょうか?

たとえば、コンストラクターでJTextFieldtoSaveToFileクラスへの参照を渡すことができます。

于 2013-01-17T12:33:24.767 に答える
2

方法を示したいと思います。以下のように String を受け入れることができる SaveToFile クラスにコンストラクターを用意します。textpane からのテキストをこのコンストラクターまたはメソッドに String として渡します。または方法さえも良いです。しかし、これら2つのいずれかを持っています。

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

私がやっていることは、textpanel から SaveToFile クラスにテキストを送信していることです。そこで、この文字列を使用できます。以下のように JTextPane を final として宣言してください。

final JTextPane textPane = new JTextPane();

これで、別のクラスにある textpane から SaveToFile クラスへのテキストを取得しました。私が言ったように、それは「方法ではない方法」です。

于 2013-01-17T12:52:54.243 に答える