Good Day!

My professor in object oriented programming wants us to collect a JFrame form to a class.... he told us the code but it did not work...Hope you may help me...and thanks in advance

import javax.swing.JOptionPane;



public class Firm
{
   //----------------------------------------------------------
   //  Creates a staff of employees for a firm and pays them.
   //----------------------------------------------------------
   public static void main (String[] args)
   {
      JOptionPane.showMessageDialog(null,"**=======TUP-Manila IT Corporation=======**");
      
      Staff personnel = new Staff();

      personnel.payday();
   }
}

And this is my JFrame

public class MyForm extends javax.swing.JFrame {

    /** Creates new form MyForm */
    public MyForm() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jButton1.setText("jButton1");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(154, 154, 154)
                .addComponent(jButton1)
                .addContainerGap(173, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(209, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addGap(68, 68, 68))
        );

        pack();
    }// </editor-fold>

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        
       
        MyForm Firm =new MyForm();
        Firm.setVisible(true);
        //This is what our professor told us to do but it did not work 
        // TODO add your handling code here:
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new MyForm().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    // End of variables declaration
}

Recommended Answers

All 21 Replies

it did not work

Can you explain? Post error message here.

Can you explain? Post error message here.

It will not really work because there are still 6 other classes that you should have in one package(supposed to be it should be 7 if you count Firm.java as well)....the only problem in my program is how can i link my JFrame to my superclass Firm.java
My professor said the the code is

MyForm Firm =new MyForm();
Firm.setVisible(true);

which is in lines 51 and 52..but it did not open the firm.java

By the way, I am still a newbie here in java and in this forum..hope you may help me,tnx ^^

Are you sure it didn't open it?
It looks like your button handler creates a new MyForm and makes it visible exactly like you do in main(...) - so the new MyForm will be identical to the existing one and sit exactly on top of it, so it looks like nothing has happened (maybe?)
Try dragging the form somewhere else on the screen...

It will not really work

Where there error messages? Please post them here.
Please explain what you do and what the program does that "does not work".

Are you sure it didn't open it?
It looks like your button handler creates a new MyForm and makes it visible exactly like you do in main(...) - so the new MyForm will be identical to the existing one and sit exactly on top of it, so it looks like nothing has happened (maybe?)
Try dragging the form somewhere else on the screen...

Yeah, that is what happening to my program...it overlaps the other and I cannot see my Firm.java, what should I do?

Where there error messages? Please post them here.
Please explain what you do and what the program does that "does not work".

Uhmm...for shorter codes. I made another one and still that is the problem... "How to link my JFrame to my other class" Here it is...

Lesson1.java

import javax.swing.*;
public class Lesson1 {
    public static void main(String[]args){
        String ans;
        ans=JOptionPane.showInputDialog(null,"Spped in miles per hour");
        double mph=Double.parseDouble(ans);
        double kph =1.621*mph;
        JOptionPane.showMessageDialog(null, "Kph = "+kph);
        System.exit(0);
    }
    
}

MyFrame

public class MyForm extends javax.swing.JFrame {

    /** Creates new form MyForm */
    public MyForm() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jButton1.setText("jButton1");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(154, 154, 154)
                .addComponent(jButton1)
                .addContainerGap(173, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(209, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addGap(68, 68, 68))
        );

        pack();
    }// </editor-fold>                        

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        
        MyForm Lesson1 =new MyForm();
        Lesson1.setVisible(true); 

        // TODO add your handling code here:
    }                                        

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new MyForm().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    // End of variables declaration                   
}

the problem... "How to link my JFrame to my other class"

Can you explain what your are trying to do?
What do you mean by "link"?
Do you want the code in the JFrame class to send data to the other class?
Or do you want the other class to display the JFrame?
Or ???

Is there supposed to be a relation between the class Lesson1 and the variable Lesson1?
BTW variable names should start with lowercase letters: lesson1

Can you explain what your are trying to do?
What do you mean by "link"?
Do you want the code in the JFrame class to send data to the other class?
Or do you want the other class to display the JFrame?
Or ???

Is there supposed to be a relation between the class Lesson1 and the variable Lesson1?
BTW variable names should start with lowercase letters: lesson1

Actually the class Lesson1 and the variable Lesson1 are the same(for me)...what I am trying to do is after I press the ok button(jbutton)in JFrame it will go directly to other class which is the Lesson1.java but what happen to my program is that it does not go to my class Lesson1.

Our prof told us that our class name should start at uppercase and since I am trying to link(connect) the class Lesson1 from JFrame I should use its class name.

Your actionlistener method creates a new MyForm object and puts its address in the Lesson1 variable. It does not create an object of the Lesson1 class. To create an object of the Lesson1 class it needs to use:
Lesson1 lesson1Obj = new Lesson1();
NOTE: Variable names should start with lowercase.

