Przekazanie wartości zmiennej do innego pliku .class oraz wywołanie informacji

0
  1. Chce wartość zmiennej login w pliku ExternalPanel1.java przekazać do pliku ExternalPanel2.java

Kod ExternalPanel1.java

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
import java.sql.*;

public class ExternalPanel1 extends JFrame implements ActionListener {
	
	DB_Connect dbc = new DB_Connect();
	ExternalPanel2 ep2 = new ExternalPanel2();
	JPanel p1 = new JPanel(new GridBagLayout());
	JButton loginButton = new JButton("Zaloguj się");
	JButton infoButton = new JButton("O Programie");
    JLabel labelUsername = new JLabel("Wpisz login: ");
    JLabel labelPassword = new JLabel("Wpisz hasło: ");
    JTextField textUsername = new JTextField(20);
    JPasswordField fieldPassword = new JPasswordField(20);
    String uname;
	String pwd;
	String l;
	String p;
	Statement stat;
	ResultSet rs;
  
    public ExternalPanel1() {
		super("GoldenLines ASO TelemetryApp");
		setDefaultCloseOperation(EXIT_ON_CLOSE);
        setPreferredSize(new Dimension(380, 160));
        //setResizable(false);
        setLocationRelativeTo(null);
        GridBagConstraints c = new GridBagConstraints();
        c.anchor = GridBagConstraints.WEST;
        c.insets = new Insets(5, 5, 5, 5);
        c.gridx = 0;
        c.gridy = 0;
        p1.add(labelUsername, c); 
        c.gridx = 1;
        c.gridy = 0;
        p1.add(textUsername, c);
        c.gridx = 0;
        c.gridy = 1;
        c.fill = GridBagConstraints.NONE;
        p1.add(labelPassword, c);        
        c.gridx = 1;
        c.gridy = 1;
        p1.add(fieldPassword, c);
        c.gridx = 0;
        c.gridy = 2;
        p1.add(loginButton, c);
        loginButton.addActionListener(this);
        c.gridx = 1;
        c.gridy = 2;
        p1.add(infoButton, c);
        infoButton.addActionListener(this);
		add(p1);
        p1.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Panel Główny"));
        pack();	
	}
    
    public void checkLogin() {
    	dbc.Connect();
		char[] temp_pwd = fieldPassword.getPassword();
        pwd = null;
        pwd = String.copyValueOf(temp_pwd);
        uname = textUsername.getText();
		try {	
			stat = dbc.conn.createStatement();
	        rs = stat.executeQuery("SELECT login, password FROM aso_users where login='"+uname+"' AND password='"+pwd+"'");
	        while (rs.next()) {
	        	l = String.valueOf(rs.getString("login"));
	        	p = String.valueOf(rs.getString("password"));
	        }
		}
		catch (SQLException e) {
			e.printStackTrace();
		}
	}
    
    @Override
	public void actionPerformed(ActionEvent e) {
		Object akcja = e.getSource();
		if(akcja == loginButton) {
			checkLogin();
			if (l.equals(uname) == true && p.equals(pwd) == true) {
				ep2.setVisible(true);
				setVisible(false);				
			}
			dbc.closeConnect();
		}
		else if (akcja == infoButton) {
			JOptionPane.showMessageDialog (null, "Autor: Danix6\nWersja: 0.1", "O programie", JOptionPane.INFORMATION_MESSAGE);
		}
	}
}

Kod ExternalPanel2.java

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;
import java.util.Timer;

public class ExternalPanel2 extends JFrame implements ActionListener {

	DB_Connect dbc = new DB_Connect();
	JPanel p1 = new JPanel(new GridBagLayout());
	JButton logoutButton = new JButton("Wyjście");
	JButton importButton = new JButton("Wczytaj plik telemetrii");
	FileDialog fd = new FileDialog(this, "Wybierz plik", FileDialog.LOAD);
	String katalog;
	String plik;
	JLabel fileLabel = new JLabel("Wybrano plik:");
	JLabel directoryfileLabel = new JLabel("W katalogu:");	
	Timer timer = new Timer();
	Statement stat;
	ResultSet rs;
	Scanner sc;
	Scanner line;
	String linia;
	String delimiter1 = "#";
	String delimiter2 = "\\|";
	String delimiter3 = "";
	String[] dane_kat;
	String dane_tel1;
	String dane_diag2;
	String dane_ele3;
	String dane_poz4;
	String dane_konf5;
	String[] dane1;
	String[] dane2;
	String[] dane3;
	String[] dane4;
	String[] dane5;
	
