Java – making mouse buttons can trigger class events
•
Java
I'm trying to make a program with buttons. When a button is clicked, it will call a class to work I've only been using java for about 10 weeks now. I've got the basics, but I can't find anything to understand what I want to do here
I tried
public void mouseEntered(MouseEvent e) {
if (e.getButton()== MouseEvent.BUTTON3){
Object triangle;
Frame.class.getClass();
}
}
I tried, too
panel.addMouseListener(new MouseAdapter() {
if (e.getButton()== MouseEvent.BUTTON1) {
Frame.class.getClass(circle); }
Either way, I've tried it. I usually get an error that I can't find the object, or the method getClass () in the object type is not applicable to the parameter (JButton)
Anyone can ask me to help me figure out what I did wrong?
thank you.
Solution
public class MainFrame extends JFrame {
public class MainFrame extends JFrame {
private JButton button = new JButton("Run AnotherClass");
MainFrame() {
super();
this.setTitle("Demo App");
this.setSize(200,200);
this.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new FlowLayout());
this.add(button);
button.addActionListener(new ButtonHandler());
}
public class ButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
new AnotherClass();
}
}
public static void main(String[] args) {
new MainFrame().setVisible(true);
}
}
public class AnotherClass {
public AnotherClass() {
JOptionPane.showMessageDialog(null,"AnotherClass is in operation");
}
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
