Cześć Wam,
Od jakiegoś czasu zająłem się nauką języka JAVA.
I doszedłem do problemu dla mnie "nie do przejścia", chciałbym by po wybraniu JRadioButtona zmieniał mi się system obliczeń za pomocą komendy "else if" jednak wyskakuje mi błąd i podkreśla. Bez tego obawiam się ze nie zmieni im się sposób obliczania.
poniżej przedstawiam kod:
mport javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public interface Interface {
class Main extends JFrame implements ActionListener {
JButton wyjscie, przelicz;
JLabel LabelPrzywitanie, LabelMetric, LabelQuantity, LabelLength, LabelWidth, LabelHeight, LabelPallet, LabelCbm, LabelCw;
JTextField TextQuantity, TextLength, TextWidth, TextHeight, TextPalletUs, TextCbmUs, TextCwUs, TextPalletEu,TextCbmEu, TextCwEu;
ButtonGroup ButtonsWybor;
JRadioButton RadioUs, RadioEu;
double ObliczenieQuantityUs, OobliczenieLengthUs, ObliczenieWidthUs, OblicznieHeightUs, OoblicznieCbmUs, OblicznieCwUs, ObliczenieQuantityEu, ObliczenieLengthEu, ObliczenieWidthEu, OblicznieHeightEu, OoblicznieCbmEu, OblicznieCwEu;
char Zx = 'x';
public Main() {
setSize(400, 300);
setTitle("Demo");
setLayout(null);
// RADIO BUTTONS
ButtonsWybor = new ButtonGroup();
RadioEu = new JRadioButton("EU", true);
RadioEu.setBounds(150,45,40,20);
ButtonsWybor.add(RadioEu);
add(RadioEu);
RadioEu.addActionListener(this);
RadioEu.setToolTipText("EU metric system");
RadioUs = new JRadioButton("US", false);
RadioUs.setBounds(190,45,50,20);
ButtonsWybor.add((RadioUs));
add(RadioUs);
RadioUs.addActionListener(this);
RadioUs.setToolTipText("US metric system");
//BUTTONS
przelicz = new JButton("Przelicz");
przelicz.setBounds(20, 195, 100, 20);
add(przelicz);
przelicz.addActionListener(this);
wyjscie = new JButton("Wyjście");
wyjscie.setBounds(120, 195, 100, 20);
add(wyjscie);
wyjscie.addActionListener(this);
// LABELS IN
LabelPrzywitanie = new JLabel("Demo");
LabelPrzywitanie.setBounds(100, 10, 300, 30);
add(LabelPrzywitanie);
LabelMetric = new JLabel("Chose metric system:");
LabelMetric.setBounds(20,40,200,30);
add(LabelMetric);
LabelQuantity = new JLabel("Quantity:");
LabelQuantity.setBounds(20, 65, 50, 30);
add(LabelQuantity);
LabelLength = new JLabel("Length:");
LabelLength.setBounds(20, 95, 50, 30);
add(LabelLength);
LabelWidth = new JLabel("Width:");
LabelWidth.setBounds(20, 125, 50, 30);
add(LabelWidth);
LabelHeight = new JLabel("Height:");
LabelHeight.setBounds(20, 155, 50, 30);
add(LabelHeight);
// LABELS OUT
LabelPallet = new JLabel("Pallet in CM:");
LabelPallet.setBounds(165, 65, 70, 30);
LabelCbm = new JLabel("CBM:");
LabelCbm.setBounds(165, 105, 85, 30);
LabelCw = new JLabel("C.W.:");
LabelCw.setBounds(165, 145, 85, 30);
// TEXTFIELDS IN
TextQuantity = new JTextField("");
TextQuantity.setBounds(75, 74, 50, 18);
add(TextQuantity);
TextQuantity.addActionListener(this);
TextQuantity.setToolTipText("Enter quantity");
TextLength = new JTextField("");
TextLength.setBounds(75, 104, 50, 18);
add(TextLength);
TextLength.addActionListener(this);
TextLength.setToolTipText("Enter length of your shipment");
TextWidth = new JTextField("");
TextWidth.setBounds(75, 134, 50, 18);
add(TextWidth);
TextWidth.addActionListener(this);
TextWidth.setToolTipText("Enter width of your shipment");
TextHeight = new JTextField("");
TextHeight.setBounds(75, 164, 50, 18);
add(TextHeight);
TextHeight.addActionListener(this);
TextHeight.setToolTipText("Enter height of your shipment");
// TEXTFIELDS OUT US
TextPalletUs = new JTextField("");
TextPalletUs.setBounds(240, 74, 135, 18);
TextCbmUs = new JTextField("");
TextCbmUs.setBounds(240, 114, 135, 18);
TextCwUs = new JTextField("");
TextCwUs.setBounds(240, 154, 135, 18);
// TEXTFIELDS OUT EU
TextPalletEu = new JTextField("");
TextPalletEu.setBounds(240, 74, 135, 18);
TextCbmEu = new JTextField("");
TextCbmEu.setBounds(240, 114, 135, 18);
TextCwEu = new JTextField("");
TextCwEu.setBounds(240, 154, 135, 18);
}
public static void main(String[] args) {
Main okno = new Main();
okno.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
okno.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e)
{
// PROGRAMOWANIE BUTTONOW
Object zrodlo = e.getSource();
if (zrodlo==wyjscie)
{
dispose();
}
else if (zrodlo==przelicz || zrodlo==TextQuantity || zrodlo==TextLength || zrodlo==TextWidth || zrodlo==TextHeight);
{
repaint();
add(LabelPallet);
add(LabelCbm);
add(LabelCw);
}
if (zrodlo==RadioEu);
{
add(TextPalletEu);
add(TextCbmEu);
add(TextCwEu);
// PROGRAMOWANIE TEXFIELDOW IN EU
ObliczenieQuantityEu = Double.parseDouble(TextQuantity.getText());
ObliczenieLengthEu = Double.parseDouble(TextLength.getText());
ObliczenieWidthEu = Double.parseDouble(TextWidth.getText());
OblicznieHeightEu = Double.parseDouble(TextHeight.getText());
OoblicznieCbmEu = ((ObliczenieLengthEu / 100) * (ObliczenieWidthEu / 100) * (OblicznieHeightEu / 100) * ObliczenieQuantityEu);
OblicznieCwEu = OoblicznieCbmEu * 166.66666666666;
// PROGRAMOWANIE TEXTFIELDOW OUT EU
TextPalletEu.setText(String.valueOf(ObliczenieLengthEu) + Zx + String.valueOf(ObliczenieWidthEu) + Zx + String.valueOf(OblicznieHeightEu));
TextCbmEu.setText(String.valueOf(OoblicznieCbmEu));
TextCwEu.setText(String.valueOf(OblicznieCwEu));
}
if (zrodlo==RadioUs);
{
add(LabelPallet);
add(LabelCbm);
add(LabelCw);
add(TextPalletUs);
add(TextCbmUs);
add(TextCwUs);
// PROGRAMOWANIE TEXTFIELDOW IN US
ObliczenieQuantityUs = Double.parseDouble(TextQuantity.getText());
OobliczenieLengthUs = Double.parseDouble(TextLength.getText());
ObliczenieWidthUs = Double.parseDouble(TextWidth.getText());
OblicznieHeightUs = Double.parseDouble(TextHeight.getText());
OobliczenieLengthUs = OobliczenieLengthUs * 2.54;
ObliczenieWidthUs = ObliczenieWidthUs * 2.54;
OblicznieHeightUs = OblicznieHeightUs * 2.54;
OoblicznieCbmUs = ((OobliczenieLengthUs / 100) * (ObliczenieWidthUs / 100) * (OblicznieHeightUs / 100) * ObliczenieQuantityUs);
OblicznieCwUs = OoblicznieCbmUs * 166.66666666666;
// PROGRAMOWANIE TEXTFIELDOW OUT US
TextPalletUs.setText(String.valueOf(OobliczenieLengthUs) + Zx + String.valueOf(ObliczenieWidthUs) + Zx + String.valueOf(OblicznieHeightUs));
TextCbmUs.setText(String.valueOf(OoblicznieCbmUs));
TextCwUs.setText(String.valueOf(OblicznieCwUs));
}
}
}
}