实际上我创建了一个Swing GUI,它有两个按钮(galaxy和iPhone),我创建了2个 java class 来手动设计表,当我使用 new iPhoneTable()时。 setVisible(true)对于那个按钮我没有得到答案,另一方面我试图将该按钮与另一个摆动GUI连接并且它正常工作。
这是我的主要程序,我拿了重要的代码。
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)
尝试并运行此示例。我有三节课。主要的一个,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);
}
}