2013-03-03 61 views
-4

我有三个类别如下:如何从Java中不包含void main的另一个类中获取值?

-OnePa(包含公共静态无效的主要()) - 新 - 选择 在同一个包。

我想 “字符串名称”,这是声明,并从Choose.java存储在New.java

package com.ash; 

    import javax.swing.JFrame; 

    public class New1 extends JFrame { 

/** 
* 
*/ 
private static final long serialVersionUID = 4648531955064052430L; 
private JPanel contentPane; 
private JTextField textField; 
private JTextField textField_1; 
private JTextField textField_3; 
private JFormattedTextField textField_2; 
private JComboBox comboBox; 
String FNm; 
String LNm; 

Connection con; 
Choose CT11=new Choose(); 



//Mask-Formatter is created 
protected MaskFormatter createFormatter(String s){ 
    MaskFormatter formatter=null; 
    try{ 
     formatter=new MaskFormatter(s); 
    }catch(java.text.ParseException exc){ 
     System.err.println("formatter is bad:"+exc.getMessage()); 
     System.exit(-1); 
    } 
    return formatter; 
} 



public New1() { 
    setTitle("New "); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setBounds(100, 100, 585, 415); 
    contentPane = new JPanel(); 
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
    setContentPane(contentPane); 
    contentPane.setLayout(null); 

    JLabel lblNewLabel = new JLabel("First Name"); 
    lblNewLabel.setFont(new Font("Times New Roman", Font.BOLD, 13)); 
    lblNewLabel.setBounds(40, 62, 91, 28); 
    contentPane.add(lblNewLabel); 

    JLabel lblLastName = new JLabel("Last Name"); 
    lblLastName.setFont(new Font("Times New Roman", Font.BOLD, 13)); 
    lblLastName.setBounds(40, 101, 91, 28); 
    contentPane.add(lblLastName); 



    textField = new JTextField(); 
    textField.setBounds(150, 66, 146, 20); 
    contentPane.add(textField); 
    textField.setColumns(10); 

    textField_1 = new JTextField(); 
    textField_1.setColumns(10); 
    textField_1.setBounds(150, 105, 146, 20); 
    contentPane.add(textField_1); 



    JButton btnNewButton = new JButton("Next >>"); 
    btnNewButton.setBounds(150, 289, 89, 23); 
    contentPane.add(btnNewButton); 



    ImageIcon home = new ImageIcon("D:\\Source AP\\home.jpg"); 
    JButton HomeButton = new JButton(home); 
    HomeButton.addMouseListener(new MouseAdapter() { 
     @Override 
     public void mouseClicked(MouseEvent arg0) { 


      textField.setText(null); 
      textField_1.setText(null); 
      textField_2.setText(null); 
      textField_3.setText(null); 

      dispose(); 

     } 
    }); 
    HomeButton.setBounds(26, 289, 45, 38); 
    contentPane.add(HomeButton); 





    btnNewButton.addMouseListener(new MouseAdapter() { 
     @Override 
     public void mouseClicked(MouseEvent e) { 

      //Storing Values in String 
      GetValues(); 

      label.setVisible(false); 
      label_1.setVisible(false); 
      label_2.setVisible(false); 
      label_3.setVisible(false); 
      lblNewLabel_1.setVisible(false); 

      if((FNm.length()==0 || FNm.trim().length()==0)|| 

          (LNm.length()==0 || LNm.trim().length()==0))) 
      { 
       System.out.println("It is blank or contains white 
            spaces"); 

      } 
      else{ 

      System.out.println("Success"); 


       //Connection 

       try { 
       con=DbConnPa.getConObj(); 
      } catch (ClassNotFoundException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } catch (SQLException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } 
      try { 
       DbConnPa.Reg1(con, FNm, LNm, Age, RefDoc, Sex); 
      } catch (SQLException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } 

      CT11.setVisible(true); 

      //hides the current JFrame 
      dispose(); 
      } 

     } 
    }); 

} 
public void GetValues(){ 
    FNm=textField.getText(); 
    LNm=textField_1.getText(); 

} 


    } 

Choose.java的价值:

package com.ash; 

