【问题标题】:how to make action for jbutton to move another frame如何使jbutton动作以移动另一帧
【发布时间】:2013-12-21 06:45:39
【问题描述】:

实际上我创建了一个 Swing GUI,它有两个按钮(galaxy 和 iPhone),当我执行此方法 new iPhoneTable() 时,我创建了 2 个 java 类 来手动设计表格。 setVisible(true) 对于那个按钮我没有得到回答,另一方面我试图将该按钮与另一个摆动 GUI 连接,它可以正常工作。
这是我的主要程序,我采用了重要代码。

公共类 MainFrame7 扩展 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); }

【问题讨论】:

  • 你有一个名为iPhoneTable()的框架吗?
  • 不,iPhoneTable是类名,框架叫f
  • 所以你必须在当前类中创建该框架的对象然后调用。
  • 你的意思是这样的! JFrame f = new iPhoneTable();

标签: java swing action jbutton


【解决方案1】:

尝试运行此示例。我有三个班。主要的是 iPhone 和 Galaxy 类。我在主 GUI 中创建了一个 iPhone 和 Galaxy 的对象,并且按钮将它们的可见性设置为 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 Builder 的完整代码。工作正常

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

    }
}

【讨论】:

  • 我做了这个 private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) { iPheezy.setVisible(true);}问题仍然没有解决
  • 我的代码和你的有什么区别?您是否在主类中创建了 iPhone 对象?你运行我的代码了吗?它对我来说很好。
  • 也许你想发布你的代码。如果我能看到一些代码会更容易提供帮助。
  • 您是否从您的侦听器中的public void actionPerformed() 调用了该方法?
  • 编辑您的帖子。你不需要代表这样做。在所有标签下,您会看到一个编辑链接
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多