Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Button and textfield don't show up in Java

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!

like image 212
Tom Schillemans Avatar asked Feb 03 '13 11:02

Tom Schillemans


3 Answers

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);
        }
      }
like image 190
joey rohan Avatar answered Oct 19 '22 03:10

joey rohan


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.

like image 4
Mythul Avatar answered Oct 19 '22 03:10

Mythul


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.
like image 4
Dan D. Avatar answered Oct 19 '22 01:10

Dan D.