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

مشكلة في الـ JComboBox

بدأه vector_ever في 14 يوليو 2011 · 3 رد · 384 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم

اثناء عملي باحد البرامج واجهتني مشكلة حتى الآن لم اعرف حلها في الـ JComboBox

تتلخص المشكلة في انه عند اختياري اي خيار يتم توليد الحدث المناسب واحصل على النتيجة التي اريدها ولكن ليس فقط للخيار انماايضا لباقي الحيارات

الكود تجدونه هنا ويرجى تجربة كل خيار مرتين او 3 مرات اقل شي وسترون المشكلة التي كنت اعنيها حيث انه مثلا لدى اختياري الخيارالاول احصل على النتيجة التابعة للخيار الاول واذا اخترت الخيار الثاني احصل على نتيجة الخيار الاول والثاني واذا اخترت الخيار الثالث احصل على الاول والثاني والثالث فارجو المساعدة

import java.awt.Dimension;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.IOException;


import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class JComboBoxTest extends JFrame implements ItemListener {

	private String[] numbers = {"Choose number to display", "10", "100", "1000"};
	private JComboBox chart = new JComboBox(numbers);

	public static void main(String[] args) throws IOException {

	JComboBoxTest frame = new JComboBoxTest();
	frame.setSize(250, 100);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}

	public JComboBoxTest() {
		JPanel p = new JPanel();
        p.add(chart);
        chart.setSelectedIndex(0);
        chart.setPreferredSize(new Dimension(200, 40));

     // add combo box to the frame
    	getContentPane().add(p);

    	// Register listeners
    	chart.addItemListener(this);
	}

public void itemStateChanged(ItemEvent e) {
	String ItemSelectd = (String) e.getItem();

	if (e.getSource() instanceof JComboBox) {
		if ("10".equals (ItemSelectd))
	    {
			JOptionPane.showMessageDialog(null, "the number is ten",
	        		null,JOptionPane.INFORMATION_MESSAGE);
	    }

		if ("100".equals (ItemSelectd))
	    {
			JOptionPane.showMessageDialog(null, "the number is hundred",
	        		null,JOptionPane.INFORMATION_MESSAGE);
	    }

		if ("1000".equals (ItemSelectd))
	    {
			JOptionPane.showMessageDialog(null, "the number is thousend",
	        		null,JOptionPane.INFORMATION_MESSAGE);
	    }
	}

}}

إذا نظــرتَ نيـوب اللـّيـثِ بـارزةً         فـلا تظــنَنَ أَنَّ اللـّيـــثَ يبْتسـِـــمُ


 


 


رسم المخططات البيانية بواسطة الجافا


 


كتاب تحليل وتصميم نظم المعلومات


#2

فقط أضف الشرط if (e.getStateChange() == ItemEvent.SELECTED)

import java.awt.Dimension;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.IOException;


import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class JComboBoxTest extends JFrame implements ItemListener {

	private String[] numbers = {"Choose number to display", "10", "100", "1000"};
	private JComboBox chart = new JComboBox(numbers);

	public static void main(String[] args) throws IOException {

		JComboBoxTest frame = new JComboBoxTest();
		frame.setSize(250, 100);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}

	public JComboBoxTest() {
		JPanel p = new JPanel();
		p.add(chart);
		chart.setSelectedIndex(0);
		chart.setPreferredSize(new Dimension(200, 40));

		// add combo box to the frame
		getContentPane().add(p);

		// Register listeners
		chart.addItemListener(this);
	}

	public void itemStateChanged(ItemEvent e) {
		if (e.getStateChange() == ItemEvent.SELECTED) {
			String ItemSelectd = (String) e.getItem();

			if (e.getSource() instanceof JComboBox) {
				if ("10".equals(ItemSelectd)) {
					JOptionPane.showMessageDialog(null, "the number is ten",
									null, JOptionPane.INFORMATION_MESSAGE);
				}

				if ("100".equals(ItemSelectd)) {
					JOptionPane.showMessageDialog(null, "the number is hundred",
									null, JOptionPane.INFORMATION_MESSAGE);
				}

				if ("1000".equals(ItemSelectd)) {
					JOptionPane.showMessageDialog(null, "the number is thousend",
									null, JOptionPane.INFORMATION_MESSAGE);
				}
			}
		}
	}
}
1
#3

ودا حل اخر باستخدم الـ ActionListener

تفضل

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class JComboBoxTest extends JFrame implements ActionListener {

	private String[] numbers = {"Choose number to display", "10", "100", "1000"};
	private JComboBox chart = new JComboBox(numbers);

	public static void main(String[] args) throws IOException {

    	JComboBoxTest frame = new JComboBoxTest();
    	frame.setSize(250, 100);
    	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	frame.setLocationRelativeTo(null);
    	frame.setVisible(true);
	}

	public JComboBoxTest() {
    	JPanel p = new JPanel();
    	p.add(chart);
    	chart.setSelectedIndex(0);
    	chart.setPreferredSize(new Dimension(200, 40));

    	// add combo box to the frame
    	getContentPane().add(p);

    	// Register listeners
    	chart.addActionListener(this);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
    	String item = (String) chart.getSelectedItem();
    	if (numbers[1].equals(item)) {
        	JOptionPane.showMessageDialog(null, "the number is ten",
                	null, JOptionPane.INFORMATION_MESSAGE);
    	} else if (numbers[2].equals(item)) {
        	JOptionPane.showMessageDialog(null, "the number is hundred",
                	null, JOptionPane.INFORMATION_MESSAGE);
    	} else if (numbers[3].equals(item)) {
        	JOptionPane.showMessageDialog(null, "the number is thousend",
                	null, JOptionPane.INFORMATION_MESSAGE);
    	}
	}
}

تم تعديل هذه المشاركة بواسطة Mahmoud Kelany في 14 يوليو 2011 في 20:25

1

Software Developer
Mahmoudkelany.com


 

#4

أو قم باستخدام ActionListener، وكل ما عليك تغييره هو:

chart.addActionListener(this);

public void actionPerformed(ActionEvent e)
    {
        if(((String) ((JComboBox) e.getSource()).getSelectedItem()).equals("10"))
        { 
            JOptionPane.showMessageDialog(null, "the number is ten", null,JOptionPane.INFORMATION_MESSAGE); 
        } 
        else if(((String) ((JComboBox) e.getSource()).getSelectedItem()).equals("100"))
        { 
            JOptionPane.showMessageDialog(null, "the number is hundred", null,JOptionPane.INFORMATION_MESSAGE); 
        } 
        else if(((String) ((JComboBox) e.getSource()).getSelectedItem()).equals("1000"))
        { 
            JOptionPane.showMessageDialog(null, "the number is thousend", null,JOptionPane.INFORMATION_MESSAGE); 
        }
    }
1
597657.png

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