Reputation: 19
Hello I'm facing this problem:
I created a button in class Luncher, when I click it I want to call a function in another class (Main) but I dont have a result. However when i define (Main) class in run mode I get the intended result. What is the problem ?
This work for me : When i set Main.class in run mode
And this not work :/ : When i click in button to show Main.class
This is the code for the runable class (Luncher):
public class Luncher extends javax.swing.JFrame {
public Luncher() {
setSize(600,400);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
JButton jButton1 = new javax.swing.JButton();
jButton1.setText("CLIC ON ME !");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
Main example = new Main();
example.doThis();
}
});
GroupLayout layout = new GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(147, 147, 147)
.addComponent(jButton1)
.addContainerGap(180, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(108, 108, 108)
.addComponent(jButton1)
.addContainerGap(169, Short.MAX_VALUE))
);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Luncher().setVisible(true);
}
});
} }
And the second class is Main:
public class Main extends javax.swing.JFrame {
private static GUI gameGui = new GUI(); //GUI its a JFrame class
private static CardLayout card = new CardLayout();
private static JPanel content = new JPanel();
public Main(){
doThis();
}
public static void doThis (){
content.setLayout(card);
gameGui.setVisible(true);
gameGui.add(content);
gameGui.repaint();
gameGui.revalidate();
card.show(content);
}
public static void main (String [] args) {
} }
Upvotes: 0
Views: 65
Reputation: 347244
You have a few basic solutions available to you.
Pass a reference of Main
to Luncher
. This way, when you want to, you can just call the functionality of Main
you need to.
This is not the best solution, as it couples Launcher
to Main
, making it difficult to re-use the code and possibly exposing functionality (of Main
) to Launcher
, which Launcher
should have access to.
You could use a delegate or observer pattern...
Define an interface
(or contract) which Launcher
requires delegates to implement in order to perform the functionality it requires.
public interface LauncherDelegate {
//...
}
You would then have Main
implement this interface
public class Main extends javax.swing.JFrame implements LauncherDelegate {
//...
and pass a reference of itself to Launcher
public class Luncher extends javax.swing.JFrame {
private LauncherDelegate delegate;
public Luncher(LauncherDelegate delegate) {
this.delegate = delegate
//...
This way, Launcher
can only call the functionality described in the contract, it decouples the code, as any implementation of LauncherDelegate
can be used and Launcher
won't care, making the code more re-usable and configurable.
Upvotes: 1