Creating screens(windows) without JFrame - java

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.

Related

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.

How do I create a JPanel and put it on a JFrame from another class?

I am trying to learn some Java GUI stuff. I am trying to create a special jPanel, which I have created in another class [it is a Panel with two labels on it] inside one class, and using a passed JFrame add it to that JFrame from the other class. I have been trying myJFrame.add(thePanel) and such, but it never seems to actually do anything. I have tried doubly making sure it is sset to visible, is at a reasonable location, ect.
I am trying to create the panel in one class, and .add it to the given jframe. There is probably something I am not understanding.
I am not the best at the java GUI stuff... so please be kind :) Any advice you can give would be wonderful.
I am using Netbeans. If I drag the special Panel I created over to my panel in the Design editor, it adds an instance of that panel, but I can't seem to make my own instance of this and have it appear.

Unable to get values from another jFrame

I want to fill values of multiple jTextBox from a jFrame into another, using accessor methods like
String getNameVal()
{
return jTextBox1.getText();
}
How to call these methods from another jFrame?
Suggestions:
It sounds like your GUI code is geared towards making JFrames, and if so, you will want to avoid this. You are painting yourself in a corner by having your class extend JFrame, forcing you to create and display JFrames, when often more flexibility is called for. In fact, I would venture that most of the Swing GUI code that I've created and that I've seen does not extend JFrame, and in fact it is rare that you'll ever want to do this.
More commonly your GUI classes will be geared towards creating JPanels, which can then be placed into JFrames or JDialogs, or JTabbedPanes, or swapped via CardLayouts, wherever needed. This will greatly increase the flexibility of your GUI coding.
This question has direct bearing on your problem. I will guess that your main problem isn't how to give classes getter methods, and how to have other classes call the getter methods. More often then not, when faced with the issue of extracting information from one GUI view to another, the issue is one of when to extract the information. If you displayed your second window as a non-modal JFrame, and then had the calling class immediately extract the data from that second JFrame, you'd get nonsense data, because you'd be extracting data before the user would have time to interact with the 2nd window and enter data.
One possible solution to this when using non-modal windows to get information from the user is to use a WindowListener so you can be notified when the user has completed his dealing with the second window, and so now data can be safely extracted.
Often better is for the 2nd window not be non-modal, as JFrames are, but instead to be a modal window such as a modal JDialog. When the calling code displays a modal dialog, all code flow in the calling code stops until the dialog is no longer visible. In this situation, no WindowListener is needed since you will know exactly when the dialog has been dealt with -- on the code line immediately after you set it visible -- and so can extract your data from it with ease.
A nice variant on this has already been mentioned in by Andrew Thompson in comments -- use a JOptionPane. Don't poo-poo this option since JOptionPanes are powerful tools, likely much more powerful than you realize as they can hold fully formed complex JPanel views, and behave just as described above, as modal dialogs.
If you need more specific help, then please don't hesitate to comment to this answer. Also if so, then consider creating and posting a Minimal, Complete, and Verifiable Example Program where you condense your code into the smallest bit that still compiles and runs, has no outside dependencies (such as need to link to a database or images), has no extra code that's not relevant to your problem, but still demonstrates your problem.
Edit
For my mcve code examples of the above suggestions, please my answers to the following StackOverflow Questions:
Using a modal JDialog to extract information
Using a JOptonPane to extract information
I assume the textfields are present in frame1 and you want to access them in frame2. The following can be a way to achieve this:
First create getters for all JTextFields that you have in your frame1. Alternatively you can have them in a panel and call getComponents() method.
Create a private variable of JFrame type in your frame2.
Modify the constructor of frame2 to receive the frame1 object and assign it to the private variable of JFrame type.
Now you can create a close() method in frame2 which disposes the frame2 and sets frame1 to visible.
But in my opinion you should create a class which handles the data in these textfields. Initialize the class object in any button click of frame1 and check for any inconsistency in the input. I can guess there is something wrong with your design.

what is Single Frame Application and what is FrameView?

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

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