Привет, я хотел бы, чтобы класс мог прослушивать один из его компонентов JButton. Здесь у меня есть класс Window, у которого есть компонент MyPanel, который сам имеет JButtons. Я хотел бы, чтобы окно было уведомлено при нажатии MyPanel JButton. Как я могу это сделать?
Вот фрагмент кода, который можно использовать только для иллюстрации моей потребности, он не был протестирован.
Спасибо за помощь!
class Window extends JFrame {
private MyPanel myPane;
public static void main(String [] args) {
Window mainWindow = new Window();
}
public Window() {
this.myPane = new MyPanel();
getContentPane().add(myPane);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(300, 600);
this.setVisible(true);
}
public computations() {
// Here I would like to get myPane.s, or do other things regarding myPane attributes (with getters), only once myPane.b2 is pressed.
}
}
class MyPanel extends JPanel {
String s;
JButton b1;
JButton b2;
public MyPanel() {
s = new String("");
b1 = new JButton("say hello");
b2 = new JButton("Close");
this.add(b1);
this.add(b2);
ButtonHandler phandler = new ButtonHandler();
b1.addActionListener(phandler);
b2.addActionListener( actionPerformed(ActionEvent e) {
this.s = "Hello world";
});
}
class ButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
// Tells Window class something happened.
}
}
}
У меня есть продвижение по службе, вы можете попробовать, если это сработает. Поскольку я только что настроил свой компьютер и не установил java-среду или IDE, жаль, что я не могу проверить свое решение.
Главное, что вы можете передать экземпляр Window to MyPanel, например, код ниже:
class Window extends JFrame {
public String myStr = "";
private MyPanel myPane;
public static void main(String [] args) {
Window mainWindow = new Window();
}
public Window() {
this.myPane = new MyPanel();
getContentPane().add(myPane);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(300, 600);
this.setVisible(true);
}
public computations() {
// Here I would like to get myPane.s, or do other things regarding myPane attributes (with getters), only once myPane.b2 is pressed.
System.out.println(this.myStr);
}
}
class MyPanel extends JPanel {
Window win = null;
String s;
JButton b1;
JButton b2;
public MyPanel(Window window) {
this.win = window;
s = new String("");
b1 = new JButton("say hello");
b2 = new JButton("Close");
this.add(b1);
this.add(b2);
ButtonHandler phandler = new ButtonHandler();
b1.addActionListener(phandler);
b2.addActionListener( actionPerformed(ActionEvent e) {
this.s = "Hello world";
});
}
class ButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
// Tells Window class something happened.
this.win.myStr = s;
}
}
}