Προς το περιεχόμενο

ερώτηση σε java


cyber_katsarida

Προτεινόμενες αναρτήσεις

Δημοσ.

παιδιά κολλάω σ ένα σημείο.

παραθέτω τον κώδικα μου παρακάτω...

θέλω λοιπόν να δίνω έναν αριθμό στο Jtextfield και ανάλογα με ποιο Jradiobutton έχω επιλέξει να γίνεται ο πολλαπλασιασμός μόλις πατάω το κουμπί και να μου εμφανίζεται το αποτέλεσμα.

πχ αν επιλέξω το 1ο radio ο αριθμός στο textfield να πολλαπλασιάζεται με 5.

αν το 2ο με 10....

ξέρω ότι κάτι λείπει στο τέλος με τις if αλλά δυσκολεύομαι λίγο.

καμιά βοήθεια;

 

σκέφτηκα να αλλάξω τις if στο τέλος :

>if(e.getSource()==JRadioButton1 && e.getStateChange()==ItemEvent.SELECTED)

αποτέλεσμα μηδέν.

 

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

public class apol extends JApplet{
private Container cont;
private JRadioButton JRadioButton1,JRadioButton2,JRadioButton3;
private ButtonGroup bg;
private JTextField text1;
private JButton button1;

public void init()
{
	cont=getContentPane();
	cont.setLayout(new FlowLayout ());
	
	JRadioButton1=new JRadioButton("*5",true);
	cont.add(JRadioButton1);
	JRadioButton2=new JRadioButton("*10",false);
	cont.add(JRadioButton2);
	JRadioButton2=new JRadioButton("*15",false);
	cont.add(JRadioButton2);
	bg=new ButtonGroup();
	bg.add(JRadioButton1);
	bg.add(JRadioButton2);
	bg.add(JRadioButton3);
	
	text1=new JTextField(20);
	text1.setText("dwse timi");
	cont.add(text1);
	
	button1=new JButton("polaplasiase");
	Handl h1=new Handl();
	button1.addActionListener(h1);
	cont.add(button1);
}
private class Handl implements ActionListener
{
	public void actionPerformed (ActionEvent e)
	{
		String st1,st2;
		int num;
		st1=text1.getText();
		num=Integer.parseInt(st1);
		double teliko=0;
		
		if(e.getSource()==JRadioButton1)
		teliko=num*5;
	      if(e.getSource()==JRadioButton2)
		teliko=num*10;
		if(e.getSource()==JRadioButton3)
		teliko=num*15;
	
		st2="to apotelesma einai:"+teliko;
	    JOptionPane.showMessageDialog(null,st2);
	}
}
}

Δημοσ.

παιδιά κολλάω σ ένα σημείο.

παραθέτω τον κώδικα μου παρακάτω...

θέλω λοιπόν να δίνω έναν αριθμό στο Jtextfield και ανάλογα με ποιο Jradiobutton έχω επιλέξει να γίνεται ο πολλαπλασιασμός μόλις πατάω το κουμπί και να μου εμφανίζεται το αποτέλεσμα.

πχ αν επιλέξω το 1ο radio ο αριθμός στο textfield να πολλαπλασιάζεται με 5.

αν το 2ο με 10....

ξέρω ότι κάτι λείπει στο τέλος με τις if αλλά δυσκολεύομαι λίγο.

καμιά βοήθεια;

 

σκέφτηκα να αλλάξω τις if στο τέλος :

>if(e.getSource()==JRadioButton1 && e.getStateChange()==ItemEvent.SELECTED)

αποτέλεσμα μηδέν.

 

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

public class apol extends JApplet{
private Container cont;
private JRadioButton JRadioButton1,JRadioButton2,JRadioButton3;
private ButtonGroup bg;
private JTextField text1;
private JButton button1;

public void init()
{
	cont=getContentPane();
	cont.setLayout(new FlowLayout ());
	
	JRadioButton1=new JRadioButton("*5",true);
	cont.add(JRadioButton1);
	JRadioButton2=new JRadioButton("*10",false);
	cont.add(JRadioButton2);
	JRadioButton2=new JRadioButton("*15",false);
	cont.add(JRadioButton2);
	bg=new ButtonGroup();
	bg.add(JRadioButton1);
	bg.add(JRadioButton2);
	bg.add(JRadioButton3);
	
	text1=new JTextField(20);
	text1.setText("dwse timi");
	cont.add(text1);
	
	button1=new JButton("polaplasiase");
	Handl h1=new Handl();
	button1.addActionListener(h1);
	cont.add(button1);
}
private class Handl implements ActionListener
{
	public void actionPerformed (ActionEvent e)
	{
		String st1,st2;
		int num;
		st1=text1.getText();
		num=Integer.parseInt(st1);
		double teliko=0;
		
		if(e.getSource()==JRadioButton1)
		teliko=num*5;
	      if(e.getSource()==JRadioButton2)
		teliko=num*10;
		if(e.getSource()==JRadioButton3)
		teliko=num*15;
	
		st2="to apotelesma einai:"+teliko;
	    JOptionPane.showMessageDialog(null,st2);
	}
}
}