import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.border.EmptyBorder; 
import javax.swing.JCheckBox; 
import javax.swing.JButton; 

import java.awt.EventQueue; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 
import java.awt.Font; 
import javax.swing.JLabel; 

public class Choose extends JFrame { 

/** 
* 
*/ 
private static final long serialVersionUID = 1946720589313613447L; 
private JPanel contentPane; 




int i=0; 

/** 
* Launch the application. 
*/ 

public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      try { 
       Choose frame = new Choose(); 
       frame.setVisible(true); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 
} 

/** 
* Create the frame. 
*/ 

public Choose() { 
    setTitle("Choose "); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setBounds(100, 100, 910, 516); 
    contentPane = new JPanel(); 
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
    setContentPane(contentPane); 
    contentPane.setLayout(null); 

    JButton btnNewButton = new JButton("Next >>"); 
    btnNewButton.setBounds(39, 396, 89, 23); 
    contentPane.add(btnNewButton); 

    JCheckBox checkBox = new JCheckBox("Hello"); 
    checkBox.setFont(new Font("Times New Roman", Font.PLAIN, 13)); 
    checkBox.setBounds(39, 120, 132, 23); 
    contentPane.add(checkBox); 

    JLabel lblFirstName = new JLabel("First Name :"); 
    lblFirstName.setFont(new Font("Times New Roman", Font.BOLD, 13)); 
    lblFirstName.setBounds(39, 11, 82, 16); 
    contentPane.add(lblFirstName); 

    JLabel lblLastName = new JLabel("Last Name :"); 
    lblLastName.setFont(new Font("Times New Roman", Font.BOLD, 13)); 
    lblLastName.setBounds(39, 34, 82, 16); 
    contentPane.add(lblLastName); 

    JLabel Ctname = new JLabel(); 
    Ctname.setBounds(131, 12, 132, 16); 
    contentPane.add(Ctname); 

    JLabel Ctlst = new JLabel(); 
    Ctlst.setBounds(131, 35, 132, 16); 
    contentPane.add(Ctlst); 
    contentPane.add(Ctref); 
    btnNewButton.addMouseListener(new MouseAdapter() { 
     @Override 
     public void mouseClicked(MouseEvent arg0) { 
      //if Chk1 is selected it will return TRUE else FALES. 
      Boolean afb=checkBox.isSelected(); 

      if(afb== true) 
       { 
       System.out.println("Is selected"); 
       } 
     } 
    }); 
} 
      } 
+0

粘贴一些代码 – 2013-03-03 17:52:10

+0

这三个类的代码太长了。 – AshwinK 2013-03-03 17:53:40

+0

这是simple.google它,你可以很容易地找到它。 – 2013-03-03 17:57:13

回答

0

Choose.java称为public void setName(String name)创建方法,那么当你在New.java调用GetValues()传递变量FNM到Choose.java如下:CT11.setName(FNm)。现在你已经把名字传给了choose类,你可以把它存储在一个全局变量中,然后用它来做什么。

+0

CT11.setName(Fm)中的Fm应该在New.java中或在方法public void SetName()中声明? – AshwinK 2013-03-03 18:32:14

+0

我指的是你已经拥有'FNm = textField.getText();'的变量。 – Byron 2013-03-03 18:33:13

+0

我是新来的java所以问这么多问题。对不起 我做到了,但如何在Choose.java中获得该值?我创建了 public void setName(String Ctname){ system.out。的println(Ctname); } 但无法打印它 – AshwinK 2013-03-03 18:37:39

0

最可能来自于Choose.java中的方法或构造函数。

main()就像java中的任何其他方法,除了一件事情 - 在运行时,jvm使用它来查找应用程序的入口点。

所以,如果你知道如何从main()访问现场,你可以最有可能访问使用从给定的任何其他方法相同的做法,即场本身或任何它的存取的变量是accessible

+0

当我在Choose.java中创建New.java对象时,它显示init()的错误; – AshwinK 2013-03-03 17:56:31

+0

@ user1995161,有什么错误? – 2013-03-03 17:58:46

+0

at com.ash.Choose。 (Choose.java:22)这种类型的错误显示接近20-30倍 – AshwinK 2013-03-03 18:00:08

相关问题