Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- public class Okno implements ActionListener
- {
- JButton start;
- JTextField x,c;
- JTextField wynik;
- public Okno()
- {
- JFrame frame = new JFrame("Swing Paint");
- Container content = frame.getContentPane();
- // set layout on content pane
- content.setLayout(new BorderLayout());
- // create draw area
- // create controls to apply colors and call clear feature
- JPanel controls = new JPanel();
- JPanel controls2 = new JPanel();
- x = new JTextField(5);
- c = new JTextField(30);
- wynik = new JTextField(20);
- start = new JButton("Start");
- start.addActionListener(this);
- wynik.setEditable(false);
- controls.add(x);
- controls.add(start);
- controls.add(wynik);
- controls2.add(new JLabel("C ="));
- controls2.add(c);
- // add to content pane
- content.add(controls, BorderLayout.CENTER);
- content.add(controls2, BorderLayout.NORTH);
- frame.setSize(400, 150);
- // can close frame
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- // show the swing paint result
- frame.setVisible(true);
- }
- public void actionPerformed(ActionEvent e) {
- if (e.getSource() == start)
- {
- String s = c.getText();
- String[] splited = s.split("\\s+");
- double tab[] = new double[splited.length];
- for(int i = 0; i < splited.length; i++)
- {
- tab[i] = Double.parseDouble(splited[i]);
- }
- wynik.setText(String.valueOf(licz(Double.parseDouble(x.getText()),tab)));
- }
- }
- public double licz(double x, double tab[])
- {
- double output = 0.0;
- int tmp = 0;
- for(int i=tab.length-1;i>0;i--){
- output += tab[tmp] * Math.pow(x,i);
- tmp++;
- }
- output += tab[tab.length-1];
- return output;
- }
- public static void main()
- {
- Okno o = new Okno();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement