Java Swing, radiobuttony wykluczające się

0

Hey ;)
Mam mały problem z prostą aplikacją w Java (To mój początek więc wszelkie porady itd. mile widziane)
dodam tylko że wszystko działa poprawnie, czyli tekst się zmienia.
chodzi oto że po zaznaczeniu jednej z opcji poprzednie się nie odznaczają i wygląda to tak :
screenshot-20200607110502.png

Kod:


import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

public class main extends JFrame implements ActionListener{
	
	private JLabel lcelcjusza, lfarenheita;
	private JTextField tcelcjusza, tfarenheita;
	private ButtonGroup bgRozmiar;
	private JRadioButton rbMały, rbŚredni, rbDuży;
	private JButton boblicz, bwyjscie;
	double tempcelcjusza, tempfarenheita;
	
	
	public main() {
		setSize(450, 450);
		setTitle("Pierwszy program");
		setLayout(null);
		
		lcelcjusza = new JLabel("Stopnie Celcjusza: ");
		lcelcjusza.setBounds(20, 20, 150, 20);
		add(lcelcjusza);
		
		lfarenheita = new JLabel("Stopnie farenheita: ");
		lfarenheita.setBounds(20, 100, 150, 20);
		add(lfarenheita);
		
		tcelcjusza = new JTextField();
		tcelcjusza.setBounds(200, 20, 150, 20);
		add(tcelcjusza);
		tcelcjusza.addActionListener(this);
		tcelcjusza.setToolTipText("Podaj temperature w stopniach celcjusza");
		
		tfarenheita = new JTextField();
		tfarenheita.setBounds(200, 100, 150, 20);
		add(tfarenheita);
		tfarenheita.addActionListener(this);
		
		bwyjscie = new JButton("Wyjście");
		bwyjscie.setBounds(20, 250, 100, 80);
		add(bwyjscie);
		bwyjscie.addActionListener(this);
		
		boblicz = new JButton("Oblicz");
		boblicz.setBounds(220, 250, 100, 80);
		add(boblicz);
		boblicz.addActionListener(this);
		
		bgRozmiar = new ButtonGroup();
		rbMały = new JRadioButton("Mały", true);
		rbMały.setBounds(50, 150, 100, 20);
		bgRozmiar.add(rbMały);
		add(rbMały);
		rbMały.addActionListener(this);
		
		bgRozmiar = new ButtonGroup();
		rbŚredni = new JRadioButton("Średni" , false);
		rbŚredni.setBounds(150, 150, 100, 20);
		bgRozmiar.add(rbŚredni);
		add(rbŚredni);
		rbŚredni.addActionListener(this);
		
		
		bgRozmiar = new ButtonGroup();
		rbDuży = new JRadioButton("Duży", false);
		rbDuży.setBounds(250, 150, 100, 20);
		bgRozmiar.add(rbDuży);
		add(rbDuży);
		rbDuży.addActionListener(this);


	}
	
	
	public static void main (String[] args) {
		main apkaMain = new main();
		apkaMain.setVisible(true);
		apkaMain.setDefaultCloseOperation(EXIT_ON_CLOSE);
		
	}


	public void actionPerformed(ActionEvent e) {
		Object objekt = e.getSource();
		if(objekt == bwyjscie) {
			dispose();
		}
		if(objekt == boblicz || objekt == tcelcjusza ) {
			tempcelcjusza = Double.parseDouble(tcelcjusza.getText());
			tempfarenheita = 32.0 + 9/5 * tempcelcjusza;
			tfarenheita.setText(String.valueOf(tempfarenheita));
		}else if(objekt == rbMały){
			tfarenheita.setFont(new Font("Sans serif" , Font.BOLD, 10));
			tfarenheita.setForeground(Color.red);
			
		}else if(objekt == rbŚredni){
			tfarenheita.setFont(new Font("Sans serif" , Font.BOLD, 12));
			tfarenheita.setForeground(Color.gray);
			
		}else if(objekt == rbDuży){
			tfarenheita.setFont(new Font("Sans serif" , Font.BOLD, 14));
			tfarenheita.setForeground(Color.blue);	
		}
	}
	
	
}






















