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

import Lab2.*;

import javax.swing.*;
import java.awt.*;
import java.io.IOException;
import java.util.ArrayList;

public class MyDialog extends javax.swing.JDialog {
private javax.swing.JPanel contentPane;
private JTextField textField1;
private JTextField textField2;
private JTextField textField3;
private JTextField textField4;
private JTextField textField6;
private JButton buttonAdd;
private JButton buttonCancel;
private JLabel Label;
private JTextArea extArea;
private JTextArea TextArea2;
private JTextArea TextArea4;
private JTextArea TextArea5;
private JTextArea TextArea6;
private JComboBox ComboBox;
private JTextArea TextArea0;

public MyDialog() {
setContentPane(contentPane);
setModal(true);
setTitle("My foooooorm");
ComboBox.addActionListener( e-> {
String type = (String) ComboBox.getItemAt(ComboBox.getSelectedIndex());
System.out.println(type);
if(type.equals("Автобус")||type.equals("Автомобиль")) {
textField3.setBackground(new Color(237, 237, 237));
textField3.setText("0");
textField6.setBackground(new Color(255, 255, 255));
textField6.setText("");
} else
if(type.equals("Грузовик")) {
textField3.setBackground(new Color(255, 255, 255));
textField3.setText("");
textField6.setBackground(new Color(237, 237, 237));
textField6.setText("0");
} else
{
textField6.setBackground(new Color(255, 255, 255));
textField6.setBackground(new Color(255, 255, 255));
}

});

buttonAdd.addActionListener(e -> {
addObject();

});
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent e) {
onCancel();
}
});
buttonCancel.addActionListener(e -> {
onCancel();
});
}

private void onCancel() {
dispose();
}

void addObject() {
try {
ArrayList<Trans> trs = new ArrayList<Trans>();
String type = (String) ComboBox.getItemAt(ComboBox.getSelectedIndex());
String mark = textField1.getText();
String model = textField4.getText();
int max_speed = Integer.parseInt(textField2.getText());
int max_load = Integer.parseInt(textField3.getText());
int max_pass = Integer.parseInt(textField6.getText());


if (type.equals("Автомобиль") || type.equals("Автобус")) {
if (type.equals("Автомобиль")) trs.add(new Car(model, mark, max_speed, max_pass));
if (type.equals("Автобус")) trs.add(new Bus(model, mark, max_speed, max_pass));
} else if (type.equals("Мотоцикл")) {
trs.add(new Motorcycle(mark, model, max_speed));
} else if (type.equals("Грузовик")) {
trs.add(new Truck(model, mark, max_speed, max_load));
}
System.out.println(type + " - " + trs.get(0));
JOptionPane.showMessageDialog(null, "Объект успешно добавлен");
} catch (Exception e) { //обнаружение ошибки во входном потоке
e.printStackTrace();
JOptionPane.showMessageDialog(null, "Некорректные данные");
}
}
public static void main(String[] args) {
MyDialog dialog = new MyDialog();
dialog.pack();
dialog.setVisible(true);
System.exit(0);
}
}
Соседние файлы в папке Lab6