I'm not sure I understand your problem but the starting point of each application is the main method. From there, you can create new objects and call methods. For example, to create a basic window in swing, you could do:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| import javax.swing.JFrame;
public class Example extends JFrame {
public Example() { this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setTitle("Title of the window"); this.setSize(800, 600); this.setVisible(true); } public static void main(String[] args) { new Example(); } } |
The main method creates a new Example object, which is a JFrame. This code would be in Example.java. Each .java file holds one class so if you make several classes, you'd have several .java files. I'd search for some Swing tutorials on event handling, they should describe how to handle events with event listeners and such.
For example, to respond to an event when the user pressed a key, you can create a key listener (in ExampleKeyListener.java):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| import java.awt.event.KeyEvent; import java.awt.event.KeyListener;
public class ExampleKeyListener implements KeyListener {
public void keyPressed(KeyEvent e) { System.out.println("You pressed: " + e.getKeyChar()); }
public void keyReleased(KeyEvent e) { }
public void keyTyped(KeyEvent e) { }
} |
which can be added to the window:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| import javax.swing.JFrame;
public class Example extends JFrame {
public Example() { this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.addKeyListener(new ExampleKeyListener()); this.setTitle("Title of the window"); this.setSize(800, 600); this.setVisible(true); } public static void main(String[] args) { new Example(); } } |
In this case there are two .java files: the Example.java file which holds the code for starting the program (main method) and the code for the window, and ExampleKeyListener.java which holds the code that happens when the user presses a key. The same idea can be used for other events, like when the user moves the mouse or presses a button.
Basically, you can create an object when you need it and then call some methods or give it as a parameter. In this example, I created a new ExampleKeyListener object and I gave that as a parameter to addKeyListener. So, in your calculator, you'd have some buttons on the screen and each button could listen for a button press and then update the internal state of the program, like adding a number or so or displaying the result of the calculation.