How do I run a Java program? - java

I've tried to make it work, am I missing a code to make it be able to show the gui on windows,
or do I need some kind of Java to run it?
Here is my code:
package math;
import java.awt.EventQueue;
import javax.swing.JFrame;
public class Main {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
JFrame window = new JFrame();
window.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}

Your code compiles and runs fine. (Although it is more appropriate to put setVisible command in the initialize method).
Seems like you could not export it properly. I exported your code with NetBenas and I could run it by double clicking on it. Try exporting your class with NetBenas (It makes it easier).
If you dont have NetBeans and creating the jar file via command line check your manifest file. Make sure you gave the correct main method name.

the Main window part doesn't make sense
replace your try-block
try {
Main window = new Main();
window.frame.setVisible(true);
}
with
try {
initialize();
frame.setVisible(true);
}

you have to set your frame visible in contructor.
private void initialize() {
frame = new JFrame();
frame.setVisible(true);
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

The code above works fine. It could be that it worked for you but you didn't realize because the window created is very small. It could also be that you tried to export this into a .jar file but didn't specify in the Manifest what the Main-Class should be. Depending on the IDE or build tool that you're using, the way might be different. It could also be that you simply do not yet know how to get this code running at all, i.e. compiling and running Java code. In that case, you need to learn about javac and java, which are included in the Java SDK.
If you use Java 8, you could actually simplify the code. You don't need an exception handler that prints the stack trace, the EventQueue is already doing that for you.
package math;
import javax.swing.*;
import java.awt.*;
public class Main {
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
JFrame window = new JFrame();
window.setVisible(true);
});
}
}
or, if the code is in a separate method:
package math;
import javax.swing.*;
import java.awt.*;
public class Main {
public static void main(String[] args) {
EventQueue.invokeLater(Main::initUI);
}
private static void initUI() {
JFrame window = new JFrame();
window.setVisible(true);
}
}
If initUI() is to be an instance method, you could do it like this:
package math;
import javax.swing.*;
import java.awt.*;
public class Main {
public static void main(String[] args) {
new Main();
}
Main() {
EventQueue.invokeLater(this::initUI);
}
private void initUI() {
JFrame window = new JFrame();
window.setVisible(true);
}
}

Related

Open a new JFrame window from a button click in another JFrame window

Update: I've decided the simplest thing to do at the moment would be to use separate JPane's and not JFrame's for the sub-menu's. I'll create them all together and set the others to invisible, and toggle that way. The menus aren't that complex that this would be too much of a problem.
I am creating a GUI that opens another JFrame window from a button click in another. I am just not sure of the right way to approach closing the main window when one of the buttons is clicked, but not closing the whole program. Neither am I sure how to get the second window visible (the line of code I tried from another example isn't working). The second frame that is brought up will give the user options to do things and will actually call another program/class to run on a button clicked within it (the result of one of the options is a long program so I think I need to run it on another thread.). After the program has finished running, the user will have the option to return to the main menu, which would close the second menu (and kill it), or exit the program (and thus kill the main menu and clean everything up). From the main menu, they will also have the option to close the program, where everything will be cleaned up. This is what I have so far:
Main GUI:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
class GUIMain implements ActionListener {
GUIMain(){
JFrame jFrm = new JFrame("Data Mining Application");
jFrm.setSize(800,600);
jFrm.setLayout(new BorderLayout());
jFrm.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
prepareGUI(jFrm.getContentPane());
jFrm.pack();
jFrm.setVisible(true);
}
private void prepareGUI(final Container pane){
JPanel mainPanel = new JPanel(new GridLayout(3,2,50,50));
JButton b1 = new JButton("Pre-processing");
b1.addActionListener(this);
mainPanel.add(b1);
pane.add(mainPanel,BorderLayout.CENTER);
}
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new GUIMain();
}
});
}
#Override
public void actionPerformed(ActionEvent e) {
switch (e.getActionCommand()){
case "Pre-processing":
PreProcessingGUI window = new PreProcessingGUI();
window.getFrame.setVisible(true); //not working
break;
// etc
default:
break;
}
}
}
The class and JFrame that is called:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class PreProcessingGUI implements ActionListener {
PreProcessingGUI(){
JFrame jFrm = new JFrame("Pre-processing");
jFrm.setSize(800,600);
jFrm.setLayout(new BorderLayout());
jFrm.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
prepareGUI(jFrm.getContentPane());
jFrm.pack();
}
private void prepareGUI(final Container pane) {
//do stuff
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
PreProcessingGUI window = new PreProcessingGUI();
// Not surewhat to do here either as the program is not recognising the getFrame method
//window.getFrame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
#Override
public void actionPerformed(ActionEvent e) {
// do stuff
}
}
Well I don't work much with Swing but I can help you a bit:
When you try to show the second window in GUIMain.actionPerformed you seem to try to get the frame with a public variable having a method (getFrame).
window.getFrame.setVisible(true);
This variable doesn't exist! It is not defined anywhere. There is no magic here!
You should implement a getFrame() method in PreProcessingGUI and use it in instead of your variable.
In GUIMain.actionPerformed:
window.getFrame().setVisible(true);
In PreProcessingGUI
public class PreProcessingGUI implements ActionListener {
private JFrame jFrm; //You asssing is as you the constructor
PreProcessingGUI(){
jFrm = new JFrame("Pre-processing");
...
}
public getFrame(){
return jFrm;
}
...
In addition to that, I would say you should consider using JDialog (and optionally make it modal) instead of a JFrame.

JFrame not running on Mac

Upon running the project, Java shows up on my dock but doesn't run at all. It stays there for about 5-10 seconds and disappears. Is there something wrong with the code or my Mac?
import javax.swing.JFrame;
public class Test extends JFrame {
public Test() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(600,600);
this.setVisible(true);
this.setResizable(false);
this.setTitle("Test");
}
It has nothing to do with being on a Mac.
Add this to your main function (which could be in Test.java or in any other class) in order to create a GUI thread and instantiate your Test JFrame class.
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Test();
}
});
}
Calling new Test(); is all you need to do, however doing it inside javax.swing.SwingUtilities.invokeLater is very important for the future of your application as it allows you do to other things in the background and at the same time, which will remove any visible latency. This is what Java itself recommends to do.
You will need to import javax.swing.SwingUtilities; or just do import javax.swing.*;.
You need to call somewhere the instance of test in order to work..
public class Test extends JFrame {
private static final long serialVersionUID = -2949102464273156555L;
public Test() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(600, 600);
this.setVisible(true);
this.setResizable(false);
this.setTitle("Test");
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Test();
}
});
}
}

