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

ربط الكمبو بوكس بمربع النص

بدأه ~PuNiShEr~ في 23 يوليو 2011 · 4 رد · 421 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم

اخواني ممكن احد يفيدني بطريقة ربط مربع الكتابه بالكنمبو بوكس يعني مثلا لما المستخدم يدخل قيمه بعدها في الكمبو بوكس يتم اختيار دولار ام يورو وبعدها يتم ظغط البوتون وبعدها يطبع النتيجه

وهذا الكود

public class NewJFrame extends javax.swing.JFrame {

    public NewJFrame() {
        initComponents();
    }


    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jComboBox1 = new javax.swing.JComboBox();
        jTextField1 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });

        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });

        jButton1.setText("jButton1");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel1.setText("jLabel1");

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(57, 57, 57)
                        .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 72, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jComboBox1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(jButton1))
                    .add(layout.createSequentialGroup()
                        .add(165, 165, 165)
                        .add(jLabel1)))
                .addContainerGap(63, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(29, 29, 29)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jComboBox1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jButton1))
                .add(42, 42, 42)
                .add(jLabel1)
                .addContainerGap(183, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
    }

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
    }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new NewJFrame().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration
}
#2

تفضل

public class NewJFrame extends javax.swing.JFrame {

	public NewJFrame() {
    	initComponents();
	}

	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

    	jComboBox1 = new javax.swing.JComboBox();
    	jTextField1 = new javax.swing.JTextField();
    	jButton1 = new javax.swing.JButton();
    	jLabel1 = new javax.swing.JLabel();

    	setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    	jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[]{"Dolar", "URO", "EG"}));
    	jComboBox1.addActionListener(new java.awt.event.ActionListener() {

        	public void actionPerformed(java.awt.event.ActionEvent evt) {
            	jComboBox1ActionPerformed(evt);
        	}
    	});

    	jTextField1.addActionListener(new java.awt.event.ActionListener() {

        	public void actionPerformed(java.awt.event.ActionEvent evt) {
            	jTextField1ActionPerformed(evt);
        	}
    	});

    	jButton1.setText("Show Result");
    	jButton1.addActionListener(new java.awt.event.ActionListener() {

        	public void actionPerformed(java.awt.event.ActionEvent evt) {
            	jButton1ActionPerformed(evt);
        	}
    	});

    	jLabel1.setText("Result");

    	org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
    	getContentPane().setLayout(layout);
    	layout.setHorizontalGroup(
            	layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING).add(layout.createSequentialGroup().add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING).add(layout.createSequentialGroup().add(57, 57, 57).add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 72, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE).addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED).add(jComboBox1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE).addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED).add(jButton1)).add(layout.createSequentialGroup().add(165, 165, 165).add(jLabel1))).addContainerGap(63, Short.MAX_VALUE)));
    	layout.setVerticalGroup(
            	layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING).add(layout.createSequentialGroup().add(29, 29, 29).add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE).add(jComboBox1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE).add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE).add(jButton1)).add(42, 42, 42).add(jLabel1).addContainerGap(183, Short.MAX_VALUE)));

    	pack();
	}// </editor-fold>

	private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {
    	// TODO add your handling code here:
	}

	private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
    	// TODO add your handling code here:
	}

	private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    	jLabel1.setText(jTextField1.getText() + " " + jComboBox1.getSelectedItem());
	}

	/**
 	* @param args the command line arguments
 	*/
	public static void main(String args[]) {
    	java.awt.EventQueue.invokeLater(new Runnable() {

        	public void run() {
            	new NewJFrame().setVisible(true);
        	}
    	});
	}
	// Variables declaration - do not modify
	private javax.swing.JButton jButton1;
	private javax.swing.JComboBox jComboBox1;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JTextField jTextField1;
	// End of variables declaration
}

Software Developer
Mahmoudkelany.com


 

#3

عملت لك مثال بسيط:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class NewJFrame extends JFrame implements ActionListener
{
    private JComboBox combo;
    private JTextField input, output;
    private JButton button;
    public static void main(String[] args)
    {
        new NewJFrame();
    }    
    public NewJFrame()
    {
        super("العنوان");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(500,100);

        String[] currency = {"ريال","جنيه","دولار","درهم", "ليرة"};
        JPanel panel = new JPanel();
        combo = new JComboBox(currency);
        input = new JTextField(10);
        output = new JTextField(10);
        output.setEditable(false);
        button = new JButton("اضغط هنا");
        button.setActionCommand("button");
        button.addActionListener(this);

        panel.add(combo);
        panel.add(input);
        panel.add(button);
        panel.add(output);
        add(panel);

        setVisible(true);
    }
    public void actionPerformed(ActionEvent e)
    {
        if(e.getActionCommand().equals("button"))
        {
            output.setText(input.getText() + " " + combo.getSelectedItem());
        }
    }
}
597657.png
#4

مشكوووووورين ياخوان على الرد

ولكن كيف يتم اضافة الشرط لان اللي انتو حاطينه يطبع الشي اللي انا اخترته

وانا ابي اضع شرط لما يختار دولار مثلا يتم تنفيذ اكواد معينه

ويعطيكم الف عافيه

#5

يبدو انك لا تعلم المزيد عن الـ GUI

يمكنك النظر لذلك

http://download.oracle.com/javase/tutorial/uiswing/components/combobox.html

لكى تعلم الكثير عن ComBox وان كنت تريد المزيد عن غيرها ستجده هناك

Software Developer
Mahmoudkelany.com


 

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