Java Developer
Мастер
(1616)
3 года назад
import javax.swing.*;
import java.awt.*;
public class Main {
public static void main(String[] args) {
new StartWindow();
}
}
class StartWindow extends JFrame {
public StartWindow() {
setSize(200, 200);
setTitle("Стартовое окно");
setDefaultCloseOperation(EXIT_ON_CLOSE);
JLabel label1 = new JLabel("Запущена программа");
JLabel label2 = new JLabel("студента группы ХХХ-ХХХ");
JLabel label3 = new JLabel("Анотова Антона");
JButton button = new JButton("Далее");
button.addActionListener(actionEvent -> {
Calculator calculator = new Calculator();
new CalcWindow(calculator);
dispose();
});
Container container = getContentPane();
container.setLayout(new BoxLayout(container, BoxLayout.Y_AXIS));
container.add(label1);
container.add(label2);
container.add(label3);
container.add(button);
setVisible(true);
}
}
class CalcWindow extends JFrame {
private JTextField textFieldInput;
private JTextField textFieldOutput;
public CalcWindow(Calculator calculator) {
setSize(300, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Расчеты по формуле");
JLabel label1 = new JLabel("Число Х:");
textFieldInput = new JTextField(30);
JButton button = new JButton("Расчет");
JLabel label2 = new JLabel("Результат Y:");
textFieldOutput = new JTextField(30);
button.addActionListener(actionEvent -> {
double arg = Double.parseDouble(textFieldInput.getText());
calculator.calc(arg);
textFieldOutput.setText(String.valueOf(calculator.getResult()));
});
Container container = getContentPane();
container.setLayout(new BoxLayout(container, BoxLayout.Y_AXIS));
container.add(label1);
container.add(textFieldInput);
container.add(button);
container.add(label2);
container.add(textFieldOutput);
setVisible(true);
}
}
class Calculator {
private double result;
public void calc(double x) {
this.result = 2 * Math.log(x) / (x * x + 1) + 12 * Math.sin(x) / Math.sqrt(x * x + 1);
}
public double getResult() {
return result;
}
}
// с лейаутами сам разберешься