Advertisement
Guest User

Untitled

a guest
Aug 30th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.20 KB | None | 0 0
  1. package com;
  2. import java.awt.BorderLayout;
  3. import java.awt.EventQueue;
  4. import javax.swing.JFrame;
  5. import javax.swing.JPanel;
  6. import javax.swing.border.EmptyBorder;
  7. import javax.swing.JLabel;
  8. import javax.swing.JOptionPane;
  9. import javax.swing.JTextField;
  10. import javax.swing.JComboBox;
  11. import javax.swing.DefaultComboBoxModel;
  12. import javax.swing.JButton;
  13. import java.awt.event.ActionListener;
  14. import java.awt.event.ActionEvent;
  15.  
  16. public class Tela extends JFrame {
  17.  
  18. private JPanel contentPane;
  19. private JTextField numero;
  20.  
  21. /**
  22. * Launch the application.
  23. */
  24. public static void main(String[] args) {
  25. EventQueue.invokeLater(new Runnable() {
  26. public void run() {
  27. try {
  28. Tela frame = new Tela();
  29. frame.setVisible(true);
  30. } catch (Exception e) {
  31. e.printStackTrace();
  32. }
  33. }
  34. });
  35. }
  36. public static void verificar(){
  37.  
  38. }
  39. /**
  40. * Create the frame.
  41. */
  42. public Tela() {
  43. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  44. setBounds(100, 100, 450, 300);
  45. contentPane = new JPanel();
  46. contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  47. setContentPane(contentPane);
  48. contentPane.setLayout(null);
  49.  
  50. JPanel panel = new JPanel();
  51. panel.setBounds(10, 0, 414, 261);
  52. contentPane.add(panel);
  53. panel.setLayout(null);
  54.  
  55. JLabel lblNumero = new JLabel("Numero:");
  56. lblNumero.setBounds(10, 11, 53, 14);
  57. panel.add(lblNumero);
  58.  
  59. numero = new JTextField();
  60. numero.setBounds(73, 8, 155, 20);
  61. panel.add(numero);
  62. numero.setColumns(10);
  63.  
  64. JLabel lblDaBase = new JLabel("Da base:");
  65. lblDaBase.setBounds(10, 39, 67, 14);
  66. panel.add(lblDaBase);
  67.  
  68. JComboBox dabase = new JComboBox();
  69. dabase.setModel(new DefaultComboBoxModel(new String[] {"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16"}));
  70. dabase.setBounds(73, 39, 40, 20);
  71. panel.add(dabase);
  72.  
  73. JLabel lblParaBase = new JLabel("Para base:");
  74. lblParaBase.setBounds(10, 64, 67, 14);
  75. panel.add(lblParaBase);
  76. JComboBox parabase = new JComboBox();
  77.  
  78. parabase.setModel(new DefaultComboBoxModel(new String[] {"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16"}));
  79. parabase.setBounds(73, 64, 40, 20);
  80. panel.add(parabase);
  81. JLabel resposta = new JLabel("");
  82. resposta.setBounds(10, 95, 394, 20);
  83. panel.add(resposta);
  84.  
  85. JButton btnConverter = new JButton("Converter");
  86. btnConverter.addActionListener(new ActionListener() {
  87. public void actionPerformed(ActionEvent e) {
  88. String n = numero.getText();
  89. String res = "";
  90. int num = 0 , i = 0;
  91. int baseOrigem = Integer.parseInt(dabase.getSelectedItem().toString());
  92. int baseDestino = Integer.parseInt(parabase.getSelectedItem().toString());
  93. if(!CDB.existeNaBase(n, baseOrigem)){
  94. resposta.setText("O numero "+n+" não existe na base "+baseOrigem);
  95. }else{
  96. while(i < n.length()){
  97. num = num + (CDB.comparar(String.valueOf(n.charAt(i)))) * (int) Math.pow(baseOrigem, n.length() -1- i);
  98. i++;
  99. }
  100. resposta.setText(String.valueOf("O numero "+n+" na base "+baseOrigem+" equivale ao numero "+CDB.converterBase(num,baseDestino)+" na base "+baseDestino));
  101. }
  102. System.out.println("O numero "+n+" na base "+baseOrigem+" equivale ao numero "+CDB.converterBase(num,baseDestino)+" na base "+baseDestino);
  103. }
  104. });
  105.  
  106. parabase.addActionListener(new ActionListener() {
  107. public void actionPerformed(ActionEvent arg0) {
  108. String n = numero.getText();
  109. String res = "";
  110. int num = 0 , i = 0;
  111. int baseOrigem = Integer.parseInt(dabase.getSelectedItem().toString());
  112. int baseDestino = Integer.parseInt(parabase.getSelectedItem().toString());
  113. if(!CDB.existeNaBase(n, baseOrigem)){
  114. resposta.setText("O numero "+n+" não existe na base "+baseOrigem);
  115. }else{
  116. while(i < n.length()){
  117. num = num + (CDB.comparar(String.valueOf(n.charAt(i)))) * (int) Math.pow(baseOrigem, n.length() -1- i);
  118. i++;
  119. }
  120. resposta.setText(String.valueOf("O numero "+n+" na base "+baseOrigem+" equivale ao numero "+CDB.converterBase(num,baseDestino)+" na base "+baseDestino));
  121. }
  122. System.out.println("O numero "+n+" na base "+baseOrigem+" equivale ao numero "+CDB.converterBase(num,baseDestino)+" na base "+baseDestino);
  123. }
  124. });
  125. btnConverter.setBounds(238, 7, 103, 23);
  126. panel.add(btnConverter);
  127.  
  128.  
  129. }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement