what is Single Frame Application and what is FrameView? - java

I have a basic understanding of Java Swing, I know about JFrame and JPanel. the problem is : Yesterday I started to write a Simple Desktop Application in Netbeans, I created a SingleFrameApplication, Netbeans created few classes for me one of them that contained main method was public class ShamsApp extends SingleFrameApplication and the other which was a view class public class ShamsView extends FrameView. Everything was fine until I tried to add a new JPanel to my application, at that point i found out that I can't add this JPanel to any of these classes, because none of them is a JFrame instance , so the questions raised, what are FrameView and SingleFrameApplication anyway? are they standard Swing JComponents? is it appropriate to ask questions like JPanel Vs FrameView? Or it is nonsense? Would you please enlighten me and future googlers?

SingleFrameApplication is a part of the swing application framework, you can find all you want to know about it here if you want to play with the JFrames in FrameView, you can use the getter and setter

Related

Basic JPanel/JFrame class problem in Java

I started not so long ago learning Java through tutorials and videos, and after understanding a few things (how buttons, layouts, audio, and a few other things work) one of my goal now is to create a little interactive game.
I wrote a pretty big part of the game in the Main Class and it was working good, but it got messy after a while.
So I decided to try another time from the beginning using different classes for every part of the game to make the code look more clear and understandable.
But I have a problem from the very beginning and after a few hours of searching tutorials, answers on forums and not finding a precise answer, I think it's the best if you will see exactly my problem (which is very simple!)
-So I just constructed the JFrame in a class (I use the main Class only to launch the frame, and it works fine) :
import javax.swing.*;
import java.awt.*;
public class principalFrame {
public principalFrame(){
JFrame mainFrame = new JFrame();
mainFrame.setVisible(true);
mainFrame.setSize(1200,750);
mainFrame.getContentPane().setBackground(Color.BLACK);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setResizable(false);
}
}
and I created a JPanel in another class :
import javax.swing.*;
import java.awt.*;
public class mainMenu{
public mainMenu(){
JPanel menuPanel = new JPanel();
menuPanel.setSize(300,300);
menuPanel.setBackground(Color.BLUE);
}
}
And my goal is to add the JPanel inside the JFrame. And... I don't understand how to do it.
I tried to add the menuPanel class as an object in the mainFrame class so I could add the JPanel but it did'nt work. Then tried a bunch of other solutions from what I read on older questions but nothing really helped me.
PS : I know that I didn't add any layout manager or any other things in the code here because I want to keep the code very simple for the question.
So if you want to make a separate class for mainMenu, you should make it extend JPanel. This way, your mainFrame class can instantiate it.
Here is what I would tell you about the lessons I learned in building things in swing:
You (almost) never need a separate class to create a JFrame. If you create a class that extends JPanel, it is easy enough to create a JFrame in a static method (like main) to put your JPanel in. That being said, if you also want to use the JLayeredPane or want to add menus on a JMenuBar, there might be a case of subclassing JFrame. The advantage of not subclassing JFrame is that it makes it easier to stick your JPanel into a JFrame, a JDialog (via JOptionPane) or a JWindow.
Unless you have a Component that you think could be useful in other applications, you should build your entire GUI in one class, and also use that class as the Controller. What is an example of a Component that could be used in other applications? Back in the day, I made a "ColorButton" swing class, for color picking. This component has a self-contained model, has its own controller and an API to get the picked color. (https://sourceforge.net/p/tus/code/HEAD/tree/tjacobs/ui/ex/) which makes it reuseable. But normally, the elements of the GUI you are building aren't really reuseable outside of what you are doing in that class.
With that said, to the code you posted above:
Your mainMenu JPanel isn't accessible outside the class, so that is probably a problem. As I said in the paragraph above, if you're not sure that the Component you are creating could be used in another place, it is better to put the entire view and model building in the same class as the controller
You were close. You have to be able to get the JPanel from the MainMenu class.
The JFrame methods must be called in a specific order. This is the order I use for my Swing applications.
Class names start with an upper case character.
Here's one way you could code your MainMenu class.
public class PrincipalFrame {
public PrincipalFrame() {
JFrame mainFrame = new JFrame();
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.add(new MainMenu().getMenuPanel(), BorderLayout.CENTER);
mainFrame.setSize(1200, 750);
mainFrame.setResizable(false);
mainFrame.setVisible(true);
}
}
public class MainMenu {
private final JPanel menuPanel;
public MainMenu() {
this.menuPanel = new JPanel();
this.menuPanel.setPreferredSize(300, 300);
this.menuPanel.setBackground(Color.BLUE);
}
public JPanel getMenuPanel() {
return menuPanel;
}
}

Making a basic GUI for a Java 'Connect Four' game

This might be either a too basic and a too huge question to ask.
Although I study business management as my discipline, by some chance I am taking a Java module this year and am currently working on building a 'Connect Four' game. So far, I have succeeded making the game work in the console, however, I have utterly no idea about how to present the game through GUI. I mean, I could build a GUI window in a separate class but could you help me extending the class methods I created before to the GUI class that I don't have to write the whole programme again? Many thanks.
All of the codes I wrote are here: https://github.com/aca16kd/java-connect-four/tree/master/Assignment/src. The classes live in assignment2017 folder as well as the main class, PlayConnect4, and the EasyReader class which could be used as exactly same as java.util.Scanner lives in sheffield folder.
The simplest way (I can think of) is having a separate GUI class which extends Jframe and implements Action Listeners. This way you could have a simple text box in a frame with a couple of buttons.

Creating screens(windows) without JFrame

I have been looking at the source code of Notch's game Metagun as a guide to making my own. I noticed he never implements JFrame as a means of making a screen for the game to run in. I can't make any sense of his Screen class because I only see a drawImage method for some strings. What other ways can you create a window besides using JFrame in Java?
There are several ways to create windows in Java, but for a game, I believe you're looking for a window without decoration. I'll point you then to JWindow, but it's more rudimentary...
Looking at the source I found on github, he extends Applet and Runnable and creates a generic JFrame within Metagun.main.

Java ActionListener in another Class

I am trying to build a small notepad application using the Java Swing library. I have a main function which calls a constructor of JFrame (NotepadClass). In this NotepadClass I have a MenuDesigner class something like this:
this.setJMenuBar(new MenuDesigner());
The MenuDesigner class extends JMenuBar which calls actionListener (MenuActionListener) which is written in another class.
Now my question is: If I click on "new" menuItem, the title which is in NotepadClass should change. How do I access a class that is two levels up?
Which concept of Java should I use to accomplish this?
use Swing Action instead of ActionListener, this API is designed for your purpose
post an SSCCE demonstrated your issue, just about JFrame with JMenuBar, JMenu and one, two JMenuItem(s), noting else
Without seeing your code it's difficult to give a definitive answer, but one of the reasons to write a separate class to build your menu is that you can pass instances to the class.
this.setJMenuBar(new MenuDesigner(notepadClass));
This is one reason why it's good to have a model class or classes when you're building a GUI.
You can pass an instance of the highest level model class to all of your GUI components, and each component can get or set the parts of the model class that they represent.
You could pass down the NotepadClass in the constructors and provide a method to change the title.
Another option is to build the ActionListener inside the NotepadClass or let NotepadClass itself implement the Actionlistener interface so you can access the variables or methods.
Why is your actionListener for your menu in another class?
You could create a new class that implements ActionListener in which you can add you own logic. This way you can reuse it in another file.
Also, you should probably de-couple your MenuDesigner class by moving it into its own file.

Difference between JPanel, JFrame, JComponent, and JApplet

I'm making a physics simulator for fun and I was looking up graphics tutorials when I tried to figure out the difference between all these J's.
Can somebody elaborate on them or perhaps provide a link to a helpful source?
Those classes are common extension points for Java UI designs. First off, realize that they don't necessarily have much to do with each other directly, so trying to find a relationship between them might be counterproductive.
JApplet - A base class that let's you write code that will run within the context of a browser, like for an interactive web page. This is cool and all but it brings limitations which is the price for it playing nice in the real world. Normally JApplet is used when you want to have your own UI in a web page. I've always wondered why people don't take advantage of applets to store state for a session so no database or cookies are needed.
JComponent - A base class for objects which intend to interact with Swing.
JFrame - Used to represent the stuff a window should have. This includes borders (resizeable y/n?), titlebar (App name or other message), controls (minimize/maximize allowed?), and event handlers for various system events like 'window close' (permit app to exit yet?).
JPanel - Generic class used to gather other elements together. This is more important with working with the visual layout or one of the provided layout managers e.g. gridbaglayout, etc. For example, you have a textbox that is bigger then the area you have reserved. Put the textbox in a scrolling pane and put that pane into a JPanel. Then when you place the JPanel, it will be more manageable in terms of layout.
JFrame and JApplet are top level containers. If you wish to create a desktop application, you will use JFrame and if you plan to host your application in browser you will use JApplet.
JComponent is an abstract class for all Swing components and you can use it as the base class for your new component. JPanel is a simple usable component you can use for almost anything.
Since this is for a fun project, the simplest way for you is to work with JPanel and then host it inside JFrame or JApplet. Netbeans has a visual designer for Swing with simple examples.
You might find it useful to lookup the classes on Oracle's website, for example:
https://docs.oracle.com/en/java/javase/15/docs/api/java.desktop/javax/swing/JFrame.html
There you can see that JFrame extends JComponent and JContainer.
JComponent is a a basic object that can be drawn. JContainer extends this so that you can add components to it. JPanel and JFrame both extend JComponent so that you can add things to them. JFrame provides a window, whereas JPanel is just a panel that goes inside a window.

Categories

Resources