0

それぞれが異なる ActionListener を持つ 4 つの JButton を JToolBar に追加しようとしています。

匿名で参照される JButton に ActionListener を追加する方法があるかどうか、または各ボタンを明確に定義して各リスナーを追加する必要があるかどうか疑問に思っています。

現在、コードは次のようになっています。

JToolBar tools = new JToolBar();
tools.setFloatable(false);
gui.add(tools, BorderLayout.PAGE_START);// adds tools to JPanel gui made in another method

// add buttons to toolbar
tools.add(new JButton("New"));
tools.add(new JButton("Save"));
tools.add(new JButton("Restore"));
tools.addSeparator();
tools.add(new JButton("Quit"));

tools.add(new JButton("foo"));同じ方法で ActionListener を行に追加する方法があるかどうか、Thread t = new FooRunnableClass().start();または各ボタンを定義し、各ボタンに ActionListener を追加してから、各ボタンをツールに追加する必要があるかどうか疑問に思っていました。

4

1 に答える 1