In your code there is NO connection between the variable: Lesson1 and the class: Lesson1

Your actionlistener method creates a new MyForm object and puts its address in the Lesson1 variable. It does not create an object of the Lesson1 class. To create an object of the Lesson1 class it needs to use:
Lesson1 lesson1Obj = new Lesson1();
NOTE: Variable names should start with lowercase.

In your code there is NO connection between the variable: Lesson1 and the class: Lesson1

I still cant fix it...
BTW...when Im trying to create a variable...like Bark....it still works.

I still cant fix it...

Please explain and show the error messages you are getting.

Please explain and show the error messages you are getting.

can i post screenshots here..and how? XD

If you execute at a command prompt you can copy the screen.
To copy the contents of the command prompt window:
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'

Paste here.

I am pretty sure he is trying to use integration.
Like your professor said. In your ActionListener for your JButton

when you wrote

MyForm Firm = new MyForm();

You made an object out of your super class(the class your gui is in) and named if Firm. So when you called Firm it only brought back up your gui witch was already there.
You need to instead make your class Firm() into an object.

Firm someName = new Firm();

If you execute at a command prompt you can copy the screen.
To copy the contents of the command prompt window:
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'

Paste here.

We don't use the command prompt because we have the import java.swing.*; The cmd cant print it... :)

I am pretty sure he is trying to use integration.
Like your professor said. In your ActionListener for your JButton

when you wrote

MyForm Firm = new MyForm();

You made an object out of your super class(the class your gui is in) and named if Firm. So when you called Firm it only brought back up your gui witch was already there.
You need to instead make your class Firm() into an object.

Firm someName = new Firm();

I did try what you are saying but it still did not work :(

Please post your latest code. Given that you asked this question 17 days ago there must been some changes to code, so we do not want to code something that you will come back moaning that you do not know how to use. Secondly it will show that you actually been trying to work on it and not just simply waiting for solution from us.

The name of your Jframe is MyForm.
You have to declare a object of MyForm type as ROVY has said....

MyForm Firm =new MyForm();
    Firm.setVisible(true);

in the class " Firm ".

Then it will looks like........

public class Firm
{
//----------------------------------------------------------
// Creates a staff of employees for a firm and pays them.
//----------------------------------------------------------
public static void main (String[] args)
{
JOptionPane.showMessageDialog(null,"**=======TUP-Manila IT Corporation=======**");
 
Staff personnel = new Staff();
 
personnel.payday();
MyForm Firm =new MyForm();
    Firm.setVisible(true);
}
}

OK......

The name of your Jframe is MyForm.
You have to declare a object of MyForm type as ROVY has said....

MyForm Firm =new MyForm();
    Firm.setVisible(true);

in the class " Firm ".

OK......

Thanks for the help...now I know where I have mistakes...i should place the

MyForm Firm =new MyForm();
    Firm.setVisible(true);

in my class Firm...thanks again...but there is another problem....when i run it..they both appear together...

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */



import javax.swing.JOptionPane;



public class Firm extends MyForm
{
   //----------------------------------------------------------
   //  Creates a staff of employees for a firm and pays them.
   //----------------------------------------------------------
   public static void main (String[] args)
   {
      MyForm Firm =new MyForm();
      Firm.setVisible(true);
      int value1=value;
             
      if(value1==1)
      {
      
      Staff personnel = new Staff();
      personnel.payday();
        }
      else
          System.exit(0);
     
   }
}
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MyForm.java
 *
 * Created on Jul 26, 2011, 2:01:20 AM
 */
/**

public class MyForm extends javax.swing.JFrame {

    /** Creates new form MyForm */
    public MyForm() {
        
        initComponents();
        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        button = new javax.swing.JButton();
        button1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        button.setText("ok");
        button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonActionPerformed(evt);
            }
        });

        button1.setText("cancel");
        button1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(154, 154, 154)
                .addComponent(button)
                .addContainerGap(203, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(254, Short.MAX_VALUE)
                .addComponent(button1)
                .addGap(83, 83, 83))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(175, Short.MAX_VALUE)
                .addComponent(button1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(button)
                .addGap(68, 68, 68))
        );

        pack();
    }// </editor-fold>

    private void buttonActionPerformed(java.awt.event.ActionEvent evt) {                                       
        int haha=1;   
        int value=haha;
    }                                      

    private void button1ActionPerformed(java.awt.event.ActionEvent evt) {
       System.exit(0);// TODO add your handling code here:
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new MyForm().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JButton button;
    private javax.swing.JButton button1;
    // End of variables declaration
}

I would like to pass the variable value in MyForm to value1 in Firm...so that i can control them...but the variable value is in private what should I do?

commented: Hi i want to ask. In the JFrame must put the this code?? MyForm Firm =new MyForm(); Firm.setVisible(true); +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.