Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActionListener between two classes

import javax.swing.*;

class Labels extends JFrame{ JPanel pnl = new JPanel();

ImageIcon duke = new ImageIcon("duke.png");

JLabel lbl1 = new JLabel(duke);
JLabel lbl2 = new JLabel("Duke is the friendly mascot of Java technology.");
JLabel lbl3 = new JLabel ("Duke", duke, JLabel.CENTER);

public Labels(){

    super("Swing Labels");
    setSize(1000 , 800);
    setDefaultCloseOperation( EXIT_ON_CLOSE);
    add(pnl);
    setVisible(true);

    lbl1.setToolTipText("Duke - the Java Mascot");

    lbl3.setHorizontalTextPosition(JLabel.CENTER);
    lbl3.setVerticalTextPosition(JLabel.BOTTOM);

    pnl.add(lbl1);
    pnl.add(lbl2);
    pnl.add(lbl3);

}
    public static void main(String [] args){
        Labels gui = new Labels();
    }

}

What if I want to use this as a JApplet? what has to be done? Is it hard to change?

The things that run on a JFrame are the same as the ones on a JApplet?

like image 898
MBC870 Avatar asked Dec 27 '22 22:12

MBC870


2 Answers

As I recommended in one of your previous questions, you should re-organize your program to conform to the Model-View-Controller pattern, or one of its variants, as this will allow you to cleanly separate out the control code (that called by the ActionListener) from the GUI. For an example of an M-V-C program, please check out my suggestions and code in this recent answer.

like image 99
Hovercraft Full Of Eels Avatar answered Jan 06 '23 23:01

Hovercraft Full Of Eels


Well okay first check this out: action listener in another class - java

now all you have to do is edit it to your needs, and in the actionPerformed(ActionEvent ae) you will then get the values in controlB.java class by accessing the data using a accessor/get method or make the Actionlistener class extend the ControlB so it will have access to all its public data, then once you have the values, pass them to the Graph.java either during initiation or using a mutator/set method?

like image 25
David Kroukamp Avatar answered Jan 07 '23 01:01

David Kroukamp