
a programme which displays a combobox which contains num from 1 to 10 and calculates the factorial of dt num which user clicks.............

Hi Friend,
Try this,
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class JComboBoxEx extends JFrame
{
JComboBoxEx(){
setLayout(null);
JLabel lab1=new JLabel("Select");
final JComboBox combo=new JComboBox();
combo.addItem("--Select--");
for(int i=1;i<=10;i++){
combo.addItem(i);
}
JLabel lab2=new JLabel("Factorial");
final JTextField text=new JTextField(20);
combo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
JComboBox comboBox = (JComboBox) event.getSource();
String st= combo.getSelectedItem().toString();
int m=Integer.parseInt(st);
long num=m;
for(int i=m;i>1;i--){
num=num*(i-1);
}
text.setText(Long.toString(num));
}
});
lab1.setBounds(10,10,100,20);
combo.setBounds(150,10,100,20);
lab2.setBounds(10,40,100,20);
text.setBounds(150,40,100,20);
add(lab1);
add(combo);
add(lab2);
add(text);
setVisible(true);
setSize(300,100);
}
public static void main(String[] args)
{
new JComboBoxEx();
}
}
Thanks
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.