简体   繁体   English

我不能使用JTextField

[英]I cant use JTextField

I have a problem with JTextField. 我对JTextField有问题。 I will type in my form name, surname, phone_number and email. 我将输入表单名称,姓氏,电话号码和电子邮件。 But I don't know how. 但是我不知道如何。 Actually System.out.print prints 4 times null . 实际上System.out.print打印4次null Method .getText() return unknow source. 方法.getText()返回.getText()源。

This is my code: 这是我的代码:

package View;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

import Date.DaneDoKontaktow;
import Date.KolekcjaKontaktow;
import Main.main;
import Model.OperacjeKontakty;

public class DodajKontakt extends JFrame implements ActionListener {

public JButton dodaj;
public JButton anuluj;
public JTextField s_imie;
public JTextField s_nazwisko;
public JTextField s_telefon;
public JTextField s_email;
private OperacjeKontakty operacje = new OperacjeKontakty();

public DodajKontakt(){
    super("Dodawanie kontaktu");
    setSize(300,300);
    setLocationRelativeTo(null);
    setLayout(null);
    setResizable(false);
    setVisible(true);

    dodaj = new JButton("Dodaj");
    dodaj.setBounds(50, 220, 100, 30);
    add(dodaj);

    anuluj = new JButton("Anuluj");
    anuluj.setBounds(165, 220, 100, 30);
    add(anuluj);

    DaneDoKontaktow dane = new DaneDoKontaktow();

    JLabel imie = new JLabel(dane.imie);
    imie.setBounds(15,30,70,15);
    add(imie);

    JLabel nazwisko = new JLabel(dane.nazwisko);
    nazwisko.setBounds(15,60,70,20);
    add(nazwisko);

    JLabel telefon = new JLabel(dane.telefon);
    telefon.setBounds(15,90,70,20);
    add(telefon);

    JLabel email = new JLabel(dane.email);
    email.setBounds(15,120,70,20);
    add(email);

    anuluj.addActionListener(this);
    anuluj.setActionCommand("klik_anuluj");

    JTextField s_imie = new JTextField();
    s_imie.setBounds(100,20,150,30);
    add(s_imie);

    JTextField s_nazwisko = new JTextField();
    s_nazwisko.setBounds(100,52,150,30);
    add(s_nazwisko);

    JTextField s_telefon = new JTextField();
    s_telefon.setBounds(100,85,150,30);
    add(s_telefon);

    JTextField s_email = new JTextField();
    s_email.setBounds(100,117,150,30);
    add(s_email);

    dodaj.addActionListener(this);
    dodaj.setActionCommand("dodaj");

}

public void actionPerformed(ActionEvent e) {

    if( e.getActionCommand().equals("klik_anuluj")){
        this.dispose();
    }
    if (e.getActionCommand().equals("dodaj")){
        System.out.print(s_imie);
        System.out.print(s_nazwisko);
        System.out.print(s_telefon);
        System.out.print(s_email);
        //operacje.dodaj(s_imie.getText(), s_nazwisko.getText(), s_email.getText(), s_telefon.getText());
        this.dispose();
    }
}
}

You are re-declaring your instance variables inside the constructor. 您正在内部构造函数中重新声明实例变量。 When you do 当你做

JTextField s_imie = new JTextField();

you are creating a local variable, so the instance one won't be affected. 您正在创建局部变量,因此实例1不会受到影响。

How can you solve it? 你怎么解决呢? Don't declare them again, just initialize: 无需再次声明它们,只需初始化即可:

s_imie = new JTextField();

you should do the same with the other variables. 您应该对其他变量执行相同的操作。

you are overriding the visibility of your Attibutes 您将超越您的Attibutes的能见度

public JTextField s_imie;
public JTextField s_nazwisko;
public JTextField s_telefon;
public JTextField s_email;

Instead of : 代替 :

JTextField s_imie = new JTextField();
s_imie.setBounds(100,20,150,30);
add(s_imie);

use this to initialize your declared variable instead of creating a new one: 使用它来初始化您声明的变量,而不是创建一个新的变量:

s_imie = new JTextField();
s_imie.setBounds(100,20,150,30);
add(s_imie);

Afterwards use the following to print out the content: 然后,使用以下命令打印内容:

System.out.print(s_imie.getText());

Do this for all your variables. 对所有变量执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM