-4

我有以下三个课程:

-OnePa (它包含 public static void main()) -New -Choose 在同一个包下。

我想 从 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();

}


     }

选择.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");
                }
        }
    });
}
          }
4

2 回答 2

0

很可能来自Choose.java.

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

因此,如果您知道如何从 访问该字段main(),则很可能使用与任何其他方法相同的方法访问该变量,前提是该字段本身或其任何访问器都是可访问的。

于 2013-03-03T17:54:58.390 回答
0

Choose.java在called中创建一个方法public void setName(String name),然后在 New.java 中调用时GetValues()将变量 FNm 传递给Choose.java如下CT11.setName(FNm):现在您已将名称传递给选择类,您可以将其存储在全局变量中并随心所欲地使用它。

于 2013-03-03T18:26:21.030 回答