Witam! Jestem osobą początkującą, jeśli chodzi o programowanie. Byłbym bardzo wdzięczny, jeśli jakaś osoba podpowiedziała mi, jak mógłbym uprościć/skrócić ten projekt. Z mojej strony, chciałbym jeszcze spytać, w jaki sposób mógłbym sprawdzić, co jest wpisywane w pole tekstowe i zarazem to weryfikować (tekst czy liczba).
Może mógłbym również przenieść część projektu do innego pliku.
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Main extends JFrame implements ActionListener {
//Functions
private void From()
{
from1.setVisible(false);
from2.setVisible(false);
from3.setVisible(false);
from4.setVisible(false);
to1.setVisible(true);
to2.setVisible(true);
to3.setVisible(true);
to4.setVisible(true);
}
private void To()
{
to1.setVisible(false);
to2.setVisible(false);
to3.setVisible(false);
to4.setVisible(false);
first_field.setVisible(true);
second_field.setVisible(true);
first_unit.setText(name1);
second_unit.setText(name2);
first_unit.setVisible(true);
second_unit.setVisible(true);
convert.setVisible(true);
back.setBounds(10,210,150,20);
}
private void Back()
{
dispose();
Main Main_Window = new Main();
Main_Window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Main_Window.setVisible(true);
}
private void Properties()
{
weight.setVisible(false);
area.setVisible(false);
length.setVisible(false);
exit.setVisible(false);
back.setVisible(true);
from1.setVisible(true);
from2.setVisible(true);
from3.setVisible(true);
from4.setVisible(true);
}
//Variables
long result;
private double first_number;
private double second_number;
private double unit_from;
private double unit_to;
private int choice;
private String name1;
private String name2;
//Buttons
private JButton weight;
private JButton area;
private JButton length;
private JButton exit;
private JButton back;
private JButton to1;
private JButton to2;
private JButton to3;
private JButton to4;
private JButton from1;
private JButton from2;
private JButton from3;
private JButton from4;
private JButton convert;
//TextFields
private TextField first_field;
private TextField second_field;
//JLabels
private JLabel first_unit;
private JLabel second_unit;
private Main() {
//Window Parameters
setSize(300, 300);
setTitle("Unit Converter");
setLayout(null);
// Defining Buttons
weight = new JButton("Weight");
area = new JButton("Area");
length = new JButton("Length");
exit = new JButton("Exit");
back = new JButton("Back");
to1 = new JButton("");
to2 = new JButton("");
to3 = new JButton("");
to4 = new JButton("");
from1 = new JButton("");
from2 = new JButton("");
from3 = new JButton("");
from4 = new JButton("");
convert = new JButton("Convert");
weight.setBounds(10, 10, 120, 20);
area.setBounds(10, 50, 120, 20);
length.setBounds(10, 90, 120, 20);
exit.setBounds(10,130,120,20);
back.setBounds(10,170,150,20);
to1.setBounds(10,10,150,20);
to2.setBounds(10,50,150,20);
to3.setBounds(10,90,150,20);
to4.setBounds(10,130,150,20);
from1.setBounds(10,10,150,20);
from2.setBounds(10,50,150,20);
from3.setBounds(10,90,150,20);
from4.setBounds(10,130,150,20);
convert.setBounds(10,170,150,20);
add(weight);
add(area);
add(length);
add(exit);
add(back);
add(to1);
add(to2);
add(to3);
add(to4);
add(from1);
add(from2);
add(from3);
add(from4);
add(convert);
weight.setVisible(true);
area.setVisible(true);
length.setVisible(true);
exit.setVisible(true);
back.setVisible(false);
to1.setVisible(false);
to2.setVisible(false);
to3.setVisible(false);
to4.setVisible(false);
from1.setVisible(false);
from2.setVisible(false);
from3.setVisible(false);
from4.setVisible(false);
convert.setVisible(false);
weight.addActionListener(this);
area.addActionListener(this);
length.addActionListener(this);
exit.addActionListener(this);
back.addActionListener(this);
to1.addActionListener(this);
to2.addActionListener(this);
to3.addActionListener(this);
to4.addActionListener(this);
from1.addActionListener(this);
from2.addActionListener(this);
from3.addActionListener(this);
from4.addActionListener(this);
convert.addActionListener(this);
// Defining TextFields
first_field = new TextField(10);
second_field = new TextField(10);
first_field.setBounds(10, 50,120,20);
second_field.setBounds(10, 120,120,20);
add(first_field);
add(second_field);
first_field.setVisible(false);
second_field.setVisible(false);
first_field.addActionListener(this);
second_field.addActionListener(this);
// Defining JLabels
first_unit = new JLabel("");
second_unit = new JLabel("");
first_unit.setBounds(10, 25, 200,20);
second_unit.setBounds(10,95,200,20);
add(first_unit);
add(second_unit);
first_unit.setVisible(false);
second_unit.setVisible(false);
}
public static void main(String[] args) {
Main Main_Window = new Main();
Main_Window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Main_Window.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e)
{
Object action = e.getSource();
if (action == weight )
{
from1.setText("from grams");
from2.setText("from decagrams");
from3.setText("from kilograms");
from4.setText("from tons");
Properties();
choice = 1;
}
else if (action == area)
{
from1.setText("from millimeters^2");
from2.setText("from centimeters^2");
from3.setText("from metres^2");
from4.setText("from kilometres^2");
Properties();
choice = 2;
}
else if (action == length)
{
from1.setText("from millimetres ");
from2.setText("from centimetres");
from3.setText("from metres");
from4.setText("from kilometres");
Properties();
choice = 3;
}
else if (action == exit)
{
dispose();
}
else if (action == back)
{
Back();
}
else if (action == from1) {
switch (choice) {
case 1:
unit_from = 1;
to1.setText("to grams");
to2.setText("to decagrams");
to3.setText("to kilograms");
to4.setText("to tons");
name1 = from1.getText();
From();
break;
case 2:
unit_from = 0.000001;
to1.setText("to millimetres^2");
to2.setText("to centimetres^2");
to3.setText("to metres^2");
to4.setText("to kilometres^2");
name1 = from1.getText();
From();
break;
case 3:
unit_from = 1;
to1.setText("to millimetres");
to2.setText("to centimetres");
to3.setText("to metres");
to4.setText("to kilometres");
name1 = from1.getText();
From();
break;
}
}
else if (action == from2)
{
switch (choice)
{
case 1:
unit_from = 10;
to1.setText("to grams");
to2.setText("to decagrams");
to3.setText("to kilograms");
to4.setText("to tons");
name1 = from2.getText();
From();
break;
case 2:
unit_from = 0.0001;
to1.setText("to millimetres^2");
to2.setText("to centimetres^2");
to3.setText("to metres^2");
to4.setText("to kilometres^2");
name1 = from2.getText();
From();
break;
case 3:
unit_from = 10;
to1.setText("to millimetres");
to2.setText("to centimetres");
to3.setText("to metres");
to4.setText("to kilometres");
name1 = from2.getText();
From();
break;
}
}
else if (action == from3)
{
switch (choice)
{
case 1:
unit_from = 1000;
to1.setText("to grams");
to2.setText("to decagrams");
to3.setText("to kilograms");
to4.setText("to tons");
name1 = from3.getText();
From();
break;
case 2:
unit_from = 1;
to1.setText("to millimetres^2");
to2.setText("to centimetres^2");
to3.setText("to metres^2");
to4.setText("to kilometres^2");
name1 = from3.getText();
From();
break;
case 3:
unit_from = 1000;
to1.setText("to millimetres");
to2.setText("to centimetres");
to3.setText("to metres");
to4.setText("to kilometres");
name1 = from3.getText();
From();
break;
}
}
else if (action == from4)
{
switch(choice)
{
case 1:
unit_from = 1000000;
to1.setText("to grams");
to2.setText("to decagrams");
to3.setText("to kilograms");
to4.setText("to tons");
name1 = from4.getText();
From();
break;
case 2:
unit_from =1000000;
to1.setText("to millimetres^2");
to2.setText("to centimetres^2");
to3.setText("to metres^2");
to4.setText("to kilometres^2");
name1 = from4.getText();
From();
break;
case 3:
unit_from = 1000000;
to1.setText("to millimetres");
to2.setText("to centimetres");
to3.setText("to metres");
to4.setText("to kilometres");
name1 = from4.getText();
From();
break;
}
}
else if (action == to1)
{
switch(choice)
{
case 1:
unit_to = 1;
name2 = to1.getText();
To();
break;
case 2:
unit_to = 0.000001;
name2 = to1.getText();
To();
break;
case 3:
unit_to = 1;
name2 = to1.getText();
To();
break;
}
}
else if (action == to2)
{
switch (choice)
{
case 1:
unit_to = 10;
name2 = to2.getText();
To();
break;
case 2:
unit_to = 0.0001;
name2 = to2.getText();
To();
break;
case 3:
unit_to = 10;
name2 = to2.getText();
To();
break;
}
}
else if (action == to3)
{
switch (choice)
{
case 1:
unit_to = 1000;
name2 = to3.getText();
To();
break;
case 2:
unit_to = 1;
name2 = to3.getText();
To();
break;
case 3:
unit_to = 1000;
name2 = to3.getText();
To();
break;
}
}
else if (action == to4)
{
switch (choice)
{
case 1:
unit_to = 1000000;
name2 = to4.getText();
To();
break;
case 2:
unit_to = 1000000;
name2 = to4.getText();
To();
break;
case 3:
unit_to = 1000000;
name2 = to4.getText();
To();
break;
}
}
else if(action == first_field)
{
first_number = Double.parseDouble(first_field.getText());
second_number = unit_from / unit_to;
second_field.setText(String.valueOf(first_number*second_number));
}
else if (action == convert)
{
first_number = Double.parseDouble(first_field.getText());
second_number = unit_from / unit_to;
second_field.setText(String.valueOf(first_number*second_number));
}
}
}
~~~