الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

مشكل في حدث keypressed في JTextField

بدأه lovely_ned في 19 نوفمبر 2010 · 2 رد · 473 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام غليكم عيد مبارك للجميع تقبل الله منا ومنكم وغفر الله لنا ولكم

هذا هو الكود الذي استخدمه

import javax.swing.JDialog;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JTextField;
import java.awt.Rectangle;
import java.awt.Font;
import javax.swing.JLabel;
import java.awt.ComponentOrientation;
public class Essai {

	private JDialog jDialog = null;  //  @jve:decl-index=0:visual-constraint="184,56"
	private JPanel jContentPane = null;
	private JTextField Price = null;
	private JTextField Result = null;
	private JLabel jLabel = null;
	private JLabel jLabel1 = null;

	/**
	 * This method initializes jDialog	
	 * 	
	 * @return javax.swing.JDialog	
	 */
	private JDialog getJDialog() {
		if (jDialog == null) {
			jDialog = new JDialog();
			jDialog.setSize(new Dimension(546, 201));

			jDialog.setContentPane(getJContentPane());
			jDialog.setVisible(true);
		}
		return jDialog;
	}

	/**
	 * This method initializes jContentPane	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jLabel1 = new JLabel();
			jLabel1.setBounds(new Rectangle(35, 89, 102, 16));
			jLabel1.setText("Result:");
			jLabel1.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
			jLabel = new JLabel();
			jLabel.setBounds(new Rectangle(31, 43, 99, 16));
			jLabel.setText("Price:");
			jLabel.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(getPrice(), null);
			jContentPane.add(getResult(), null);
			jContentPane.add(jLabel, null);
			jContentPane.add(jLabel1, null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes Price	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getPrice() {
		if (Price == null) {
			Price = new JTextField();
			Price.setBounds(new Rectangle(182, 45, 231, 21));
			Price.setFont(new Font("Calibri", Font.BOLD, 14));

			Price.addKeyListener(new java.awt.event.KeyAdapter() {   
				public void keyPressed(java.awt.event.KeyEvent e) {    
					int a = 20 - Integer.parseInt(getPrice().getText());
					getResult().setText((""+a).toString());
				}

			});

		}
		return Price;
	}

	/**
	 * This method initializes Result	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getResult() {
		if (Result == null) {
			Result = new JTextField();
			Result.setBounds(new Rectangle(184, 88, 233, 21));
			Result.setFont(new Font("Calibri", Font.BOLD, 14));
		}
		return Result;
	}


	public static void main(String[] args) {
		new Essai().getJDialog();

		}

}

اريد لما اكتب في price تطلع النتيجة مباشرة في result وهي 20 - price

لكن المشكلة انه النتيجة لا تظهر الا اذا ضغطت على مرتين على الكيبورد

يعني لما اكتب 1 لا تظهر النتيجة ولما اضغط زر اخر عندها تاتي النتيجة 19

اين المشكل

#2

استخدام الـ keyPressed غلط فى حاله الاستماع الى تغيرات الكتابه

انما الصح اهوو

import javax.swing.JDialog;
import javax.swing.JPanel;
import java.awt.Dimension;
import javax.swing.JTextField;
import java.awt.Rectangle;
import java.awt.Font;
import javax.swing.JLabel;
import java.awt.ComponentOrientation;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

public class Essai {

	private JDialog jDialog = null;  //  @jve:decl-index=0:visual-constraint="184,56"
	private JPanel jContentPane = null;
	private JTextField Price = null;
	private JTextField Result = null;
	private JLabel jLabel = null;
	private JLabel jLabel1 = null;

	/**
 	* This method initializes jDialog
 	*
 	* @return javax.swing.JDialog
 	*/
	private JDialog getJDialog() {
    	if (jDialog == null) {
        	jDialog = new JDialog();
        	jDialog.setSize(new Dimension(546, 201));

        	jDialog.setContentPane(getJContentPane());
        	jDialog.setVisible(true);
    	}
    	return jDialog;
	}

	/**
 	* This method initializes jContentPane
 	*
 	* @return javax.swing.JPanel
 	*/
	private JPanel getJContentPane() {
    	if (jContentPane == null) {
        	jLabel1 = new JLabel();
        	jLabel1.setBounds(new Rectangle(35, 89, 102, 16));
        	jLabel1.setText("Result:");
        	jLabel1.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        	jLabel = new JLabel();
        	jLabel.setBounds(new Rectangle(31, 43, 99, 16));
        	jLabel.setText("Price:");
        	jLabel.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        	jContentPane = new JPanel();
        	jContentPane.setLayout(null);
        	jContentPane.add(getPrice(), null);
        	jContentPane.add(getResult(), null);
        	jContentPane.add(jLabel, null);
        	jContentPane.add(jLabel1, null);
    	}
    	return jContentPane;
	}

	/**
 	* This method initializes Price
 	*
 	* @return javax.swing.JTextField
 	*/
	private JTextField getPrice() {
    	if (Price == null) {
        	Price = new JTextField();
        	Price.setBounds(new Rectangle(182, 45, 231, 21));
        	Price.setFont(new Font("Calibri", Font.BOLD, 14));
        	Price.getDocument().addDocumentListener(new DocumentListener() {

            	public void insertUpdate(DocumentEvent e) {
                	getResultFromPrice();
            	}

            	public void removeUpdate(DocumentEvent e) {
                	getResultFromPrice();
            	}

            	public void changedUpdate(DocumentEvent e) {
                	getResultFromPrice();
            	}
        	});
    	}
    	return Price;
	}

	private void getResultFromPrice() {
    	try {
        	int a = 20 - Integer.parseInt(getPrice().getText());
        	getResult().setText(("" + a).toString());
    	} catch (Exception e) {
    	}

	}

	/**
 	* This method initializes Result
 	*
 	* @return javax.swing.JTextField
 	*/
	private JTextField getResult() {
    	if (Result == null) {
        	Result = new JTextField();
        	Result.setBounds(new Rectangle(184, 88, 233, 21));
        	Result.setFont(new Font("Calibri", Font.BOLD, 14));
    	}
    	return Result;
	}

	public static void main(String[] args) {
    	new Essai().getJDialog();
	}
}

بالتوفيق :)

Software Developer
Mahmoudkelany.com


 

#3

شكرا ليك و ميرسي

مواضيع مشابهة