Δημοσ.

Στο if μπορεις να βάλεις JRadioButton1.isSelected() για να δεις αν ειναι επιλεγμένο, αλλα μου πεταει κατι exception τωρα που δεν ξερω γιατι...

edit:

Ο σωστός κωδικας

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

public class apol extends JApplet{
/**
 * 
 */
private static final long serialVersionUID = 1L;
private Container cont;
private JRadioButton JRadioButton1,JRadioButton2,JRadioButton3;
private ButtonGroup bg;
private JTextField text1;
private JButton button1;

public void init()
{
	cont=getContentPane();
	cont.setLayout(new FlowLayout ());
	
	JRadioButton1=new JRadioButton("*5",true);
	cont.add(JRadioButton1);
	JRadioButton2=new JRadioButton("*10",false);
	cont.add(JRadioButton2);
[color="Red"]		JRadioButton3=new JRadioButton("*15",false);
	cont.add(JRadioButton3);[/color] [color="YellowGreen"]//είχες βαλει ξανα το 2 εδω[/color]
	bg=new ButtonGroup();
	bg.add(JRadioButton1);
	bg.add(JRadioButton2);
	bg.add(JRadioButton3);
	
	text1=new JTextField(20);
	text1.setText("dwse timi");
	cont.add(text1);
	
	button1=new JButton("polaplasiase");
	Handl h1=new Handl();
	button1.addActionListener(h1);
	cont.add(button1);
}
private class Handl implements ActionListener
{
	public void actionPerformed (ActionEvent e)
	{
		String st1,st2;
		int num;
		st1=text1.getText();
		num=Integer.parseInt(st1);
		int teliko=0;

		if([color="#ff0000"]JRadioButton1.isSelected()[/color])
		{
			teliko=num*5;
		}
		[color="Red"]else[/color] if([color="#ff0000"]JRadioButton2.isSelected()[/color])
		{
	    	teliko=num*10;
		}
		[color="#ff0000"]else[/color] if([color="#ff0000"]JRadioButton3.isSelected()[/color])
		{
			teliko=num*15;
		}
		
		st2="to apotelesma einai:"+teliko;
	    JOptionPane.showMessageDialog(null,st2);
	}
}
}



Δημοσ.

Στο if μπορεις να βάλεις JRadioButton1.isSelected() για να δεις αν ειναι επιλεγμένο, αλλα μου πεταει κατι exception τωρα που δεν ξερω γιατι...

edit:

Ο σωστός κωδικας

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

public class apol extends JApplet{
/**
 * 
 */
private static final long serialVersionUID = 1L;
private Container cont;
private JRadioButton JRadioButton1,JRadioButton2,JRadioButton3;
private ButtonGroup bg;
private JTextField text1;
private JButton button1;

public void init()
{
	cont=getContentPane();
	cont.setLayout(new FlowLayout ());
	
	JRadioButton1=new JRadioButton("*5",true);
	cont.add(JRadioButton1);
	JRadioButton2=new JRadioButton("*10",false);
	cont.add(JRadioButton2);
[color="Red"]		JRadioButton3=new JRadioButton("*15",false);
	cont.add(JRadioButton3);[/color] [color="YellowGreen"]//είχες βαλει ξανα το 2 εδω[/color]
	bg=new ButtonGroup();
	bg.add(JRadioButton1);
	bg.add(JRadioButton2);
	bg.add(JRadioButton3);
	
	text1=new JTextField(20);
	text1.setText("dwse timi");
	cont.add(text1);
	
	button1=new JButton("polaplasiase");
	Handl h1=new Handl();
	button1.addActionListener(h1);
	cont.add(button1);
}
private class Handl implements ActionListener
{
	public void actionPerformed (ActionEvent e)
	{
		String st1,st2;
		int num;
		st1=text1.getText();
		num=Integer.parseInt(st1);
		int teliko=0;

		if([color="#ff0000"]JRadioButton1.isSelected()[/color])
		{
			teliko=num*5;
		}
		[color="Red"]else[/color] if([color="#ff0000"]JRadioButton2.isSelected()[/color])
		{
	    	teliko=num*10;
		}
		[color="#ff0000"]else[/color] if([color="#ff0000"]JRadioButton3.isSelected()[/color])
		{
			teliko=num*15;
		}
		
		st2="to apotelesma einai:"+teliko;
	    JOptionPane.showMessageDialog(null,st2);
	}
}
}



Αρχειοθετημένο

Αυτό το θέμα έχει αρχειοθετηθεί και είναι κλειστό για περαιτέρω απαντήσεις.

  • Δημιουργία νέου...