2013-12-21 22 views
0

其实我创建了有两个按钮(星系和iPhone)一个Swing GUI的,我创建了2 Java类手动设计表,当我做这个方法新iPhoneTable ().setVisible(true)对于那个按钮,我没有回答,另一方面,我试图将该按钮与另一个图形用户界面连接起来,它可以正常工作。
这是我的主程序,我拿了重要的代码。 如何使行动JButton的移动另一个帧

 
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); }

+0

显示你的努力? –

+0

你有一个名为'iPhoneTable()'的框架吗? –

+0

不,iPhoneTable是类名称,该帧被称为f –

回答

0

尝试和运行这个例子。我有三节课。主要的是iPhone和Galaxy类。我创建的主界面既iPhone的对象,银河和按钮设置自己的知名度为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); 

    } 
} 

编辑: 你的代码做什么都没有。

  • 你的方法签名不覆盖public void actionPerformed()
  • 您还没有注册任何按钮的监听器。

编辑2:从我的GUI生成器完整的代码。工作正常

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); 

    } 
} 
+0

我做了这个私人无效jButton3ActionPerformed(java.awt.event.ActionEvent evt)iPheezy.setVisible(true);}问题仍然没有解决 –

+0

我的代码和你的代码有什么不同?你在主类中创建了iPhone对象吗?你运行我的代码?这对我来说可以。 –

+0

也许你想发布你的代码。如果我能看到一些代码,它会更容易帮助。 –

相关问题