Java components are invisible when swing app is run as APPLET

Respected all,
I am making a Swing-application window in ECLIPSE. When I run the program as 'JAVA-Application' the program functions well. However when I try to run the program as "Java_applet" the components like 'command button', 'textbox' are invisible.
I am entirely new to Java. I had previously worked on C#. Kindly please help me.
import java.awt.EventQueue;
import java.applet.*;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
import java.awt.BorderLayout;
public class sa extends Applet{
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
sa window = new sa();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public sa() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JRadioButton rdbtnNewRadioButton = new JRadioButton("New radio button");
frame.getContentPane().add(rdbtnNewRadioButton, BorderLayout.CENTER);
}
}
You can't just have your class extend Applet and expect that that is all that is necessary for it to behave like a proper Applet. You need to give the class a proper init method and build the GUI from within this method. But most importantly you will need to read an Applet tutorial, any decent tutorial should do. Myself, I'd have my GUI extend JPanel, build the GUI in its constructor, and then I could use this JPanel in a JApplet or JFrame as needed.
As Andrew aptly notes in comment,
I think the OP should also dump the entire 'applet' part of it and develop the JFrame with an idea to launching it from a link using Java Web Start. At least then it would have a better chance for working for users of Chrome and FireFox (which are both planning to remove all support for applets).

Java. Could not find or load main class

I know this has been asked before...Just none of the answers on other questions worked.
When I try to run this in eclipse I just get Error: Could not find or load main class Hey.Init in the console. "Hey" is the package.
I can post the third class I just don't think it's relevant.
package Hey;
import javax.swing.SwingUtilities;
public class Init {
static Runnable createGui = new Runnable(){
public void run(){
new Gui();
}
};
public static void main(String[] args){
SwingUtilities.invokeLater(createGui);
}
}
Other Class:
package Hey;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Gui {
private JFrame frame = new JFrame("Title");
private JButton button;
public Gui(){
frame.setLayout(new FlowLayout());
button = new JButton("DON'T HIT ME!!!");
button.addMouseListener(new Yo());
}
}
Weird as i do not see any issue in here. However it could be that you are trying to run a file which is not set as "main project" and effectively may not have a main method, if you know what i mean. Also on the side note
#Override
public void run(){
Gui gui = new Gui();
}
And one more thing why not implement the interface ?
public class Hello implements Runnable{
....
}

How does JFrame.setDecoratedLookAndFeel(true) work exactly? Getting some weird results

So I have 3 JFrames. JFrame 1 (aka opts) is for inputting some variables. On OK, it creates JFrame 2 (aka view, a graphical viewing Frame). In the constructor, JFrame 3 (aka manager, for cycling through the graphical views) is created instantly.
I want them to be Windows-alike decorated with JFrame.setDefaultLookAndFeelDecorated(true) (from now on referred to as "it").
A few differtent cases:
When I don't set it at all, all JFrames are Windows decorated.
When I put it in the "opts" or "manager" constructor, run the project, and click the OK button, all JFrames are Windows-alike decorated.
However, when I put it in the "view" constructor, only "opts" and "view" are Windows decorated, but "manager" is Java decorated.
But when I put it in a class which does nothing more than read some data from a file, and make some objects out of it, only "opts" is Windows decorated.
How exactly does it work? I want to know why it happens as it happens.
Edit: I can't reproduct case 4, but still, some things happen that I don't understand. Try commenting out some of the "it"s. It'll give some strange results.
package test;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Frame1 extends JFrame {
public Frame1() {
JFrame.setDefaultLookAndFeelDecorated(true);
Frame1.setOptions(this);
JButton b1 = new JButton("hi");
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
makeFrame2(2);
}
});
this.add(b1);
}
public void makeFrame2(int x) {
this.dispose();
new Frame2(x);
}
public static void setOptions(JFrame f) {
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(250, 250);
f.setEnabled(true);
f.setVisible(true);
}
public static void main(String[] args) {
new Frame1();
}
}
package test;
import javax.swing.JFrame;
public class Frame3 extends JFrame {
Frame2 link;
SomeClass sc;
public Frame3(Frame2 link) {
JFrame.setDefaultLookAndFeelDecorated(true);
Frame1.setOptions(this);
this.link = link;
sc = new SomeClass(25);
}
}
package test;
import javax.swing.JFrame;
public class SomeClass {
int x;
public SomeClass(int x) {
JFrame.setDefaultLookAndFeelDecorated(true);
this.x = x;
}
}
package test;
import javax.swing.JFrame;
public class Frame2 extends JFrame {
public Frame2(int x) {
JFrame.setDefaultLookAndFeelDecorated(true);
Frame1.setOptions(this);
new Frame3(this);
}
}
According to the oracle java documentation, setDefaultLookAndFeelDecorated changes the parameter for all newly created JFrames, all existing JFrames are unchanged.
JFrame Documentation

Categories