7

For school I had to make a JFrame and within that One button and Two textfields. Whatever you put in Textfield one have to get into textfield two when the button is pressed. I got the code to the point I should see the textfields and the button when i run the program. For whatever reason it doesn't.

My come so far:

package helloworld;

import javax.swing.*;
import java.awt.event.*; 

    public class HelloWorld extends JFrame {

        public static void main(String[] args) {

             JFrame frame = new HelloWorld();
             frame.setSize(400, 400);
             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             frame.setTitle("Hello World Button App");

             JPanel panel = new JPanel();
             frame.setContentPane(panel);
             fram.setVisible(true);
         }
       }

       class panel extends JPanel {

          public JButton btn1 = new JButton("Klick!");
          public JTextField txt1 = new JTextField(10);
          public JTextField txt2 = new JTextField(10);

          public panel() {
               add(btn1);
               add(txt1);
               add(txt2);
            }
          }

I am not yet allowed to post images but I will provide a link to the picture down here

I am sorry if this question allready exests but i couldnt's find a similar question. I am new to programming so please dont yell at me when I forgot something or wrote something wrong in it!

Dan D.
  • 32,246
  • 5
  • 63
  • 79
  • fram.setVisible(true); should be frame.setVisible.. – Koray Tugay Feb 03 '13 at 12:03
  • 1
    Don't extend `JFrame`. I have yet to see a case where it makes sense to do so. Jut create an instance and add the GUI to it. – Andrew Thompson Feb 03 '13 at 12:22
  • @AndrewThompson I think it makes sense when defining our owm components? May be! – joey rohan Feb 03 '13 at 13:50
  • @joeyrohan Challenge. Show me a convincing SSCCE where the only way to achieve the effect is to extend `JFrame`. The answer will be considered invalid if I can reproduce the effect shown (and it is not simply a 'dumb' effect), *without* extending frame. – Andrew Thompson Feb 03 '13 at 15:03
  • @AndrewThompson for sure you can reproduce the same effect.And i also know its a dumb thing to extend `JFrame`.But suppose if creating multiple instances, like new `ClassExtendsJFrame().setVisible(true);` // first window `new ClassExtendsJFrame().setVisible(true);` won't it be just little easy? – joey rohan Feb 03 '13 at 15:16
  • @joeyrohan This is ironic. My two top voted answers 1) [show multiple frames](http://stackoverflow.com/questions/7143287/how-to-best-position-swing-guis/7143398#7143398) (without extending frame) & [a case against using multiple frames](http://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-bad-practice/9554657#9554657). ;) But in brief, no I don't really see the advantage of extending frame over (for example) implementing a factory method to stamp them out (as many frames as needed). – Andrew Thompson Feb 03 '13 at 15:42
  • @AndrewThompson agree with those links :) – joey rohan Feb 03 '13 at 15:57
  • @AndrewThompson what if I extend JFrame with an abstract interface and make it as a skeleton structure ? Applicable – joey rohan Aug 18 '14 at 07:18
  • @joeyrohan What (methods) does the interface declare? – Andrew Thompson Aug 18 '14 at 07:36
  • @AndrewThompson Think it as an adapter or regular design patterns.Or some virtual methods . – joey rohan Aug 18 '14 at 09:02

3 Answers3

7

Here i have modified your code a bit, but did in a similar way. I won't extend JFrame until and unless i don't want to do something creative, but you always CAN.

You had already extended JFrame , so no worth of calling methods with frame.foo() but simply foo() , and most important JFrame frame = new HelloWorld() , will make no sense, if you have already extended you class with JFrame:

enter image description here

import javax.swing.*;

public class HelloWorld extends JFrame{

 public static void main(String[] args) {

          SwingUtilities.invokeLater(new Runnable() {
          public void run() {
          new HelloWorld().setVisible(true);
        }
    });

     }
    public  HelloWorld()
    {

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("Hello World Button App");

        panel pan= new panel();
        add(pan.panel);
        pack();
        setVisible(true);
    }
   }

   class panel {

      private JButton btn1 = new JButton("Klick!");
      private JTextField txt1 = new JTextField(10);
      private JTextField txt2 = new JTextField(10);
      JPanel panel;
      public panel() {
           panel = new JPanel();
           panel.add(btn1);
           panel.add(txt1);
           panel.add(txt2);
        }
      }

Also, you can also extend your panel class with JPanel :

 public  HelloWorld()
    {

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("Hello World Button App");

          panel pan= new panel();
         add(pan);
         pack();
         setVisible(true);
    }
   }

   class panel extends JPanel {

      private JButton btn1 = new JButton("Klick!");
      private JTextField txt1 = new JTextField(10);
      private JTextField txt2 = new JTextField(10);

      public panel() {

           add(btn1);
           add(txt1);
           add(txt2);
        }
      }
