IcaroPeretti

Untitled

Aug 6th, 2021 (edited)
67
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.awt.Color;
  2. import java.awt.Container;
  3. import java.awt.Font;
  4. import java.awt.GridLayout;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.io.ByteArrayInputStream;
  8.  
  9. import javax.swing.JButton;
  10. import javax.swing.JFrame;
  11. import javax.swing.JScrollPane;
  12. import javax.swing.JTextArea;
  13. import javax.swing.JTextField;
  14. import javax.swing.JLabel;
  15.  
  16. public class CalculadoraGUI extends JFrame {
  17. private static final long serialVersionUID = 1L;
  18. private JTextField campo1;
  19. private JTextArea campo2;
  20. private Font font = new Font("Verdana", Font.BOLD, 26);
  21. private JButton calcular;
  22. private JLabel lblNewLabel;
  23.  
  24. // configura a GUI
  25. public CalculadoraGUI() {
  26. super("Calculadora");
  27.  
  28. Container container = getContentPane();
  29. container.setLayout(new GridLayout(3, 1));
  30.  
  31. campo1 = new JTextField(15);
  32. campo1.setToolTipText("Digite aqui a expressão matemática.");
  33. campo1.setFont(font);
  34. campo1.setForeground(Color.BLACK);
  35. container.add(campo1);
  36.  
  37. calcular = new JButton("Calcular");
  38. container.add(calcular);
  39.  
  40. campo2 = new JTextArea(5, 15);
  41. campo2.setLineWrap(true);
  42. campo2.setToolTipText("Mensagens ao usuário.");
  43. campo2.setWrapStyleWord(true);
  44. campo2.setFont(font);
  45.  
  46. JScrollPane scrollPane = new JScrollPane(campo2,
  47. JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
  48. JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
  49. container.add(scrollPane);
  50.  
  51. lblNewLabel = new JLabel("Icaro Peretti e Vinicius Boneto");
  52. scrollPane.setColumnHeaderView(lblNewLabel);
  53.  
  54. // registra tratador de eventos
  55. calcular.addActionListener(new ActionListener() {
  56.  
  57. @Override
  58. public void actionPerformed(ActionEvent paramActionEvent) {
  59. Calculadora parser = new Calculadora(new ByteArrayInputStream(
  60. campo1.getText().getBytes()));
  61. double ret;
  62. try {
  63. ret = parser.Start();
  64. campo2.setForeground(Color.BLUE);
  65. campo2.setText(String.valueOf(ret));
  66. } catch (Throwable t) {
  67. campo2.setForeground(Color.RED);
  68. campo2.setText("Erro!\n" + t.getMessage());
  69. }
  70. }
  71. });
  72. setSize(250, 400);
  73. setVisible(true);
  74. }
  75.  
  76. // executa a aplicacao
  77. public static void main(String args[]) {
  78. CalculadoraGUI aplicacao = new CalculadoraGUI();
  79. aplicacao.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  80. }
  81. }
RAW Paste Data Copied