I'm new to Japplets.. I'm wondering if I have the following...
classA A = new classA();
//launch Japplet here
and classA extends Japplet how would I launch the Japplet?
If it's any help I'm using a Java Bean and I've tried calling classA.init(); although this class does execute when I call this method it just doesn't show my JApplet on display.
Perhaps the init() method?
I suspect you either need to use a browser or an applet viewer to see anything. During development I would recommend an applet viewer as it is easier to get going (fewer security issues).
Related
So I'm doing a project for school where I'm supposed to simulate a supermarket. I'm pretty far into it, but I came across a problem I haven't had before. Long story short, (among others) I have a controller class and a JFrame class.
In the main class and method, I have an object of the Controller class. In the controller cass I have an object of the JFrame class. When I run the project, the main makes a controller, which makes a JFrame.
Now, I want to add a button into the JFrame that signals the controller I want to 'move on' the simulation. I'm kind of stumped as to how to accomplish this. Basically, I want to add a button into the JFrame that will call a method in the controller class when clicked.
I'm happy to post code if necessary, but it's more a problem of 'how do I do this?' than fixing something that is broken.
Extend the JFrame with your own JFrame, which has an overloaded constructor that takes an instance of the Controller. Then when you create an instance of the JFrame from the Controller you pass it a reference to itself using the "this" keyword. When the button inside the JFrame needs to notify the controller it should be able to call some getController() method. Of course, this is all conjecture without seeing some actual code :)
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.
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.
So I have two classes.
One is a class called Main. The Main class is supposed to process some data.
Then I have another class called MainApplet, an Applet of course.
How can I display MainApplet from the Main class? This is what I have so far, but the applet does not show:
public class Main {
public static void main(String args[]) {
System.out.println("Starting application.");
MainApplet Main = new MainApplet();
Main.setVisible(true);
Main.show();
} }
How can I display MainApplet from the Main class?
You don't. You display an applet from HTML code.
Are you sure your GUI is in fact an applet? Does it extend JApplet or Applet? If so and you want to show it on the desktop through code, then don't make it an applet but instead display a JFrame. The Java Swing tutorials will show you how to do this: How to use Swing Components
Edit
you state:
Basically I have a Main class that is not an applet. It doesn't extend anything. Then I have another class named MainApplet that is an applet (extends JApplet). I want to run Main first, then display MainApplet after... but I can do it the other way around if needed.
You don't sound like you're running your code from a web page (for some reason you're still keeping this information from us), so the solution is not to use applets for this. Instead create a JFrame. Please check out the tutorials that I've linked to above since an applet is not appropriate for your needs.
You will have a main that creates your GUI, that passes any information into the GUI via constructor or method parameters, and then that tells the GUI to show itself (by calling setVisible(true) if it's a JFrame).
Your mainApplet class must extend Applet. You would then replace your main(...) method with init(...) as this is the method that gets called when you open an Applet. You can test this in most environments such as Eclipse. It order to actually put the Applet on a webpage, you must tell the applet to display using HTML in the page.
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