I've just started to learn Java Graphical Programming. And I have a question. Where should I put the Swing APIs (such at setSize, setLocation, setTitle, etc)?
In the main method or in the constructor? Some tutorials put them inside the constructor of the class that extends the JFrame, but some others put them inside the main method. Is there any difference between the two?
Related
I am new to stackoverflow and am sorry, if this question was already asked, but when I searched for one containing my problem, I could not find one.
Here is my question:
In Java I am creating a game (just for fun and am learning to code). You start my game with a launcher to log in. However, if you do not have an account, you can register. What I like more is to use one frame. I have a panel containing the launcher elements and one containing the register elements. I am trying to learn how to code, if another one is working on my project, he does not necessarily need to look all over the code to do some work. In other words, I am using packages to sort classes. Example: Classes for the launcher are in package Launcher. Classes for register are in package register.
In my code, I have a class called Display extending JFrame. Display is instanciated in the class with the main method. Display has 2 Methods for removing a panel and adding a panel, requiring you to pass a JPanel, if you use any of both methods. After Display is instantiated, it instantiates a jpanel inside the constructor. This has all the components to Display. The event listeners are in another class. So I am passing the buttons and Display to that class, because here is the eventlistener code for the button register. In my register class I have defined and instantiated all necessary components. I passed the display through all of the classes until it reached the register class which extends jpanel. I even passed the Launcher class. Now i can use the methods in display to remove the launcher panel and add my register panel.
My Question:
is this good code or overkill?
I never instantiated Display after the main method again. I always declared it and set it to the passed display. I did this, because instantiated a new Display would mean unnecessary use of memory and passing objects in java is actually a passing by references, meaning it is not passing the object but a pointer to the object. So this would mean less memory usage.
So I've made a game in Java, mostly for practice. It works in the command line now, but now I want to implement it in a GUI with Swing. I got some pictures etc. to implement, but that's not relevant now. I also want to implement a mouselistener.
My (basic) question is: how should I do the layout of the classes? I got my game main class, and some subclasses. Should I create a seperate class for the GUI, or should I just implement it in my game main class? Also, where does the mouse listener go?
Sorry for the newbie question, I'm a beginner with GUI's and I want to do it right.
You might try to use a Model View Presenter pattern, as described here:
I've been taught not to place most methods in a general "System" class but where do they go instead?
When adding new class extending JFrame (or java.awt.Frame) the class is added with main() method inside like this:
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewFrame5().setVisible(true);
}
});
}
Every JFrame class has its own main method and I guess all classes starts simultaneously.
How do I add frames without main methods?
Firstly, see The Use of Multiple JFrames, Good/Bad Practice?. Generally, recommended not to do it. See accepted answer for other possibilities (for example a JDialog)
As for your main concern, there's no way around netbeans creating the main method for top level containers like JFrame and JDialog. The logic seems right in the case of JFrame, as an application should only have one JFrame as the main top-level container for the application, but I'm not sure the logic behind the JDialog having a main method (as the dialog is usually run in the same JVM as the main JFrame). The only thing I can think is that the JDialog is created with a main for development purpopsed, if you want to test the dialog in stand-alone mode. But ultimately, you should delete the main method of the JDialog should you choose to use one.
Going back to the first point about multiple JFrames, other options I might recommend
Use a JDialog. Yes you will have to delete the main method, when going into production, as the dialog will be instantiated within the context of the main JVM and generally shouldn't run its own process.
Another option, depending on your requirements is to use a Cardlayout which will let you switch between views/panels (You can create JPanel forms in netbeans). See How to Use CardLayout in Netbeans GUI Builder for a working guide. And the official How to use CardLayout tutorial
An aside, if you are a beginner, I strongly suggest you put aside the builder tool and learn to hand code first. There may be many tutorials teaching you how to use the builder tool, but they may miss out on important concept in the swing architecture and swing in general. IMO this will greatly affect your understanding of how and why things work with the builder, causing a lot a headache when trying to debug. Keep Creating a GUI With JFC/Swing, the official tutorial handy and go through it.
I did a GUI by my own which extends a JFrame. And I saw a program code from advanced programmer who imports the JFrame. I might know the difference between import and extend. But what advantage is given by which sort of implementation?
By "import" i presume you mean that your programmer friend's class uses a JFrame whereas your class is a JFrame. I don't think extending a JFrame is "wrong", but my preferred approach is to create and configure a JFrame rather than extending it. If there's really some protected method that you need access to then you might extend it but i'm sure that, in most cases, simply creating and configuring a standard JFrame is right - i have never found a need to extend it.
In other words, by inheriting JFrame with no real need to do that, you're just complicating your system. This is a same argument you would have when choosing inheritance versus aggregation with any other class. To find out what's right, you need to ask yourself if the class you're writing really is a JFrame which is a window widget. I suspect that in most cases you are writing a Swing application which needs a JFrame but which isn't one itself - it's a Swing application with some other purpose.
I have created a MainJFrame.java in the package myproject using New project->java->java aplication and NewJIF.java in the same package.
When a JButton clicked in MainJFrame.java i want JInterFrame to open using
new NewJIF().setVisible(true); inside actionPerformed() method.
But this is not working ...and caught some people saying this is impossible in netbeans to call another java class using swing framework...
I'm sure it is possible. You can add any code you want to an ActionListener. So there is no reason you can't do this.
I suggest you read the section from the Swing tutorial on How to Use Internal Frames. Understand how the code in the ActionListener works there and then incorporate the concepts and code in to your application.