I am a newbie in the Java world and this is among my first programs (Hello World, obviously!). The problem is when I try to run this program as a class from the menu with a play icon on it, a blank window shows up with no "Hello World" on it (just white colour filling the window). At the bottom border of this window there is a black thick line. But when I run this program as an applet from the same menu everything is okay and the "Hello World" shows at the right position and everything is fine. But how can I make the program run regularly as a class???
This is my code...
import acm.graphics.*;
import acm.program.*;
import java.awt.*;
public class HelloProgram extends GraphicsProgram {
private static final long serialVersionUID = 1L;
public void run() {
GLabel label = new GLabel("hello, world", 100, 75);
label.setFont("SansSerif-36");
label.setColor(Color.RED);
add(label);
}
}
I have no idea about acm, but looked in this page: http://jtf.acm.org/tutorial/Introduction.html
As you see there GraphicsProgram is a subclass of JApplet and because of that it's supposed to be run as applet, not desktop application.
Try this. It will be a separate class, but you will be able to run your program from this one. Please let me know if you are confused.
public class Runner{
public static void main(String[] args){
HelloProgram p = new HelloProgram();
p.run();
}
}
I agree with publ1c_stat1c, your program is missing the "main" method for it to be considered a standalone application.
Create an instance of your application
HelloProgram hello = new HelloProgram();
call the run method of the instance
hello.run();
the main method doesn't have to be in a different class, try adding below codes in-between "run method" and "private static final long serialVersionUID = 1L;"
public static void main(String[] args){
HelloProgram hello = new HelloProgram();
hello.run();
}
Related
How can I embed a PApplet into a JApplet ?
I wanted to add it to a JPanel inside the applet, but I couldn't.
If any of you know how I can do this. ??
As of Processing 3, you can no longer do this. PApplet no longer extends Applet, so it can't be treated as a component.
For 95% of users, this is okay. Applets are dead anyway, so you really shouldn't be using them. If at all possible, you should try deploying with Processing.js.
If you need to execute a Processing sketch from Java code, then you should use the PApplet.main("YourSketchNameHere"); function to launch it. Processing will take care of the window for you.
If you really need to treat a PApplet as a component, then you're going to have to go through its PSurface. The PSurface class contains a getNative() function that returns an object that can be treated as a component. But that's overkill for most Processing users.
Prior to Processing 3, this code should have worked for you as #Kevin has explained. So, if your question is directed towards understanding a legacy code here is what you will need to know:
import javax.swing.JFrame;
import javax.swing.JPanel;
class MyPApplet extends PApplet implements ActionListener{
#Override
public void setup() {
super.setup();
// setup
}
#Override
public void draw() {
// my draw code
}
}
public class PAppletDemo {
public static void main(String[] args) {
final JFrame frame = new JFrame("PApplet in Java Application");
JPanel panel = new JPanel();
//create an instance of your processing applet
final MyPApplet applet = new MyPApplet();
applet.init();
panel.add(applet); // From processing 3, this will give you error that applet is not a Component
frame.add(panel);
frame.setSize(applet.getSize().width, applet.getSize().height +200);
frame.setVisible(true);
}
}
To circumvent this, you will need to use PSurface getNative() function. Please refer to the example and discussion given on this link.
I want to include one java file into another. Both have the main functions in it. One file looks similar to the following:
public class FileShow
{
public static void main(String args[])
{
JFrame guiFrame = new JFrame();
JFrame.setDefaultLookAndFeelDecorated(true);
JDialog.setDefaultLookAndFeelDecorated(true);
//make sure the program exits when the frame closes
guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
guiFrame.setTitle("RTL Parser GUI");
guiFrame.setSize(500,500);
//This will center the JFrame in the middle of the screen
guiFrame.setLocationRelativeTo(null);
JPanel comboPanel = new JPanel();
JTextField handle = new JTextField(30);
comboPanel.add(handle);
guiFrame.add(comboPanel);
guiFrame.setVisible(true);
}
}
whereas my other java file is:
public class AnotherFile{
public static void main(String[] args) {
new AnotherFile();
}
public AnotherFile()
{
guiFrame = new JFrame();
//make sure the program exits when the frame closes
guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
guiFrame.setTitle("Assertion-Based GUI");
guiFrame.setSize(500,500);
//This will center the JFrame in the middle of the screen
guiFrame.setLocationRelativeTo(null);
JPanel comboPanel = new JPanel();
JTextField handle = new JTextField(30);
comboPanel.add(handle);
guiFrame.add(comboPanel);
guiFrame.setVisible(true);
}
}
Is there any way to combine both the files and run together, since both have the main functions in it?
How do i combine both the files in same java file and run both of them together?
You just can't do that. Each Java file should have only one main method.
But you can better organize your files to do what you want:
public class FileShow{
public void doSomething(){
//...
}
}
public class AnotherFile{
public void doSomething(){
//...
}
}
public class mainClass(){
public static void main(String args[])
new FileShow().doFileShow();
new AnotherFile().doAnotherFile();
}
}
I would just add 'AnotherFile' object in the main method of Fileshow. You can only have one main method.
So in Fileshow.java, in the main method add
Anotherfile a = new Anotherfile()
From what you wrote, it is completely unclear, what you want to achieve. Include two Java classes into one .java file? Into one .jar file?
What do you mean by "run together"?
Combining two top-level Java classes in one source file is possible (according to JLS), while only one of them may be public. I believe though, it is not a best practice, just because you get quite a messy lifecycle of your classes. But if you still want to do it, you must make one of them either package private or nested.
Getting both to one jar is trivial. Just call jar cf jarname <classes>. It would also be possible to call the main methods separately by explicit mentioning them in java command line, like java -cp jarname <package>.FileShow.
Still, I'm not sure I understood your question right.
In Java, each Java file can contain one public class and by default JDK will call it's main method. If you have two classes both having a main method and you want to keep it in one Java file, the two classes can not be public, one must be an inner/nested class. I have given an example below.
public class FileShow
{
public static void main(String args[])
{
AnotherFile.main(args);;
// Your code
}
static class AnotherFile
{ // as it contains a static method
public static void main(String[] args) //or any static class
{
new AnotherFile();
}
public AnotherFile(){
// Your code
}
}
}
Logically it will work. But I highly discourage to go with this. It is not standard.
Here while I run java project in netbeans all things are working okay. But after they are built there is not any item added in combobox as it works during netbeans run. The sample code is given below.
First Login JFrame
public class Login_Frame extends javax.swing.JFrame {
welcome w = new welcome();
public Login_Frame() {
initComponents();
}
//button action perform event for dispose this window and open new welcome window
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
.
.
w.setVisible(true);
this.dispose();
.
.
}
}
Second JFrame
public final class welcome extends javax.swing.JFrame {
// comboitem is class in which method for adding item in combobox from sqlite
db is declared
comboitem c = new comboitem();
// textclass is class in which method for changing lowercase text entered in
text to uppercase is declared
textclass tc = new textclass();
public welcome() {
// while I try to run the project using netbeans run project option
// logincall() method initialized and work fine.
Problem
After project built when I try to run the jar file from the cmd. it runs without any
error but logincall() method doesn't work or may be not initialized.
initComponents();
logincall();
.
.
}
public void logincall(){
//Remarks
//tc.uppercase() method is working fine after built. But other c.but_stn() like
// doen't.while during running project through netbeans all thing working fine.
c.bus_stn();
c.bus_trl();
c.inq_stn();
c.editframe();
c.userlist();
c.editTrainStation();
c.editFlightStation();
c.flightFlight();
c.pickupstand();
tc.uppercase();
}
I didn't know what is wrong with it. I searched on google but didn't find any proper answer.
There also any error showing up in netbeans. Please fill free to ask any questions if more information is needed. I appreciate all your replies.
This is my Welcome Main class's main method.
public static void main(String args[]) {
...
look and feel auto generated code
....
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new welcome().setVisible(true);
}
});
}
A couple of things that might be an issue here:
1) Are you running your GUI on the EventDispatchThread? this is mandatory for java swing GUI to be able to work properly. The main reason for this is concurrency. Details here.
2) Are you re-rendering your combobox? it is important that you do this because changes to GUI elements may not be immediately shown.
3) What Database are you using? in order to determine if the fault lies in the code or the DB you could write a test using static data, if it loads in the IDE and outside of it chances are that your code is correct but the DB isn't
I am having some trouble with my code which is written in java. The first time after I start eclipse it launches and runs perfectly, but if I try to run it again - without making any changes - the only thing I see is an empty JFrame. Why might this happen? I have gotten quite a bit of files so to launch them all up here would be a lot to look through. Maybe you've tried something like this before? Where the program can launch sometimes? If not tell me so I can add the code.
I know that all of my classes are called since I have printed them all in my search to get the game to work.
The entire code worked until I started to put most of it in different objects in order to make it easier to look through.
package Pacman;
import java.util.Scanner;
import javax.swing.JFrame;
public class Frame extends JFrame{
public static int Width, Height;
public static void main(String[] args){
Scanner console = new Scanner(System.in);
Width = console.nextInt();
Height = console.nextInt();
new Frame();
}
public Frame(){
new JFrame();
this.setTitle("PacMan");
this.setLocation(400,300);
this.setResizable(false);
this.setVisible(true);
Screen screen = new Screen(this, Width, Height);
this.add(screen);
}
}
This is the JFrame, but I am pretty sure the problem isn't here.
If you want to see the entire codesystem it's at github: https://github.com/Lampeskaerm/SoftTek_Projekt.git
I hope you can help me
Anne-Katrine
After adding the component you should use this.setVisible(true) in the last.
So, for some reason when I try to use a for loop to initialize panels in chess board, it actually loops the loop itself. In other words, it doesn't go on forever, but it starts and completes again and again.
package chessgame;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ChessGame extends JFrame implements ActionListener{
public static final int WIDTH=800;
public static final int HEIGHT=800;
public static void main(String[] args) {
ChessGame gui = new ChessGame();
gui.setVisible(true);
}
public ChessGame(){
super("Chess Game Demo");
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(8,8));
JPanel[] chessSquares = new JPanel[64];
for (int a=0;a<64;a++){
System.out.println(a);
}
}
}
I have included all potentially relevant code because I plan to use indices of chessSquares to color squares black and white. When I do this I also get a NullPointerException. I can understand why I'm getting that given the following error, but I cannot at all understand why a would be printed 0, 1....62, 63 over and over again. I am relatively new to Swing and have absolutely no idea why it does this. If anyone could explain that would be tremendously helpful. Thanks.
Don't put meaningful initialization in ChessGame's constructor, but instead override frameInit. When you do, also be sure to call super.frameInit(). See the javadoc or this tutorial.