loving java
loving java

Reputation: 37

how to make action for jbutton to move another frame

actually I created one Swing GUI which has two buttons (galaxy and iPhone), and I created 2 java class to design tables manually, when I did this method new iPhoneTable().setVisible(true) for that button I got no answer,on the other hand I tried to connect that button with another swing GUI and it works properly.
this my main program,I took the Important codes.

public class MainFrame7 extends javax.swing.JFrame {

iPhoneTable iPheezy = new iPhoneTable(); GalaxyTable galxy = new GalaxyTable(); public MainFrame7() { initComponents();} private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) { iPheezy.setVisible(true); }

Upvotes: 0

Views: 1655

Answers (1)

Paul Samsotha
Paul Samsotha

Reputation: 208964

Try and run this example. I have three class. The main one, the iPhone and the Galaxy classes. I create an object of both iPhone and Galaxy in the main GUI and the buttons set their visibility to true

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class Test10 extends JPanel {

    JButton galaxy = new JButton("Galaxy");
    JButton iPhone = new JButton("iPhone");

    iPhone iPheezy = new iPhone();
    Galaxy galxy = new Galaxy();

    public Test10() {
        add(galaxy);
        add(iPhone);

        galaxy.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                galxy.setVisible(true);

            }
        });
        iPhone.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                iPheezy.setVisible(true);

            }
        });
    }

    public static void createAndShowGui() {
        JFrame frame = new JFrame();
        frame.add(new Test10());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.pack();
        frame.setVisible(true);

    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGui();
            }
        });
    }
}

class iPhone extends JFrame {

    public iPhone() {
        setSize(300, 300);
        setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        setLocationByPlatform(true);

    }
}

class Galaxy extends JFrame {

    public Galaxy() {
        setSize(300, 300);
        setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        setLocationByPlatform(true);

    }
}

Edit: You're code does nothing at all.

  • Your method signature doesn't override the public void actionPerformed()
  • You haven't registered a listener with any button.

Edit 2: Full code from my GUI Builder. Works fine

import javax.swing.JFrame;


public class MainFrame7 extends javax.swing.JFrame {
    iPhone iPheezy = new iPhone();
    Galaxy galxy = new Galaxy();

    public MainFrame7() {
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

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

        jButton2.setText("Galaxy");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(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(81, 81, 81)
                .addComponent(jButton1)
                .addGap(51, 51, 51)
                .addComponent(jButton2)
                .addContainerGap(123, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(118, 118, 118)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap(156, Short.MAX_VALUE))
        );

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

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        iPheezy.setVisible(true);
    }                                        

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        galxy.setVisible(true);
    }                                        

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MainFrame7.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MainFrame7.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MainFrame7.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MainFrame7.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MainFrame7().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    // End of variables declaration                   
}

class iPhone extends JFrame {

    public iPhone() {
        setSize(300, 300);
        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        setLocationByPlatform(true);

    }
}

class Galaxy extends JFrame {

    public Galaxy() {
        setSize(300, 300);
        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        setLocationByPlatform(true);

    }
}

Upvotes: 0

Related Questions