joey rohan
  • 3,505
  • 5
  • 33
  • 70
  • 1
    Please learn java naming conventions and stick to them And extending JSomething is nearly always suboptimal, they are meant to be used as-are. – kleopatra Feb 03 '13 at 14:34
  • You know why I did the extending and stuff like that? It was becouse the teacher on school learned me it that way :D I will try it thanks for you reply! – Tom Schillemans Feb 03 '13 at 14:45
  • @kleopatra Yeah, very true.Just dint changed OP's code basic code. – joey rohan Feb 03 '13 at 14:56
  • 2
    @TomSchillemans As suggested by - kleopatra, "_extending JSomething is nearly always suboptimal, they are meant to be used as-are_" , you should tell this to your teacher, make use of JSomething as objects whenever possible, i din't changed the concept of your code, cause of your convenience, and don't forget the naming-conventions.Good Luck! – joey rohan Feb 03 '13 at 15:00
  • :) +1 for *"I won't extend JFrame until and unless i don't want to do something creative"* ;) – Andrew Thompson Feb 03 '13 at 15:06
  • @joeyrohan I replaced your code with mine but it gave me even more errors! not more but a other and bigger one I meant. Is it for some reason possible java installed on my virtual machine on wich I wanted to make the program is not installed corectly? cus if I try to make the same program with the same features in a design project it runs just fine!! Even when I add the eventhandler its works like it should be! – Tom Schillemans Feb 03 '13 at 15:42
  • another thing is all of my classmates did it the way the teacher taught us to do it. And on all of them it worked except for me xD – Tom Schillemans Feb 03 '13 at 15:46
  • @TomSchillemans the above EX works fine for me, thats why have given a screen shot.What is your exception? – joey rohan Feb 03 '13 at 15:56
  • I was derping a but with my previous error xD put the wrong thing in the setContentPane I guess. I followed Dan's tips. But now I got a Uncompilable source code - Erroneoud ctor sym type: error. – Tom Schillemans Feb 03 '13 at 16:19
  • @joeyrohan Sorry i post much and probebly anyoing you guys but i tried it one more time with you code and it works now :D tranks for helping me alltho I anoyed you! – Tom Schillemans Feb 03 '13 at 16:39
  • @TomSchillemans not a problem! But please tell your teacher!! okay? – joey rohan Feb 03 '13 at 17:06
  • @joeyrohan I will :D See what he thinks of it :D – Tom Schillemans Feb 03 '13 at 17:15
4

You declared a class called panel that you are not using anywhere. Please replace the line bwlow:

JPanel panel = new JPanel();

with:

SomePanel panel = new SomePanel();

Then, your class panel becomes SomePanel to follow correct class naming.

Some thoughts to help you:

  • Name your classes following the Java style
  • Don't use public fields
  • Set layouts on your panels. This time it worked for you as the default is FlowLayout.
Dan D.
  • 32,246
  • 5
  • 63
  • 79
4

That is because your class name is panel not JPanel

Modify this:

panel panel = new panel();
frame.setContentPane(panel);
frame.setVisible(true);

You should try to use names for your Class that are not so confusing, and try to declare them with uppercase.

Example:

Class Panel extends JPanel {}

Object:

Panel panel = new Panel()

Here you can clearly read which one is the class name and which is the object (instance of that class) of that class.

Mythul
  • 1,807
  • 7
  • 34
  • 53
  • 1
    Op really shouldnt be extending `JPanel` if simply adding components – David Kroukamp Feb 03 '13 at 13:06
  • 1
    @joeyrohan I would suggest you refresh your OO basics a bit :) – kleopatra Feb 03 '13 at 14:59
  • When I tried this and tried to run it it gave me a Exception: `Exception in thread "Main" Java.awt.IllegalComponentStateException: contentPane cannot be set to null.` I have renamed all the Variables and in Netbeans itselfs it doesnt give any warnings only when I run the program. – Tom Schillemans Feb 03 '13 at 15:01
  • @joeyrohan which OO basic text did you consult? If it doesn't handle the ... basics (as to what/how a class is and when/not to extend), look into another :-) – kleopatra Feb 03 '13 at 15:08
  • @kleopatra i got this from pages of [Oracle](http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html) "`The idea of inheritance is simple but powerful: When you want to create a new class and there is already a class that includes some of the code that you want, you can derive your new class from the existing class. In doing this, you can reuse the fields and methods of the existing class without having to write (and debug!) them yourself`" Still i am not getting your point. – joey rohan Feb 03 '13 at 15:29