Reputation: 4807
I have little experience with Java WindowBuilder. Can you please help me? I with to put all GUI in second class but use what I need in my main class. What I did does not print anything.
Main Class:
import java.awt.EventQueue;
public class MainClass {
static GUIClass gui;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
gui = new GUIClass();
gui.getFrame().setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public MainClass() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
gui.btnNewButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Test"); //Not printing
}
});
}
}
GUI class:
import java.awt.EventQueue;
public class GUIClass {
private JFrame frame;
public JButton btnNewButton;
/**
* Launch the application.
*/
/**
* Create the application.
*/
public GUIClass() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
btnNewButton = new JButton("New button");
btnNewButton.setBounds(170, 107, 89, 23);
frame.getContentPane().add(btnNewButton);
}
public JButton getBtnNewButton() {
return btnNewButton;
}
public JFrame getFrame() {
return frame;
}
}
Upvotes: 0
Views: 55
Reputation: 1306
The problem is that you never created the Main Class.
The main method has nothing to do with the MainClass. The main method must create the MainClass.
Try this
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
gui = new GUIClass();
gui.getFrame().setVisible(true);
new MainClass();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
Upvotes: 1