Java 如何使jbutton移动另一帧
实际上,我创建了一个Swing GUI,它有两个按钮galaxy和iPhone,我创建了两个java类来手动设计表,当我使用这个方法new iPhoneTable.setVisibletrue时,我没有得到任何答案,另一方面,我尝试将该按钮与另一个Swing GUI连接,它工作正常。 这是我的主程序,我接受了重要的代码。 public class MainFrame7 extends javax.swing.JFrame {Java 如何使jbutton移动另一帧,java,swing,action,jbutton,Java,Swing,Action,Jbutton,实际上,我创建了一个Swing GUI,它有两个按钮galaxy和iPhone,我创建了两个java类来手动设计表,当我使用这个方法new iPhoneTable.setVisibletrue时,我没有得到任何答案,另一方面,我尝试将该按钮与另一个Swing GUI连接,它工作正常。 这是我的主程序,我接受了重要的代码。 public class MainFrame7 extends javax.swing.JFrame { iPhoneTable iPheezy = new iPhoneT
iPhoneTable iPheezy = new iPhoneTable();
GalaxyTable galxy = new GalaxyTable();
public MainFrame7() {
initComponents();}
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
iPheezy.setVisible(true);
}
试着运行这个例子。我有三节课。主要的是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);
}
}
编辑:
你是说代码什么都不做
您的方法签名不会覆盖已执行的公共无效操作
您尚未使用任何按钮注册侦听器。
编辑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);
}
}
你有一个名为iPhoneTable的框架吗?没有,iPhoneTable是类名,框架名为fso。你必须在当前类中创建该框架的对象,然后调用。你的意思是这样的吗!JF帧=新的iPhoneTable;我做了这个私有的void jButton3ActionPerformedjava.awt.event.ActionEvent evt{iPheezy.setVisibletrue;}问题仍然没有解决我的代码和你的代码有什么区别?您是否在主类中创建了iPhone对象?你运行我的代码了吗?这对我来说很好。也许你想发布你的代码。如果我能看到一些代码,我会更容易提供帮助。您是否从侦听器中执行的public void action调用了该方法?编辑您的帖子。你不需要代表来做那件事。在所有标记下,您将看到一个编辑链接