0 Members and 4 Guests are viewing this topic.
From what I got, is a class like a recipe that defines how to do something or how something is made or what it is made of?
Also, I can only have one main function per program, how do I define which function runs first in a class if that class is not the class with the main function?
Also, why is the relation between file names and class names?
Another thing, a function in a class and both have the same name doesn't need a return type, why??
Quote from: Scout on March 08, 2011, 09:56:20 amAnother thing, a function in a class and both have the same name doesn't need a return type, why??That's a constructor, which is a special type of function. It constructs an object when it's created. In other words, whenever you create a new object of that type, it gets run through the constructor which can set up/initiate the object.
public class MainClass { public static void main(String[] args) { MainClass mainClass = new MainClass(); mainClass.MyFunction(); } public void MyFunction() { System.out.println("Hello"); } public MainClass() { System.out.println("How's it going"); }}
How's it goingHello
import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.*;public class MainClass extends JApplet{ public static void main(String[] args) { MainClass mainClass = new MainClass(); mainClass.theGUI(); System.out.println(Math.cos(100)); } public void theGUI() { JFrame myFrame = new JFrame("Calculator"); myFrame.setSize(400,300); JButton enterButton = new JButton("Enter"); enterButton.addActionListener(new DisplayMessage()); myFrame.getContentPane().add(enterButton); myFrame.setVisible(true); } private class DisplayMessage implements ActionListener { public void actionPerformed(ActionEvent e) { System.out.println("You just pressed a button"); } } public MainClass() { System.out.println("This is the constructor class being ran."); }}
Quote from: Deep Thought on March 08, 2011, 10:40:06 amQuote from: Scout on March 08, 2011, 09:56:20 amAnother thing, a function in a class and both have the same name doesn't need a return type, why??That's a constructor, which is a special type of function. It constructs an object when it's created. In other words, whenever you create a new object of that type, it gets run through the constructor which can set up/initiate the object.This is the one that I still don't get.So when I make MainClass mainClass = new MainClass(); will it run through the constructor (if it exists?).
A constructor is ran when its classed is called as an object??
Car myFirstCar = new Car("Ford", "Fusion");Car mySecondCar = new Car("Mercury", "Milan");Car myThirdCar = new Car("Lincoln", "Navigator");
myFirstCar.drive();mySecondCar.drive(); // Not going to crash my brand-new Lincoln :P
Exception in thread "main" java.lang.NullPointerException at java.awt.Component.setLocation(Component.java:1997) at MainClass.createGUI(MainClass.java:21) at MainClass.main(MainClass.java:12)
import javax.swing.*;import java.awt.*;public class MainClass extends JApplet { JFrame mainWindow = new JFrame("Axe Sprite Editor"); public static void main(String[] args) { MainClass mainClass = new MainClass(); mainClass.createGUI(); } public MainClass() { } public void createGUI() { mainWindow.setSize(640,640); mainWindow.setLocation(null); mainWindow.setResizable(false); JPanel pixel1 = new JPanel(); mainWindow.add(pixel1); mainWindow.setVisible(true); }}
mainWindow.setLocation(x, y);ormainWindow.setLocation(new Point(x, y));
import javax.swing.*;import java.awt.*;public class MainClass extends JApplet { JFrame mainWindow = new JFrame("Axe Sprite Editor"); public static void main(String[] args) { MainClass mainClass = new MainClass(); mainClass.createGUI(); } public MainClass() { mainWindow.setSize(640,640); mainWindow.setResizable(false); JFrame pixel1 = new JFrame(); mainWindow.add(pixel1); } public void createGUI() { mainWindow.setVisible(true); }}
Exception in thread "main" java.lang.IllegalArgumentException: adding a window to a container at java.awt.Container.checkNotAWindow(Container.java:431) at java.awt.Container.addImpl(Container.java:1039) at java.awt.Container.add(Container.java:959) at javax.swing.JApplet.addImpl(JApplet.java:300) at java.awt.Container.add(Container.java:365) at MainClass.<init>(MainClass.java:20) at MainClass.main(MainClass.java:12)
Oh damn, I just noticed, I made it JFrame, I meant JPanel. Sorry and thanks for the setLocationRelativeTo thing
Now from now on, I'm going to have problems in the layout of the window.I wanna make a 640*640 window with 64 panels of 80*80 as in a grid.It's a 8*8 grid, each square panel is 80pixels*80pixels, althogether makes a 640*640 pixels window.Any suggestions/ideas/layouts/code on how to do this?