私は本からオブジェクト指向プログラミングを学んでいます。私はかなり遠くになっていますが、今私は立ち往生しています。 wExec
は再実行されますか?これが可能なのかどうかはわかりません。誰かが私のためにこれをクリアすることはできますか? 「完了」ボタンを押して参照オブジェクトの実行を終了すると、私は混乱します。ここで各 クラスのコードは次のとおりイベントオブジェクトはどうなりますか?コードを再実行していますか?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class RunApp {
public static void main(String[] args) {
MainApp app = new MainApp("hello world");
app.showApp();
}
}
class MainApp extends MmvcApp {
WmvcButton btnDone;
WmvcButton btnExit;
public MainApp(String title) {
super(title);
btnDone = new WmvcButton("Done", new WmvcExecutor() {
public void execute(ActionEvent event) {
System.out.println("done");
System.out.println("test 2");
}
});
btnExit = new WmvcButton("Exit", btnDone.getExecutor());
}
}
abstract class MmvcApp {
private static JFrame frame;
private static JPanel panel;
private static MmvcApp singletonApp = null;
public MmvcApp(String frameText) {
singletonApp = this;
initializeApp(frameText);
}
public void initializeApp(String frameText) {
frame = new JFrame(frameText);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel();
panel.setPreferredSize(new Dimension(400, 300));
frame.add(panel, BorderLayout.CENTER);
}
public static JFrame getFrame() {
return frame;
}
public static JPanel getPanel() {
return panel;
}
public static MmvcApp getApp() {
return singletonApp;
}
public void showApp() {
frame.pack();
frame.setVisible(true);
}
}
class WmvcButton extends WmvcController {
private JButton myButton;
public WmvcButton(String compText, WmvcExecutor wExec) {
super((JComponent) new JButton(), wExec);
myButton = (JButton) myComponent;
myButton.setText(compText);
myButton.addActionListener(this);
MmvcApp.getPanel().add(myButton);
}
}
class WmvcController implements ActionListener {
public JComponent myComponent;
private WmvcExecutor wExec;
public WmvcController(JComponent component, WmvcExecutor wExec) {
myComponent = component;
this.wExec = wExec;
}
public WmvcExecutor getExecutor() {
return wExec;
}
public void actionPerformed(ActionEvent event) {
if (wExec != null) {
wExec.execute(event);
System.out.println("test 1");
} else
System.out.println("not working ");
}
}
class WmvcExecutor {
public void execute(ActionEvent event) {
}
}
申し訳ありません - あなたは何を正確に混乱させましたか?あなたの正確な質問は何ですか? –
さて、あなたは[Java命名規則](http://ja.wikipedia.org/wiki/Naming_convention_(programming)#Java)を学び、使いたいと思うでしょう。変数名はすべて小文字で始まり、クラス名は大文字で始める必要があります。これを学んでこれに従うことで、コードをよりよく理解でき、他者のコードをよりよく理解できるようになります。 –
'new wmvcExecutor(){...}'は、新しい種類の** class **を定義します(具体的には、[Anonymous Class](https://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses .html))。 –