JFrame not running on Mac - java

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();
}
});
}
}

Related

Java - How to run init method in Main class

I'm want to run my gui, but I don't really know how to initialize this class. I'm transitioning from python to Java, so I'm still fairly new to this. The code works, I just need to know how to run it.
import javax.swing.*;
import java.awt.*;
// Where the main run function for the application will lie
public class MainWindow extends JFrame{
public void init(){
// Initial window
JFrame startFrame = new JFrame("P.D");
startFrame.setSize(1200, 800);
startFrame.setVisible(true);
startFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Panel to hold our buttons
JPanel startPanel = new JPanel();
startFrame.add(startPanel);
// Button to initialize everything
JButton startButton = new JButton("Start");
startPanel.add(startButton);
startFrame.setLayout( new GridBagLayout() );
startFrame.add(startButton, new GridBagConstraints());
// Take out the border around the text
startButton.setFocusable(false);
}
public static void main(String[] args) {
}
}
How do I run the init() method under static void main(String[] args?
suppose you just create a new MainWindow object in main method and call the init() method.
public static void main(String[] args){
new MainWindow().init();
}
You can also run the function by making it static.
Like python "static method".
public static void init(){
..
And:
public static void main(String[] args) {
init();
}
You can implement constructor instead of init() method and you can start gui like below
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MainWindow().setVisible(true);
}
});
}

How do I run a Java program?

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);
}
}

? concerning running GUI classes..please assist

Consider the two classes and could you offer an explanation on the difference between them.. I know the first creates an object of the class but that is not my concern...
Specifically the block of code including and below the line with,
**javas.swing.swingUtiliies.invokeLater(new Runnable() {
//also both import javax.swing
public class HelloWorld extends JFrame{
public HelloWorld(){
super("HelloWorldSwing");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label = new JLabel("Hello World");
getContentPane().add(label);
pack();
setVisible(true);
}
public static void main(String[] args) {
HelloWorld h = new HelloWorld();
}
}
public class HelloWorldSwing {
private static void createAndShowGUI() {
JFrame frame = new JFrame("HelloWorldSwing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label = new JLabel("Hello World");
frame.getContentPane().add(label);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
createAndShowGUI();
}
});
}
}
The second example upholds the rule to execute all GUI-related code on the Event Dispatch Thread, which is achieved by passing a Runnable with said code to the invokeLater method.
By "GUI-related code" I mean instantiating any AWT/Swing classes, calling any methods on them or accessing any properties.

JApplet contain a frame, or JApplet alone?

I have this project that I need to publish, but don't know which way will be best. Can somebody help me please.
I have a Gui application(Jframe). In there I have a Jpanel that contains some animations (implement runnable). So in my main method I would call the constructor first, so everything display nicely, then called Runner.start(). (Thread)
So basically the gui pop up and then the animation happens, to be specific the animation is just the title of my program that slides in.
Now I want to put this on the website so my students can use.
I do not want to use java web start, i want this to act as an applet.
So do i put this jframe into my applet?
or should I convert this whole thing from jframe to japplet? and is this applet need to implement Runnable?
The thing that bug me is that Japplet has no main method, so how can I specified when my Jpanel can execute its animation? I want the animation to occur after everything has load up on the screen, not before.
I guess put it as the last statement of init() method? Correct me if I am wrong.
Thanks,
I have a Gui application(Jframe). .. I want to put this on the website so my students can use.
While it is possible to convert the frame to an applet, a better option is to launch the frame from a link using Java Web Start.
you can do both:
MainGui
import java.awt.BorderLayout;
import javax.swing.*;
public class MainGui extends JPanel {
public MainGui() {
this(null);
}
public MainGui(MyJApplet applet) {
this.applet = applet;
if (!isApplet()) {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} else
frame = null;
setLayout(new BorderLayout());
// setPreferredSize(new Dimension(640, 480));
SwingUtilities.invokeLater(new Runnable() {
public void run() {
MainGui.this.run();
}
});
}
String title() {
return "Title";
}
public void addContent() {
add(new JLabel("add content! top"));
}
void run() {
if (isApplet()) addContent();
else {
frame.setTitle(title());
frame.getContentPane().add(this, BorderLayout.CENTER);
addContent();
frame.pack();
System.out.println(getSize());
frame.setVisible(true);
}
}
boolean isApplet() {
return applet != null;
}
public static void main(String[] args) {
new MainGui(null);
}
protected final JFrame frame;
protected final MyJApplet applet;
private static final long serialVersionUID = 1;
}
MyJApplet
import java.awt.BorderLayout;
import javax.swing.JApplet;
public class MyJApplet extends JApplet {
public void start() {
}
public void init() {
getContentPane().setLayout(new BorderLayout());
addContent();
}
public void addContent() {
getContentPane().add(new MainGui(this), BorderLayout.CENTER);
}
public static void main(String[] args) {
new MainGui(null);
}
private static final long serialVersionUID = 1;
}

how to call a public class to display gui in java using jformdesigner

I wish to display my gui in the main method however it doesn't seem to do so...
I've used the suggestion here:
jformdesigner design it won't display?
But that did not work,
My error at the moment is that eclipse is suggesting I need to create a method called setDefaultCloseOperation which is already defined in the class and the same for the setvisible.
"The method setDefaultCloseOperation(int) is undefined for the type bmicalc
The method setVisible(boolean) is undefined for the type bmicalc"
main method:
import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
public class iu {
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
bmicalc GUI = new bmicalc();
GUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GUI.setVisible(true);
}
});
class bmicalc extends JFrame{
public bmicalc() {
initComponents();
}
private void initComponents() {
JFrame bmiCalculatorFrame = new JFrame();
{
bmiCalculatorFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
bmiCalculatorFrame.setTitle("BMI Calculator");
Container bmiCalculatorFrameContentPane = bmiCalculatorFrame.getContentPane();
bmiCalculatorFrameContentPane.setLayout(new GridLayout());
}
}}}}
Your bmicalc class should extend JFrame, but it is not. setDefaultCloseOperation and setVisible methods belong to JFrame.
Also, it is not very clear, but it looks like the JFormDesigner generated another JFrame.
EDIT:
Here is a example of JFrame generated by JFormDesigner:
public class TestFrame extends JFrame {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
TestFrame frame = new TestFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
public TestFrame() {
initComponents();
}
private void initComponents() {
// JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents
//======== this ========
Container contentPane = getContentPane();
contentPane.setLayout(new FormLayout(
"default",
"default"));
pack();
setLocationRelativeTo(getOwner());
// JFormDesigner - End of component initialization //GEN-END:initComponents
}
// JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variables
// JFormDesigner - End of variables declaration //GEN-END:variables
}
EDIT - according to the last question edit
To address your compilation issues see the snippet below. However, it is not clear what are you trying to achieve with JFrame bmiCalculatorFrame.
class bmicalc extends JFrame{
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
bmicalc GUI = new bmicalc();
GUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GUI.setVisible(true);
}
});
}
public bmicalc() {
initComponents();
}
private void initComponents() {
//............
}
}

Categories