I'm trying to learn the ins and outs of Classes and especially Objects, but I'm having trouble accessing the variables between classes.
So far, I've got the main() starting the ball rolling by calling the startProgram() method of the Program class.
In the Program class, I have it creating objects to build the UI:
package control; import ui.*; import java.awt.*; import javax.swing.*; public class Program { public static void startProgram() { Frame window = new JFrame("CoordinateCalculator"); DisplayPanel displayPanel = new DisplayPanel(); NavPanel navPanel = new NavPanel(); NumPanel numPanel = new NumPanel(); KeyEntry keyEntry = new KeyEntry(); MouseEntry mouseEntry = new MouseEntry(); window.setLayout(new BorderLayout(5,10)); window.add(displayPanel.panel, BorderLayout.NORTH); window.add(navPanel.panel,BorderLayout.CENTER); window.add(numPanel.panel, BorderLayout.SOUTH); window.pack(); window.setLocation(250,250); window.setResizable(false); window.setVisible(true); } }
The class used to build the displayPanel object:
package ui; import java.awt.*; import java.text.NumberFormat; import javax.swing.*; public class DisplayPanel { public JPanel panel = new JPanel(); public JLabel display = new JLabel(); public JFormattedTextField object1a = new JFormattedTextField(NumberFormat.getInstance()); public JFormattedTextField object1b = new JFormattedTextField(NumberFormat.getInstance()); public JFormattedTextField object1c = new JFormattedTextField(NumberFormat.getInstance()); public JFormattedTextField object2a = new JFormattedTextField(NumberFormat.getInstance()); public JFormattedTextField object2b = new JFormattedTextField(NumberFormat.getInstance()); public JFormattedTextField object2c = new JFormattedTextField(NumberFormat.getInstance()); public DisplayPanel() { panel.setLayout(new GridLayout(3,3)); panel.add(object1a); panel.add(object1b); panel.add(object1c); panel.add(object2a); panel.add(object2b); panel.add(object2c); panel.add(display); } }
The next part I'm having trouble with. I have a DataHandling() class, which holds the method doMath() where the magic happens:
package math; import ui.*; import java.awt.*; import javax.swing.*; public class DataHandling { public static void doMath() { double object1A = Double.parseDouble(displayPanel.object1a.getText()); double object1B = Double.parseDouble(displayPanel.object1b.getText()); double object1C = Double.parseDouble(displayPanel.object1c.getText()); double object2A = Double.parseDouble(displayPanel.object2a.getText()); double object2B = Double.parseDouble(displayPanel.object2b.getText()); double object2C = Double.parseDouble(displayPanel.object2c.getText()); double objectA = object1A - object2A; double objectB = object1B - object2B; double objectC = object1C - object2C; if (objectA < 0) { objectA *= -1; } if (objectB < 0) { objectB *= -1; } if (objectC < 0) { objectC *= -1; } String i = Double.toString(Math.sqrt((objectA * objectA) + (objectB * objectB) + (objectC * objectC))); displayPanel.display.setText(i); } }
I'm getting the error 'displayPanel cannot be resolved', since displayPanel is the variable referencing the DisplayPanel() object, I thought that I could use 'displayPanel.object1a' to access that item (for instance), but I can only get that to work is from in the Program() class... So, did I set something up wrong? Am I using the wrong syntax to access the data in question? What am I obviously missing?
Thank you kindly,
Chris