如何从另一个Java类调用JFrame
我正在尝试做一个简单的Jform并从另一个类调用它。 我想在服务器客户端应用程序中使用此Jframe,但我不知道如何从另一个类打开JFrame类。
喜欢用户必须选择
1-打开Jframe。
2-退出。
那我做错了什么?
以下是代码:
Jframe类名为Calculas.java
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ /** * * @author user */ public class Calculas extends javax.swing.JFrame { /** * Creates new form Calculas */ public Calculas() { initComponents(); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // private void initComponents() { a1Text = new javax.swing.JTextField(); a2Text = new javax.swing.JTextField(); jLabel1 = new javax.swing.JLabel(); jButton1 = new javax.swing.JButton(); answer = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jButton1.setText("jButton1"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(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() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(a1Text, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(a2Text, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(87, 87, 87) .addComponent(answer, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup() .addGap(113, 113, 113) .addComponent(jLabel1)) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(jButton1))) .addContainerGap(86, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(19, 19, 19) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(a1Text, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(a2Text, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(answer, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(60, 60, 60) .addComponent(jLabel1) .addGap(34, 34, 34) .addComponent(jButton1) .addContainerGap(140, Short.MAX_VALUE)) ); pack(); }// private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { int a; a = Integer.parseInt(a1Text.getText()) + Integer.parseInt(a2Text.getText()); answer.setText("Answer" + a); // TODO add your handling code here: } /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ // /* 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(Calculas.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(Calculas.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(Calculas.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(Calculas.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } // /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new Calculas().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JTextField a1Text; private javax.swing.JTextField a2Text; private javax.swing.JLabel answer; private javax.swing.JButton jButton1; private javax.swing.JLabel jLabel1; // End of variables declaration }
名为Test.java的测试类
public class Test extends JFrame { public static void main(String args[]) { Calculas CAL = new Calculas(); CAL.Calculus(); } }
原谅我,如果这是天真的,因为我不是任何Java程序员…但不仅仅是因为你需要设置可见?
Calculas CAL = new Calculas(); CAL.setVisible(true);
Calculas
的构造函数不显示( setVisible
)框架。
如果您想以这种方式与Calculas
类进行交互,您还应该调用CAL.setVisible(true)
此外,按照惯例,所有Java实例变量都应以小写字符开头并使用camelCase约定
在它的代码的当前forms中,您可以在Test
类中调用Calculas
类的main方法或将代码移动到Test
类。
警告:扩展JFrame不是一个好主意。
第一种选择:您不需要在Test中扩展JFrame
public class Test{ public static void main(String args[]) { Calculas.main(new String[0]); } }
你没有在你的问题中提到你希望如何能够在控制台或其他JFrame
选择关闭或打开…或者……
第二种选择:
但如果我是你,我可以做这样的事情:在一个单独的方法中包装外观和感觉的代码,并从Test
的主要调用它。
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ /** * * @author user */ public class Calculas extends javax.swing.JFrame { /** * Creates new form Calculas */ public Calculas() { initComponents(); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // private void initComponents() { a1Text = new javax.swing.JTextField(); a2Text = new javax.swing.JTextField(); jLabel1 = new javax.swing.JLabel(); jButton1 = new javax.swing.JButton(); answer = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jButton1.setText("jButton1"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(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() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(a1Text, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(a2Text, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(87, 87, 87) .addComponent(answer, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup() .addGap(113, 113, 113) .addComponent(jLabel1)) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(jButton1))) .addContainerGap(86, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(19, 19, 19) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(a1Text, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(a2Text, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(answer, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(60, 60, 60) .addComponent(jLabel1) .addGap(34, 34, 34) .addComponent(jButton1) .addContainerGap(140, Short.MAX_VALUE)) ); pack(); }// private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { int a; a = Integer.parseInt(a1Text.getText()) + Integer.parseInt(a2Text.getText()); answer.setText("Answer" + a); // TODO add your handling code here: } public static setNimbusFeel(){ /* Set the Nimbus look and feel */ // /* 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(Calculas.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(Calculas.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(Calculas.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(Calculas.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } } // Variables declaration - do not modify private javax.swing.JTextField a1Text; private javax.swing.JTextField a2Text; private javax.swing.JLabel answer; private javax.swing.JButton jButton1; private javax.swing.JLabel jLabel1; // End of variables declaration }
和测试类这样:
public class Test{ public static void main(String args[]) { Calculas cal=new Calculas(); // Calculas.setNimbusFeel(); /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new Calculas().setVisible(true); } }); } }
喜欢这个:
以下是您可以使用的基本结构示例:
import java.awt.*; import javax.swing.*; class MyGui { private JFrame window = new JFrame("This is the title"); public MyGui() { initComponents(); window.setBounds(100, 50, 600, 400); //location, size window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.setVisible(true); } public void initComponents() { Container cp = window.getContentPane(); cp.setLayout(new FlowLayout() ); cp.add(new JLabel("Hello world") ); } } public class MyProg { private static void createAndShowGUI() { new MyGui(); } public static void main(String[] args) { //Schedule a job for the event dispatch thread: //creating and showing this application's GUI. SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } }
在单独的文件中:
MyGui.java
import java.awt.*; import javax.swing.*; public class MyGui { private JFrame window = new JFrame("This is the title"); public MyGui() { initComponents(); window.setBounds(100, 50, 600, 400); //location, size window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.setVisible(true); } public void initComponents() { Container cp = window.getContentPane(); cp.setLayout(new FlowLayout() ); cp.add(new JLabel("Hello world") ); } }
MyProg.java
import javax.swing.*; public class MyProg { private static void createAndShowGUI() { new MyGui(); } public static void main(String[] args) { //Schedule a job for the event dispatch thread: //creating and showing this application's GUI. SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } }
Calculas CAL = new Calculas(); CAL.Calculus();
也许, CAL.Calculas();
我不懂java,但我意识到,对不起我的英语:)
- Apache Mina SFTP服务器侧通道侦听器用于传入文件
- 字符串变量中的\ t和\ n不会显示在JOptionPane.showMessageDialog中
- 使用Integer.parseInt转换32位二进制字符串失败
- 无法从java Double转换为java Date
- 如何计算字符串中char的出现次数?
- 我在Hadoop中收到此错误,找不到可执行文件null \ bin \ winutils.exe
- Hibernate中FlushMode.AUTO和FlushMode.ALWAYS之间的区别?
- 使用流,如何在HashMap中映射值?
- reflection:通过reflection加载的类中的常量变量