	public ExternalPanel2() {
		super("GoldenLines ASO TelemetryApp");
		setDefaultCloseOperation(EXIT_ON_CLOSE);
        setPreferredSize(new Dimension(500, 200));
        //setResizable(false);
        setLocationRelativeTo(null);
        GridBagConstraints c = new GridBagConstraints();
        c.anchor = GridBagConstraints.WEST;
        c.insets = new Insets(5, 5, 5, 5);
        c.gridx = 0;
        c.gridy = 1;
        p1.add(fileLabel, c);
        c.gridx = 0;
        c.gridy = 2;
        p1.add(directoryfileLabel, c);
        c.gridx = 0;
        c.gridy = 3;
        p1.add(importButton, c);
        importButton.addActionListener(this);
        c.gridx = 0;
        c.gridy = 4;
        p1.add(logoutButton, c);
        logoutButton.addActionListener(this);
		add(p1);
        p1.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Panel Telemetrii"));
        pack();		          
	}
	
	public void wczytajPlik() {
		fd.setVisible(true);
		katalog = fd.getDirectory();
		plik = fd.getFile();
		fileLabel.setText("Wybrano plik: " + plik);
		directoryfileLabel.setText("W katalogu: " + katalog);
	    if (plik == null) {
	    	JOptionPane.showMessageDialog (null, "Nie wybrano pliku", "Błąd", JOptionPane.INFORMATION_MESSAGE);
	    }
	    else {
	    	timer.scheduleAtFixedRate(new TimerTask() {
	        	  @Override
	        	  public void run() {
	        	    wypiszDaneZPliku();
	        	  }
	        	}, 0, 10000);   
	    }
	}
	
	public void wypiszDaneZPliku() {
		try
        {
            sc = new Scanner(fd.getFiles()[0]);
            while(sc.hasNextLine())
            {
            	line = new Scanner(sc.nextLine());
                while(line.hasNext())
                {
                	linia = line.next();
                	dane_kat = linia.split(delimiter1);
                	dane_tel1 = dane_kat[0];
                	dane1 = dane_tel1.split(delimiter2);
                	System.out.println("Dane Telemetryczne: " + dane_tel1);
                	float adBlue = Math.round(Float.valueOf(dane1[0]));
                	float srAdBlue = Float.valueOf(dane1[1]);
                	float volty = Float.valueOf(dane1[2]);
                	float psiHamulce = Float.valueOf(dane1[3]);
                	float tempHamulce = Float.valueOf(dane1[4]);
                	float cruiseControl = Float.valueOf(dane1[5]);
                	float obroty = Float.valueOf(dane1[6]);
                	int bieg = Integer.valueOf(dane1[7]);
                	float fuel = Float.valueOf(dane1[8]);
                	float srFuel = Float.valueOf(dane1[9]);
                	float mileage = Float.valueOf(dane1[10]);
                	float psiOlej = Float.valueOf(dane1[11]);
                	float tempOlej = Float.valueOf(dane1[12]);
                	int retarder = Integer.valueOf(dane1[13]);
                	float predkosc = Float.valueOf(dane1[14]);
                	float tempWody = Float.valueOf(dane1[15]);
                	System.out.println("Poziom AdBlue: " + adBlue + " l");
                	System.out.println("Średnie zużycie AdBlue: " + srAdBlue + " l/km");
                	System.out.println("Napięcie baterii: " + volty + " V");
                	System.out.println("Ciśnienie powietrza w układzie hamulcowym: " + psiHamulce + " psi");
                	System.out.println("Temperatura hamulców: " + tempHamulce + " C");
                	System.out.println("Prędkość tempomatu: " + cruiseControl + " m/s");
                	System.out.println("Obroty silnika: " + obroty + " RPM x 1");
                	System.out.println("Wybrany bieg: " + bieg);
                	System.out.println("Poziom paliwa w baku: " + fuel + " l");
                	System.out.println("Średnie zużycie paliwa: " + srFuel + " l/km");
                	System.out.println("Przebieg: " + mileage + " km");
                	System.out.println("Ciśnienie oleju: " + psiOlej + " psi");
                	System.out.println("Temperatura oleju: " + tempOlej + " C");
                	System.out.println("Poziom retardera: " + retarder);
                	System.out.println("Prędkość: " + predkosc + " m/s");
                	System.out.println("Temperatura wody: " + tempWody + " C");
                	System.out.println();
                	dane_diag2 = dane_kat[1];
                	dane2 = dane_diag2.split(delimiter2);
                	System.out.println("Dane Diagnostyczne: " + dane_diag2);
                	float sKabina = Float.valueOf(dane2[0]);
                	float sPodwozie = Float.valueOf(dane2[1]);
                	float sSilnik = Float.valueOf(dane2[2]);
                	float sSkrzyniaBiegow = Float.valueOf(dane2[3]);
                	float sOpony = Float.valueOf(dane2[4]);
                	System.out.println("Stan kabiny: " + sKabina + " %");
                	System.out.println("Stan podwozia: " + sPodwozie + " %");
                	System.out.println("Stan silnika: " + sSilnik + " %");
                	System.out.println("Stan skrzyni biegów: " + sSkrzyniaBiegow + " %");
                	System.out.println("Stan opon: " + sOpony + " %");
                	System.out.println();
                	dane_ele3 =  dane_kat[2];
                	dane3 = dane_ele3.split(delimiter3);
                	System.out.println("Elektryka: " + dane_ele3);
                	int lAdBlue = Integer.valueOf(dane3[1]);
                	int lVolty = Integer.valueOf(dane3[2]);
                	int hamulecGorski = Integer.valueOf(dane3[3]);
                	int lPsiHamulce = Integer.valueOf(dane3[4]);
                	int electric = Integer.valueOf(dane3[5]);
                	int engine = Integer.valueOf(dane3[6]);
                	int lFuel = Integer.valueOf(dane3[7]);
                	int lIndicator = Integer.valueOf(dane3[8]);
                	int rIndicator = Integer.valueOf(dane3[9]);
                	int lBeam = Integer.valueOf(dane3[10]);
                	int hBeam = Integer.valueOf(dane3[11]);
                	int lPsiOlej = Integer.valueOf(dane3[12]);
                	int hamulecReczny = Integer.valueOf(dane3[13]);
                	int hTempWody = Integer.valueOf(dane3[14]);
                	System.out.println("Niski poziom adblue: " + lAdBlue);
                	System.out.println("Niskie napięcie/Nie ładuje baterii: " + lVolty);
                	System.out.println("Aktywny hamulec bezpieczeństwa: " + hamulecGorski);
                	System.out.println("Niskie ciśnienie powietrza układu hamulcowego: " + lPsiHamulce);
                	System.out.println("Elekrtyka włączona: " + electric);
                	System.out.println("Silnik włączony: " + engine);
                	System.out.println("Niski poziom paliwa: " + lFuel);
                	System.out.println("Lewy kierunkowskaz: " + lIndicator);
                	System.out.println("Prawy kierunkowskaz: " + rIndicator);
                	System.out.println("Światła mijania: " + lBeam);
                	System.out.println("Światła drogowe: " + hBeam);
                	System.out.println("Ostrzeżenie o ciśnieniu oleju: " + lPsiOlej);
                	System.out.println("Zaciągnięty hamulec ręczny: " + hamulecReczny);
                	System.out.println("Wysoka temperatura wody: " + hTempWody);
                	System.out.println();
                	dane_poz4 = dane_kat[3];
                	dane4 = dane_poz4.split(delimiter2);
                	System.out.println("Pozycja Na Mapie (X|Y|Z): " + dane_poz4);
                	float pozX = Float.valueOf(dane4[0]);
                	float pozY = Float.valueOf(dane4[1]);
                	float pozZ = Float.valueOf(dane4[2]);
                	System.out.println("Pozycja X: " + pozX);
                	System.out.println("Pozycja Y: " + pozY);
                	System.out.println("Pozycja Z: " + pozZ);
                	System.out.println();
                	dane_konf5 = dane_kat[4];
                	dane5 = dane_konf5.split(delimiter2);
                	System.out.println("Dane Konfiguracji: " + dane_konf5);
                	String marka = dane5[0];
                	String model = dane5[1];
                	float maxFuel = Float.valueOf(dane5[2]);
                	float maxAdBlue = Float.valueOf(dane5[3]);
                	float maxObroty = Float.valueOf(dane5[4]);
                	int biegiPrzod = Integer.valueOf(dane5[5]);
                	int biegiTyl = Integer.valueOf(dane5[6]);
                	int stopnieRetarder = Integer.valueOf(dane5[7]);
                	float ostFuel = Float.valueOf(dane5[8]);
                	float ostHamulce = Float.valueOf(dane5[9]);
                	float activeHamulec = Float.valueOf(dane5[10]);
                	float ostOlej = Float.valueOf(dane5[11]);
                	float ostWoda = Float.valueOf(dane5[12]);
                	float ostVolty = Float.valueOf(dane5[13]);
                	int kola = Integer.valueOf(dane5[14]);
                	System.out.println("Marka: " + marka);
                	System.out.println("Model: " + model);
                	System.out.println("Pojemność zbiorników paliwa: " + maxFuel + " l");
                	System.out.println("Pojemność zbiorników AdBlue: " + maxAdBlue + " l");
                	System.out.println("Limit obrotów silnika: " + maxObroty + " RPM");
                	System.out.println("Ilość biegów do przodu: " + biegiPrzod);
                	System.out.println("Ilość biegów do tyłu: " + biegiTyl);
                	System.out.println("Ilość stopni retardera: " + stopnieRetarder);
                	System.out.println("Ilość paliwa która wywołuje ostrzeżenie: " + ostFuel + " l");
                	System.out.println("Ciśnienie układu hamulcowego wywołujące ostrzeżenie: " + ostHamulce + " psi");
                	System.out.println("Ciśnienie układu hamulcowego aktywujące hamulec bezpieczeństwa: " + activeHamulec + " psi");
                	System.out.println("Ciśnienie oleju wywołujące ostrzeżenie: " + ostOlej + " psi");
                	System.out.println("Temperatura wody wywołujące ostrzeżenie: " + ostWoda + " C");
                	System.out.println("Napięci baterii wywołujące ostrzeżenie: " + ostVolty + " V");
                	System.out.println("Ilość kół: " + kola + " sztuk");
                	/*dbc.Connect();
                	try {
                		stat = dbc.conn.createStatement();
                		stat.executeUpdate("INSERT INTO aso_telemetry(Telemetry) VALUES ('"+linia+"')");
                	}
                	catch (Exception ex) { 
                	    throw new RuntimeException(ex);
                	}*/
                    //System.out.println(linia);
                }
            }
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
	}
		
	@Override
	public void actionPerformed(ActionEvent e) {
		Object akcja = e.getSource();
		if(akcja == logoutButton) {
			System.exit(0);
			timer.cancel();
			dbc.closeConnect();
		}
		if(akcja == importButton) {
			dbc.Connect();
			wczytajPlik();
		}
	}
}
  1. Chce jeszcze aby po wpisaniu złego loginu i hasła w pliku ExternalPanel1.java wyświetlało się JOptionPane wpisanym złym loginie i haśle

Jak najprościej mogę te dwie rzeczy wykonać, bo już siedzę na tym kilka dni i nic nie przychodzi mi już do głowy jak mam to zrobić?

0

Chce wartość zmiennej login w pliku ExternalPanel1.java przekazać do pliku ExternalPanel2.java

A nie możesz tego przekazać przez konstruktor?

0

A jak to się robiło bo zapomniałem?

0

Jedyne co możesz zrobić z tym kodem to:
nuke.jpg
Serio. Jakiekolwiek próby poprawiania tego to tzw "rzeźbienie w gównie". Skasuj. Przeczytaj kurs programowania obiektowego. Napisz od nowa.

1 użytkowników online, w tym zalogowanych: 0, gości: 1