愚かな質問かもしれませんが、画像に示すように「メインフレームクラス」の構文を理解できませんでした。 Here, the setStringListener method is called from toolbar reference. But the syntax used inside the circular bracket is quite weird to me. I usually have parameters like strings or reference inside the call method. I did not understand how JButton will call this method. This programms works fine. I jst dont understand that partソースコード全体が以下に入力されています。あなたは、本質的にあなたの3210メソッド内new
インターフェースを作成しているのでメソッドとJButtonの呼び出しについて
public class App {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
new MainFrame();
}
});
}}
public class MainFrame extends JFrame{
private TextPanel textPanel;
private Toolbar toolbar;
public MainFrame(){
super("Hello World");
setLayout(new BorderLayout());
toolbar =new Toolbar();
textPanel=new TextPanel();
toolbar.setStringListener(new StringListener(){
public void textEmitted(String text){
textPanel.appendText(text);
}
});
add(toolbar,BorderLayout.NORTH);
add(textPanel,BorderLayout.CENTER);
setSize(600, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}}
public class TextPanel extends JPanel {
private JTextArea textArea;
public TextPanel(){
textArea=new JTextArea();
setLayout(new BorderLayout());
add(new JScrollPane(textArea),BorderLayout.CENTER);
}
public void appendText(String text){
textArea.append(text);
}}
public class Toolbar extends JPanel implements ActionListener {
private JButton helloButton;
private JButton goodbyeButton;
private StringListener textListener;
public Toolbar(){
helloButton=new JButton("Hello");
goodbyeButton=new JButton("Goodbye");
helloButton.addActionListener(this);
goodbyeButton.addActionListener(this);
setLayout(new FlowLayout(FlowLayout.LEFT));
add(helloButton);
add(goodbyeButton);
}
public void setStringListener(StringListener listener){
this.textListener=listener;
}
public void actionPerformed(ActionEvent e) {
JButton clicked=(JButton)e.getSource();
if (clicked==helloButton){
if (textListener!=null){
textListener.textEmitted("Hello\n");
}
}
else if(clicked==goodbyeButton){
if (textListener!=null){
textListener.textEmitted("goodBye\n");
}
}
}}
、インターフェイスがStringListener
public interface StringListener {
public void textEmitted(String text);
}
https://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html#syntax-of-anonymous-classes –