Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
58
Добавлен:
18.12.2017
Размер:
5.32 Кб
Скачать
package lab2_java;

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;

public class LabInterface extends JFrame {
private JButton buttonAdd = new JButton("Add");
private JButton buttonClear = new JButton("Clear");

private JTextField inputModel = new JTextField("", 20);
private JTextField inputSpeed = new JTextField("", 3);
private JTextField inputRadio = new JTextField("", 20);
private JTextField inputWeight = new JTextField("", 2);
private JTextField inputHeight = new JTextField("", 2);

private JLabel labelModel = new JLabel("Model:");
private JLabel labelSpeed = new JLabel("Speed:");
private JLabel labelRadio = new JLabel("Radio:");
private JLabel labelWeight = new JLabel("Weight:");
private JLabel labelHeight = new JLabel("Height:");

private JCheckBox checkTruck = new JCheckBox("Add Truck", false);
private JCheckBox checkRadio = new JCheckBox("Radio Status", false);

private ArrayList<Car> cars = new ArrayList<Car>();
private ArrayList<Truck> trucks = new ArrayList<Truck>();

public void TruckFieldsVisibility(boolean state) {
inputWeight.setVisible(state);
inputHeight.setVisible(state);
labelWeight.setVisible(state);
labelHeight.setVisible(state);
}

public LabInterface() {
super("Lab6");
this.setBounds(0,0,400,200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Container container = this.getContentPane();
container.setLayout(new GridLayout(7,2));
container.add(labelModel);
container.add(inputModel);
container.add(labelSpeed);
container.add(inputSpeed);
container.add(labelRadio);
container.add(inputRadio);

labelWeight.setVisible(false);
labelHeight.setVisible(false);
inputWeight.setVisible(false);
inputHeight.setVisible(false);

container.add(labelWeight);
container.add(inputWeight);
container.add(labelHeight);
container.add(inputHeight);

checkTruck.addItemListener(new CheckBoxEventListener());
container.add(checkTruck);
container.add(checkRadio);
buttonClear.addActionListener(new ClearButtonEventListener());
container.add(buttonClear);
buttonAdd.addActionListener(new AddButtonEventListener());
container.add(buttonAdd);

}

private static boolean isDigit(String s) throws NumberFormatException {
try {
Integer.parseInt(s);
return true;
} catch (NumberFormatException e) {
return false;
}
}

void ConsolePrint(){
System.out.println("=============================");

for (Car cr : cars) {
System.out.println(cr.toStr_Car());
}

for (Truck truck : trucks) {
System.out.println(truck.toStr_Truck());
}
}

class CheckBoxEventListener implements ItemListener{
public void itemStateChanged(ItemEvent e) {
TruckFieldsVisibility(checkTruck.isSelected());
}
}

class AddButtonEventListener implements ActionListener {
public void actionPerformed(ActionEvent e) {

String message = new String("Error!\n");
boolean error = false;

String model = new String();
Integer speed = null;
Radio radio = new Radio();

Integer weight = null;
Integer height = null;

if (inputModel.getText().isEmpty()) {
message += "Enter Model!\n";
error = true;
}
else {
model = inputModel.getText();
}

if(!isDigit(inputSpeed.getText()) || inputSpeed.getText().isEmpty() || Integer.parseInt(inputSpeed.getText()) <= 0) {
message += "Speed error!\n";
error = true;
}
else{
speed = Integer.parseInt(inputSpeed.getText());
}

if (inputRadio.getText().isEmpty()){
message += "Enter Radio!\n";
error = true;
}
else {
radio = new Radio(checkRadio.isSelected(),inputRadio.getText());
}

if (checkTruck.isSelected()) {

if(!isDigit(inputWeight.getText()) || inputWeight.getText().isEmpty()) {
message += "Weight error!\n";
error = true;
}
else{
weight = Integer.parseInt(inputWeight.getText());
}

if(!isDigit(inputHeight.getText()) || inputHeight.getText().isEmpty()) {
message += "Height error!\n";
error = true;
}
else{
height = Integer.parseInt(inputHeight.getText());
}
}

if (error) {
JOptionPane.showMessageDialog(null,
message,
"Output",
JOptionPane.PLAIN_MESSAGE);
return;
}
else {
if(checkTruck.isSelected()) {
Truck tr = new Truck(model, speed, radio, weight, height);
trucks.add(tr);
message = "Truck added!" + tr.toStr_Truck();
JOptionPane.showMessageDialog(null,
message,
"Output",
JOptionPane.PLAIN_MESSAGE);
ConsolePrint();
}
else {
Car car = new Car(model, speed, radio);
cars.add(car);
message = "Auto added!" + car.toStr_Car();
JOptionPane.showMessageDialog(null,
message,
"Output",
JOptionPane.PLAIN_MESSAGE);
ConsolePrint();
}
return;
}
}
}

class ClearButtonEventListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
inputModel.setText(null);
inputSpeed.setText(null);
inputRadio.setText(null);
inputWeight.setText(null);
inputHeight.setText(null);
}
}

}
Соседние файлы в папке lab2_java