As the University project, I have to make a simple program in Java and I am currently using the latest version of Netbeans IDE 8.0.2.
I know that main class is where our program will begin and when I am creating classes, I make a mistake but I do not know where..
My program mainly consists of 3 classes and User interface.
ATM - ATMCard - Account
my questions,
ATM should the main class and the GUI. How can I create such class in Netbeans? I mean, as the options, there are Java Main class or other forms.
Should GUI be separated from main class ? I mean, in total, should there be 4 classes (GUI - ATM - ATMCard - Account)
I can guess that these are very simple questions but it is important since I will build all the program on the structure, which is shaped by your answers..
Thank you in advance..
In netbeans i normaly create new Java project.
(File -> New project -> Java -> Java Application)
And select the project's name. There is an option where you can selec option create main class.
Next i normaly create two packages. For instance in your case it could be:
atm.gui (for gui) package and atm.bl package (for buisness logic).
Then select gui package and left click on it, select new JFrame form (AtmForm or something).
And in jour main class just call:
AtmForm form = new AtmForm();
form.setVisible(true);
So the structure should look like this:
Related
I'm working on a GUI in Window Builder and I want to import some of the objects I have declared on my main. The GUI is in the same package as my main and 5 classes, 1 parent and 4 child classes. Is this possible to do? As of right now, I'm trying to call my characters for the game I created into the GUI to be displayed when a certain button is pushed.
So far, I tried importing each class into my GUI. That didn't work went into my main and added my GUI by declaring it as a new object followed up by adding EventQueue.invokeLater and making that visible, but that didn't really work. Any thoughts on how to do something like this?
On Window builder Palette, click choose component and search for your class for example
package.class. This is the image Refer to this.
I teach very early coders using Eclipse, so it's imperative that I minimize distractions for them. I have all my preferences correct in Eclipse except for one: when I select "New Class", the name of the current Java Project autofills into the package field. See the image below. This is a problem because if they don't delete that content (every time!) it automatically creates a line
package my_package_name;
at the beginning of their new classes. While I know this isn't a huge deal, I worry that it's just one more thing for them to worry about ("Is that why my code is wrong?"). Is there any way to change the default content of the "Package" field in the New Class window to be blank (so it wouldn't create the package line in their source code files)?
I am new to Netbeans and trying to piece together my application by using code from different sources. I have a query. In one of my projects there are two different files with green arrows against them - I think the green arrow indicates the presence of a main routine. I want to prioritise which file has the main routine and wanted to know the easiest way to do this. Would this simply be a case of removing the line public static void main(String args[]) in one of my projects - If not how do I do it. I'm using Netbeans 7.4
Every file with a main method will have the "green arrow". Normally, a project should only have one main method, the launching point of the program.
You can't change the green arrow in the context your speaking of, but you can change which class is the launching class (Main Class).
right click your project.
from the context menu select properties
from the dialog choose run on the left.
where it says Main Class select the class you want as the launching point class.
I'm trying to make a hello world form in Intellij. I've created the form, but the question now is what code to make in main() to make the form run and show up?
PS: all the tutorials around seem to only focus on "how to do forms on intellij" not in "how to actually make it run, then".
Thanks
Go to the class with the same name as the form.
Press the keyboard shortcut for "Generate". It's Ctrl+N on Mac OS X, Alt+Ins on Windows. Alternatively, from the menu, select menu Code → Generate.
Select "Form main()".
Now the main method is written and inserted for you. It will look something like this:
public static void main(String[] args) {
JFrame frame = new JFrame("MyForm");
frame.setContentPane(new MyForm().mainPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
I just did my first Intellij Swing App.
Steve McLeod has the right instructions, however, when I tried to generate the main method using Alt+Insert => Generate main, I received an error message about one of my panels not being bound. So I clicked on the gui designer page (.form), selected my top panel, and gave it a name.
Everything else was named for me, but for some reason, the panel name was blank. Once I did that, I was able to switch over to the form .java class, press "Alt+Insert" and generate a constructor (not required, but needed).
From there, I followed Steve's advice to generate a main method. One thing that threw me off was the expectation that my Intellij generated .java class would extend or implement something swing related - it didn't. Swing only shows up in the Intellij generated main method (besides the private variables).
Check this tut while it is realy step-by-step:
JetBrains JavaFX HelloWorld
I had created a GUI in Netbeans through Netbeans Swing GUI creator. So I just dragged and dropped the Swing Components from the "palette" window and all the code was generated by netbeans.
Now the code is too long to maintain (approx. 10,000 lines). So some experts on SO suggested me to refactor my code.
I can refactor the code that was generated by me but I don't know how to refactor the code generated by the Netbeans as It doesn't allow editing in its generated code.
Any suggestions?
10.000 lines of code sounds like you have everything in that single class.
Start by splitting your source into Model, View and Control (MVC).
You might also be able to extract some JPanels into separate classes. One way to do this is to create a new JPanel (new file), and cut/paste your compoments from one main panel into that new JPanel. Save and compile your new panel.
Then go back to your main frame, select Beans -> Choose Bean from your Palette and choose the newly created class (com.example.YourPanel for example).
Make sure to have a backup of your application before you try this.
Well - if the code is generated, I don't see any advantages in refactoring it as long as the tool which generated it can handle it. The tool (meaning the designer in this case) will "destroy" all your refactoring work as soon as it updates the code.
However, you should split your Control/Window/... into multiple controls - then the code will automatically get shorter and you will be able to maintain your UI more easily.
As a conclusion: Do not refactor the generated code but do refactor your control.
Handcode the GUI code with layoutmanagers.
Using GUI builder tools, makes it nearly impossible to refactor GUI code. I have to use these idiotic Intellij Swing GUI designer forms. I now cannot even rename my packages in Eclipse because it wont be updated in the forms.XML file.
Stay away from GUI builders. If you want to build really complex, maintainable GUIs then do it by hand by using GridBagLayout and all the rest.
If you have to use netbeans, because of project limitations (e.g the rest of the team is, or requirements say to) then use Matisse to break up the huge form into smaller panels, each of which the designer can edit. You can do that by creating a new form, and cutting and pasting panels from the big form into the new form.
But at the same time, make sure all the business logic is moved out of the UI classes.
If you do not have to use matisse / netbeans, you can open the project in Eclipse, and edit the forms using WindowBuilder, it will do it in real java code instead of the uneditable form, so you can then chop and edit it to your heart's content.
You can extract the application logic into a separate subclass. Then, directly use the subclass. I succeeded with the following method.
Members defined by us that are relevant to the application logic moved to the newly created subclass.
Components access modifier made "protected" (they are "private" by
default). To do so: Right click -> Properties -> Code (tab) -> Set
"Variable modifier" to "protected"
Event handling methods moved to the subclass - When you are adding events to a component using properties pane it changes initComponents() function by adding the relevant code like in the following code sample. Here definition of btnNum6ActionPerformed() is added to the class with an empty body. Unfortunately btnNum6ActionPerformed() is private and no way to change the access modifier using NetBeans IDE. Hence, they cannot be overridden. To get rid of this, you can define another intermediary function and call it inside btnNum6ActionPerformed(). It is better to make the base class and its intermediary event handling functions abstract.
btnNum6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnNum6ActionPerformed(evt);//Definition of this method is added too
}
});