0

I find this weird, since I was just following a tutorial and his worked fine. We have the same exact code..

I tried this source code on another computer but still didn't work.

main class:

import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class swingJava {

    public static void main(String[] args){
        JFrame frame = new JFrame("Hello World!");

        SwingUtilities.invokeLater(new Runnable() {
            public void run(){
                frame.setSize(500, 400);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
            }
        });
    }
}

Here's the mainframe class.

import java.awt.*;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JFrame;

public class MainFrame extends JFrame{
public MainFrame(String title){
    super(title);

    // Set layout manager
    setLayout(new BorderLayout());

    // Create Swing Component
    JTextArea textArea = new JTextArea("test");
    JButton button = new JButton("Click me");

    // Add Swing Componenents to content pane

    Container c = getContentPane();

    c.add(textArea, BorderLayout.CENTER);
    c.add(button, BorderLayout.SOUTH);
}
}

Here's the tutorial I followed https://www.youtube.com/watch?v=svM0SBFqp4s

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
ndg
  • 59
  • 1
  • 9
  • 6
    You never create or use an instance of `MainFrame` – MadProgrammer Aug 04 '15 at 06:39
  • 1
    This: `JFrame frame = new JFrame("Hello World!");` should be this: `MainFrameframe = new MainFrame("Hello World!");` – npinti Aug 04 '15 at 06:40
  • Hi, welcome to SO. Please provide a short description of the problem: "*it doesn't work*" doesn't describe *what* doesn't work, what is the actual behaviour and what you expected to happen. – BackSlash Aug 04 '15 at 06:41

5 Answers5

3

You never create an instance of MainFrame, instead, you simply create an instance of JFrame, which is not the same thing.

Instead, consider trying something like...

SwingUtilities.invokeLater(new Runnable() {
    public void run(){
        MainFrame frame = new MainFrame("Hello World!");
        frame.setSize(500, 400);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
});

As a general rule of thumb, it's discouraged to extend directly from a top level container like JFrame, you're not adding any new functionality to the class and are just limiting yourself into a single use case.

Normally, it's better to extend from something like a JPanel, that way you can add it to what ever container you like

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
2

Instead of using JFrame frame = new JFrame("Hello World!"); You should use MainFrame frame = new MainFrame("Hello World!") because you are adding all the components like textarea and buttons in MainFrame.

sol4me
  • 15,233
  • 5
  • 34
  • 34
2

instead of this

JFrame frame = new JFrame("Hello World!");

do this:

MainFrame frame = new MainFrame("Hello World!");

You never create MainFrame instance, so there wont be any components.

You used simple JFrame, which contains no components by default

maskacovnik
  • 3,080
  • 5
  • 20
  • 26
2

You need to create an instance of MainFrame. Change your declaration like:

 JFrame frame = new MainFrame("Hello World!");
Masudul
  • 21,823
  • 5
  • 43
  • 58
0

You need to create the instance of Class and also you have to set size for and location by set Bounds method .

public class Main Frame extends J Frame {
private static final long serialVersionUID = 1L;
   public MainFrame(String title) {
    super(title);
    setLayout(new BorderLayout());
    JTextArea textArea = new JTextArea("test");
    textArea.setBounds(10, 10, 50, 20);
    JButton button = new JButton("Click me");
    button.setBounds(10, 20, 50, 20);
    Container c = getContentPane();
    c.add(textArea, BorderLayout.CENTER);
    c.add(button, BorderLayout.SOUTH);
}

public static void main(String args[]) {
    MainFrame frame = new MainFrame("Title");
    frame.setSize(500, 400);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}
shubhs
  • 170
  • 6
  • 1
    *"..and also you have to set size for and location by set Bounds method ."* No! Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Aug 04 '15 at 07:29