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);
}
});
}
Related
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();
}
});
}
}
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.
I have this code. Eclipse tells me that the syntax is correct but when I run the program it gives me this error:
Exception in thread "main" java.lang.NoSuchMethodError: main
What's wrong?
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main extends JFrame {
private static final long serialVersionUID = 1L;
public void main(String[] args){
JFrame Main = new JFrame("TEST");
Main.setSize(600, 600);
Main.setLocationRelativeTo(null);
Main.setVisible(true);
Main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Adding JPanel
JPanel panel = new JPanel();
Main.add(panel);
//JPanel settings
panel.setLayout(null);
panel.setBackground(Color.GREEN);
//Adding JButton
JButton button = new JButton("Button 1");
JButton button2 = new JButton("Button2");
panel.add(button);
panel.add(button2);
//Button action
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
JPanel panel = new JPanel();
JPanel panel2 = new JPanel();
Main.this.getContentPane().remove(panel);
Main.this.getContentPane().add(panel2);
Main.this.getContentPane().validate();
}
});
//JButton settings
button.setBounds(70, 160, 200, 200);
button2.setBounds(320, 160, 200, 200);
}
}
Your main method is not static, and you should make it static. Check this to see why
public static void main(String [] args)
Your main method should be static
public static void main(String[] args){
----
}
And see why ??Why is the Java main method static?
The class requires a method with a signature of:
public static void main(String[])
You have to make your main method static:
public static void main(String[] args) {
}
public static void main(String[] args)
instead of
public void main(String[] args)
public means that the method is visible and can be called from other objects of other types. Other alternatives are private, protected, package and package-private. See here for more details.
static means that the method is associated with the class, not a specific instance (object) of that class. This means that you can call a static method without creating an object of the class.
void means that the method has no return value. If the method returned an int you would write int instead of void.
The combination of all three of these is most commonly seen on the main method which most tutorials will include.
Your main method must be static (ie. class-bound not instance-bound) because at the time of starting your app there no class instances that could call your class' main method.
The class requires a method with a signature of like this:
public static void main(String[])
this is class main
package com.First.Game;
import javax.swing.JFrame;
public class main {
public static Display f = new Display();
public static int width =600;
public static int height = 400;
public static void main(String args[]){
f=new Display();
f.setSize(width, height);
f.setResizable(false);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setTitle("Sticky Zombies");
f.setLocationRelativeTo(null);
System.out.println("STICKY ZOMBIES is running on a main window");
}
}
and this is class Display
package com.First.Game;
import javax.swing.*;
public class Display extends JFrame{
public main panel;
public Display(){
panel= new main(this);
}
}
im a begginer and i really need help
i basicly copied some code exactly from what i saw and in the video i watched, he could run it perfectly(this is not the complete code of what i watched)
You have not defined any constructor for your class main. In such cases your class will have only default constructor(zero argument constructor). But in your code panel= new main(this); you are trying to call parametrized constructor which does not exist. Hence you get the error.
Replace it with this : panel= new main();
OR
define a constructor explicitly in your main class as follows :
public main(Display disp) {
this.display = disp;
}
package com.First.Game;
public class Display {
public main panel;
public Display(){
panel= new main(this);
}
public static void main(String[] args) throws Exception {
Display obj = new Display();
}
}
And change the name of the class main to something else
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() {
//............
}
}