Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5.  
  6. public class Okno implements ActionListener
  7. {
  8. JButton start;
  9. JTextField x,c;
  10. JTextField wynik;
  11. public Okno()
  12. {
  13. JFrame frame = new JFrame("Swing Paint");
  14. Container content = frame.getContentPane();
  15. // set layout on content pane
  16. content.setLayout(new BorderLayout());
  17. // create draw area
  18.  
  19. // create controls to apply colors and call clear feature
  20. JPanel controls = new JPanel();
  21. JPanel controls2 = new JPanel();
  22.  
  23. x = new JTextField(5);
  24. c = new JTextField(30);
  25. wynik = new JTextField(20);
  26. start = new JButton("Start");
  27. start.addActionListener(this);
  28. wynik.setEditable(false);
  29.  
  30. controls.add(x);
  31. controls.add(start);
  32. controls.add(wynik);
  33.  
  34. controls2.add(new JLabel("C ="));
  35. controls2.add(c);
  36.  
  37. // add to content pane
  38. content.add(controls, BorderLayout.CENTER);
  39. content.add(controls2, BorderLayout.NORTH);
  40.  
  41. frame.setSize(400, 150);
  42. // can close frame
  43. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  44. // show the swing paint result
  45. frame.setVisible(true);
  46. }
  47.  
  48. public void actionPerformed(ActionEvent e) {
  49. if (e.getSource() == start)
  50. {
  51. String s = c.getText();
  52. String[] splited = s.split("\\s+");
  53. double tab[] = new double[splited.length];
  54. for(int i = 0; i < splited.length; i++)
  55. {
  56. tab[i] = Double.parseDouble(splited[i]);
  57. }
  58. wynik.setText(String.valueOf(licz(Double.parseDouble(x.getText()),tab)));
  59. }
  60. }
  61.  
  62. public double licz(double x, double tab[])
  63. {
  64. double output = 0.0;
  65. int tmp = 0;
  66.  
  67. for(int i=tab.length-1;i>0;i--){
  68. output += tab[tmp] * Math.pow(x,i);
  69. tmp++;
  70. }
  71. output += tab[tab.length-1];
  72. return output;
  73. }
  74.  
  75. public static void main()
  76. {
  77. Okno o = new Okno();
  78. }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement