All pastes #895451 Raw Edit

Miscellany

public java v1 · immutable
·published 2008-02-07 20:52 UTC
rendered paste body
//package projekt;import java.awt.Dimension;import java.awt.Rectangle;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JCheckBoxMenuItem;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JMenuItem;import javax.swing.JOptionPane;import javax.swing.JTextField;import java.net.*;import java.io.*;import java.util.ArrayList;public class Projekt extends JFrame {    private JMenuBar menuBar = new JMenuBar();    private JMenu menuFile = new JMenu();    private JMenuItem menuFileExit = new JMenuItem();    private JMenu menuHelp = new JMenu();    private JButton jButton1 = new JButton();    private JLabel jLabel1 = new JLabel();    private JButton jButton2 = new JButton();    private JButton jButton3 = new JButton();    private JTextField jTextField1 = new JTextField();    private JLabel jLabel2 = new JLabel();    private JMenuItem jMenuItem1 = new JMenuItem();    private int[][] matrix;    public Projekt() {        try {            jbInit();        } catch (Exception e) {            e.printStackTrace();        }    }    private void jbInit() throws Exception {        this.setJMenuBar( menuBar );        this.getContentPane().setLayout( null );        this.setSize( new Dimension(400, 300) );        this.setTitle( "CSV Reader" );        menuFile.setText( "File" );        menuFileExit.setText( "Exit" );        menuFileExit.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent ae ) { fileExit_ActionPerformed( ae ); } } );        menuHelp.setText( "Help" );        jButton1.setText("Pobierz");        jButton1.setBounds(new Rectangle(305, 10, 80, 25));        jButton1.addActionListener(new ActionListener() {                    public void actionPerformed(ActionEvent e) {                        jButton1_actionPerformed(e);                    }                });        jLabel1.setText("Wyniki");        jLabel1.setBounds(new Rectangle(15, 155, 365, 65));        jButton2.setText("Średnia Arytmetyczna");        jButton2.setBounds(new Rectangle(10, 75, 140, 30));        jButton2.addActionListener(new ActionListener() {                    public void actionPerformed(ActionEvent e) {                        jButton2_actionPerformed(e);                    }                });        jButton3.setText("Średnia Ważona");        jButton3.setBounds(new Rectangle(10, 40, 140, 30));        jButton3.addActionListener(new ActionListener() {                    public void actionPerformed(ActionEvent e) {                        jButton3_actionPerformed(e);                    }                });        jTextField1.setBounds(new Rectangle(55, 10, 250, 25));        jLabel2.setText("URL:");        jLabel2.setBounds(new Rectangle(15, 10, 75, 25));        jMenuItem1.setText("About");        menuFile.add( menuFileExit );        menuBar.add( menuFile );        menuHelp.add(jMenuItem1);        menuBar.add(menuHelp);        this.getContentPane().add(jLabel2, null);        this.getContentPane().add(jTextField1, null);        this.getContentPane().add(jButton3, null);        this.getContentPane().add(jButton2, null);        this.getContentPane().add(jLabel1, null);        this.getContentPane().add(jButton1, null);    }            public void ToMatrix() throws Exception{            URL address = new URL(jTextField1.getText()); // stwórz adres            URLConnection aconnection = address.openConnection(); // otwórz połaczenie                       BufferedReader in = new BufferedReader(new InputStreamReader(aconnection.getInputStream())); // przygotuj do czytania                                     ArrayList<String> rows = new ArrayList<String>(); // tu będą wiersze            String inputString; // wczytywany wiersz            while ((inputString = in.readLine()) != null) rows.add(inputString); //wczytaj wiersze do rows                       int[][] matrix = new int[rows.size()][]; // stwórz tablicę o ilości wierszy równą ilości wczytanych wierszy                                     for (int i = 0; i < rows.size(); i++) { //dla każdego wczytanego wiersza                                             String row = rows.get(i); // wiersz wczytany z URL-a            int[] numbers = new int[row.split(";").length]; //tworzymy nową tablicę liczb, wielkości liczb w wierszu            String[] sNumbers = row.split(";"); // tworzymy tablicę Stringów-liczb                                             for (int j = 0; j < sNumbers.length; j++) {                numbers[j] = Integer.parseInt(sNumbers[j]); // zamieniaj Stringi w inty            }                       matrix[i] = numbers; // odpowiedniemu wierszowi w macierzy przyporządkuj daną tablicę	    this.matrix = matrix;        }    }              void fileExit_ActionPerformed(ActionEvent e) {        System.exit(0);    }    void helpAbout_ActionPerformed(ActionEvent e) {        //JOptionPane.showMessageDialog(this, new Projekt_AboutBoxPanel1(), "About", JOptionPane.PLAIN_MESSAGE);    }    private void jButton1_actionPerformed(ActionEvent e) {        if(e.getSource() == jButton1) {            try{                ToMatrix();            }            catch(Exception ex) {                JOptionPane.showMessageDialog(null,"Niepoprawne dane, podaj adres URL!","Error",JOptionPane.ERROR_MESSAGE);            }         }    }        private void jButton3_actionPerformed(ActionEvent e) {	        if(e.getSource() == jButton1) {            try {		//liczenie średniej arytmetycznej, powinno działać;)		int suma = 0;		for (int i = 0; i < this.matrix[0].length; i++) suma += this.matrix[0][i];		double srednia = suma / this.matrix[0].length; //wypisz sobie jakoś;P            }            catch(Exception ex) {                jLabel1.setText(""+ex.getMessage());            }        }     }    private void jButton2_actionPerformed(ActionEvent e) {    }        public static void main(String args[])    {        JFrame frame = new Projekt();	frame.setVisible(true);    }}