Tak wiem, wszystko powinno być po angielsku, jednak tak jak wspomniałem dopiero raczkuje :)
Z góry dzięki za podpowiedzi
Miłej niedzieli :)

3

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

public class main extends JFrame implements ActionListener{

    private JLabel lcelcjusza, lfarenheita;
    private JTextField tcelcjusza, tfarenheita;
    private ButtonGroup bgRozmiar;
    private JRadioButton rbMały, rbŚredni, rbDuży;
    private JButton boblicz, bwyjscie;
    double tempcelcjusza, tempfarenheita;

    public main() {
        setSize(450, 450);
        setTitle("Pierwszy program");
        setLayout(null);
        


        lcelcjusza = new JLabel("Stopnie Celcjusza: ");
        lcelcjusza.setBounds(20, 20, 150, 20);
        add(lcelcjusza);

        lfarenheita = new JLabel("Stopnie farenheita: ");
        lfarenheita.setBounds(20, 100, 150, 20);
        add(lfarenheita);

        tcelcjusza = new JTextField();
        tcelcjusza.setBounds(200, 20, 150, 20);
        add(tcelcjusza);
        tcelcjusza.addActionListener(this);
        tcelcjusza.setToolTipText("Podaj temperature w stopniach celcjusza");

        tfarenheita = new JTextField();
        tfarenheita.setBounds(200, 100, 150, 20);
        add(tfarenheita);
        tfarenheita.addActionListener(this);

        bwyjscie = new JButton("Wyjście");
        bwyjscie.setBounds(20, 250, 100, 80);
        add(bwyjscie);
        bwyjscie.addActionListener(this);

        boblicz = new JButton("Oblicz");
        boblicz.setBounds(220, 250, 100, 80);
        add(boblicz);
        boblicz.addActionListener(this);

        bgRozmiar = new ButtonGroup();
        rbMały = new JRadioButton("Mały", true);
        rbMały.setBounds(50, 150, 100, 20);
        bgRozmiar.add(rbMały);
        add(rbMały);
        rbMały.addActionListener(this);

        bgRozmiar = new ButtonGroup();
        rbŚredni = new JRadioButton("Średni" , false);
        rbŚredni.setBounds(150, 150, 100, 20);
        bgRozmiar.add(rbŚredni);
        add(rbŚredni);
        rbŚredni.addActionListener(this);

        bgRozmiar = new ButtonGroup();
        rbDuży = new JRadioButton("Duży", false);
        rbDuży.setBounds(250, 150, 100, 20);
        bgRozmiar.add(rbDuży);
        add(rbDuży);
        rbDuży.addActionListener(this);
        
        ButtonGroup bg = new ButtonGroup();
        bg.add(rbDuży);
        bg.add(rbMały);
        bg.add(rbŚredni);

    }

    public static void main (String[] args) {
        main apkaMain = new main();
        apkaMain.setVisible(true);
        apkaMain.setDefaultCloseOperation(EXIT_ON_CLOSE);

    }

    public void actionPerformed(ActionEvent e) {
        Object objekt = e.getSource();
        if(objekt == bwyjscie) {
            dispose();
        }
        if(objekt == boblicz || objekt == tcelcjusza ) {
            tempcelcjusza = Double.parseDouble(tcelcjusza.getText());
            tempfarenheita = 32.0 + 9/5 * tempcelcjusza;
            tfarenheita.setText(String.valueOf(tempfarenheita));
        }else if(objekt == rbMały){
            tfarenheita.setFont(new Font("Sans serif" , Font.BOLD, 10));
            tfarenheita.setForeground(Color.red);

        }else if(objekt == rbŚredni){
            tfarenheita.setFont(new Font("Sans serif" , Font.BOLD, 12));
            tfarenheita.setForeground(Color.gray);

        }else if(objekt == rbDuży){
            tfarenheita.setFont(new Font("Sans serif" , Font.BOLD, 14));
            tfarenheita.setForeground(Color.blue);  
        }
    }

}

Działa :)

0

Dziękuje slicznie, do zamknięcia :)

1 użytkowników online, w tym zalogowanych: 0, gości: 1