Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package it.cefi.model;
- import java.awt.GridLayout;
- import javax.swing.BoxLayout;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- import javax.swing.JPanel;
- import javax.swing.JTextField;
- import it.cefi.model.Triangle;
- @SuppressWarnings("serial")
- public class Frame extends JFrame {
- public String pattern = "[0-9]+(\\.[0-9]+)?";
- public Frame() {
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- JFrame mainFrame = new JFrame("Triangle Checker");
- mainFrame.setLayout(new BoxLayout(mainFrame.getContentPane(), BoxLayout.PAGE_AXIS));
- JPanel upperPanel = new JPanel(new GridLayout(0, 2));
- JLabel sideA = new JLabel("Side A");
- JTextField textA = new JTextField();
- JLabel sideB = new JLabel("Side B");
- JTextField textB = new JTextField();
- JLabel sideC = new JLabel("Side C");
- JTextField textC = new JTextField();
- upperPanel.add(sideA);
- upperPanel.add(textA);
- upperPanel.add(sideB);
- upperPanel.add(textB);
- upperPanel.add(sideC);
- upperPanel.add(textC);
- mainFrame.add(upperPanel);
- JPanel lowerPanel = new JPanel();
- JButton checker = new JButton("Check!");
- lowerPanel.add(checker);
- mainFrame.add(lowerPanel);
- mainFrame.setLocationRelativeTo(null);
- mainFrame.setSize(300, 150);
- mainFrame.setResizable(false);
- mainFrame.setVisible(true);
- checker.addActionListener(e -> {
- if (textA.getText().matches(pattern) && textB.getText().matches(pattern)&& textC.getText().matches(pattern)) {
- if (Double.parseDouble(textA.getText()) > 0 && Double.parseDouble(textB.getText()) > 0
- && Double.parseDouble(textC.getText()) > 0) {
- Triangle triangle = new Triangle(Double.parseDouble(textA.getText()),
- Double.parseDouble(textB.getText()), Double.parseDouble(textC.getText()));
- if (triangle.isEquilatero())
- JOptionPane.showMessageDialog(mainFrame, "E' Equilatero!");
- else if (triangle.isIsoscele())
- JOptionPane.showMessageDialog(mainFrame, "E' Isoscele!");
- else if (triangle.isScaleno())
- JOptionPane.showMessageDialog(mainFrame, "E' Scaleno!");
- else
- JOptionPane.showMessageDialog(mainFrame, "E che cos'รจ!?");
- }
- } else {
- JOptionPane.showMessageDialog(mainFrame,
- "Inserisci solo valori numerici positivi, anche decimali utilizzando l'apposito \".\" ");
- textA.setText(null);
- textB.setText(null);
- textC.setText(null);
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement