Jump to content

Recommended Posts

Posted

Boas! tou com um problema que me parece simples mas não consigo resolve-los, espero que me possam ajudar!

é o seguinte:

tenho 4 classes: "graf", "mensagens", "utilizadores", "menu", sendo que "mensagens" e "utilizadores" é so de metodos get's e set's, para puder criar objectos dessas classes.

Eu criei um objecto na class graf da class utilizadores e consigo aceder a esse objecto e trabalhar com ele, mas na classe "menu_on" nao consigo.

tenho o seguinte código:

---------  Class graf --------------

package trabalho;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.util.ArrayList;

public class graf extends JFrame {

    private JButton Registar,  Login;

    private Container contentPane;

    ArrayList a = new ArrayList();

    ArrayList m = new ArrayList();

    public graf() {

        setTitle("Menu Inicial");

        setSize(300, 250);

        addWindowListener(new Terminator());

        ProcessaBotao act = new ProcessaBotao();

        contentPane = getContentPane();

        contentPane.setBackground(Color.white);

        contentPane.setLayout(new FlowLayout());

        Registar = new JButton("Registar");

        Login = new JButton("Login");

        contentPane.add(Registar);

        contentPane.add(Login);

        Registar.addActionListener(act);

        Login.addActionListener(act);

    }

    class ProcessaBotao implements ActionListener {

        public void actionPerformed(ActionEvent e) {

            if (e.getSource() instanceof JButton) {

                JButton b = (JButton) e.getSource();

                if (b == Registar) {

                    Registar(a, m);

                } else if (b == Login) {

                    login (a, m);

                }

            }

        }

    }

public void Login(ArrayList<utilizadores> a, ArrayList m) {

        String utilizador = JOptionPane.showInputDialog("Introduza seu nome de utilizador:");

        // 1 falso

        // guardar_posicao serve para saber onde esta o nome de utilizador

        int guardar_posicao = 0;

        int resposta = 1;

        for (int i = 0; i < a.size(); i++) {

            if (utilizador.equals(((a.get(i)).getUsername()))) {

                resposta = 2;

            }

            guardar_posicao = i;

        }

        if (resposta == 1) {

            JOptionPane.showMessageDialog(null, "O Nome de utilizador inserido não foi encontrado tente de novo");

            Login(a, m);

        }

        String chave;

        chave = JOptionPane.showInputDialog("Introduza a sua palavra-chave:");

        // 1 falso

        int resposta_password = 1;

        for (int i = 0; i < a.size(); i++) {

            if (utilizador.equals(((a.get(i)).getUsername()))) {

                if (chave.equals(((a.get(i)).getPassword()))) {

                    resposta_password = 2;

                }

            }

        }

        if (resposta_password == 1) {

            JOptionPane.showMessageDialog(null, "A Palavra-Chave inserida nao correspondeu. Tente de novo");

            Login(a, m);

        }

        menu_on s1 = new menu_on();

        s1.setVisible(true);

        s1.setSize(300, 300);

        s1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

    class Terminator extends WindowAdapter {

        public void windowClosing(WindowEvent e) {

            System.exit(0);

        }

    }

    public static void main(String[] args) {

        JFrame jan = new graf();

        jan.show();

    }

}

------------------------------------------

------------ Class menu_on --------------

package trabalho;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.util.ArrayList;

public class menu_on extends JFrame {

    private JButton envmsg,  vermsgr,  verut,  elimsg;

    private Container contentlog;

    public menu_on() {

       

        setTitle("Bem Vindo!");

        setSize(300, 250);

        addWindowListener(new Terminator());

       

        Procb act2 = new Procb();

        contentlog = getContentPane();

        contentlog.setBackground(Color.white);

        contentlog.setLayout(new FlowLayout());

        envmsg = new JButton("Enviar Mensagem");

        vermsgr = new JButton("Ver Mensagens Recebidas");

        verut = new JButton("Ver Utilizadores Online");

        elimsg = new JButton("Elminar Mensagens");

        contentlog.add(envmsg);

        contentlog.add(vermsgr);

        contentlog.add(verut);

        contentlog.add(elimsg);

        envmsg.addActionListener(act2);

        vermsgr.addActionListener(act2);

        verut.addActionListener(act2);

        elimsg.addActionListener(act2);

    }

   

    class Procb implements ActionListener {

        public void actionPerformed(ActionEvent e) {

            String utilizador = novo.getUsername();

            if (e.getSource() instanceof JButton) {

                JButton b = (JButton) e.getSource();

                if (b == envmsg) {

                    Menu_Mensagem(utilizador, a, m);

                } else if (b == vermsgr) {

                    Recebidas(utilizador, a, m);

                } else if (b == verut) {

                    ver(utilizador, a, m);

                } else if (b == elimsg) {

                    eliminar(utilizador, a, m);

                }

            }

        }

    }

   

(...)

    class Terminator extends WindowAdapter {

        public void windowClosing(WindowEvent e) {

            System.exit(0);

        }

    }

    public static void main(String[] args) {

        JFrame jan = new menu_on();

        jan.show();

    }

}

---------------------------------------------------

Nesta class "menu_on", da-me erro para tentar aceder aos arraylists "a" e "m" e ao tentar ir buscar o nome do utilizador também.

Será que me sabem explicar qual o problema?

Cumps.

Posted

ja meti: public ArrayList getListaA(){ return a; }

na class graf, mas continua a aparecer erro na class menu_on, tanto nos arrays como se eu fizer um get para buscar algum valor do objecto de utilizadores.

  • 1 month later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.