-4

次の3つのクラスがあります。

-OnePa (public static void main() を含む) -新規 -同じパッケージの下を選択します。

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

2 に答える 2

0

ほとんどの場合、のメソッドまたはコンストラクターからChoose.javaです。

main()これは、Javaの他のメソッドと同じですが、1つだけ例外があります。実行時に、アプリケーションへのエントリポイントを見つけるためにjvmによって使用されます。

したがって、からフィールドにアクセスする方法を知っている場合は、フィールド自体またはそのアクセサのいずれかがアクセス可能であれば、他の方法から同じアプローチを使用して変数にアクセスmain()できる可能性があります

于 2013-03-03T17:54:58.390 に答える
0

Choose.java呼び出された でメソッドを作成しpublic void setName(String name)、次に New.java で呼び出すときにGetValues()、変数 FNm をChoose.java次のように渡しますCT11.setName(FNm)。これで、名前を choose クラスに渡しました。この名前をグローバル変数に格納して、好きなことを行うことができます。

于 2013-03-03T18:26:21.030 に答える