Add a lower menu

Asked

Viewed 238 times

0

I would like to do a lower menu in my java application, to put time and run time or even some part of configuration even. I already have one JMenuBar at the top.

that’s my code.

package tela;

import componente.MeuJDesktopPane;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JSeparator;

public class TelaSistema extends JFrame implements ActionListener {

    public static final MeuJDesktopPane jdp = new MeuJDesktopPane();
    private JMenuBar jmb = new JMenuBar();
    private JMenu jmCadastros = new JMenu("Cadastros");
    private JMenu cadRegiao = new JMenu("Região");
    private JMenu cadProduto = new JMenu("Produtos");
    private JMenu cadAlunoF = new JMenu("Aluno/Fornecedor");
    private JMenu cadExercicio = new JMenu("Exercícios");
    private JMenu jmMovimentacoes = new JMenu("Movimentações");
    private JMenu jmRelatorios = new JMenu("Relatórios");
    private JMenuItem jmiPais = new JMenuItem("País");
    private JMenuItem jmiEstado = new JMenuItem("Estado");
    private JMenuItem jmiCidade = new JMenuItem("Cidade");
    private JMenuItem jmiAluno = new JMenuItem("Aluno");
    private JMenuItem jmiMatricula = new JMenuItem("Matricula");
    private JMenuItem jmiMatriculaModalidade = new JMenuItem("Matricula Modalidade");
    private JMenuItem jmiGrupoMatricula = new JMenuItem("Grupo Matricula");
    private JMenuItem jmiGrupoMuscular = new JMenuItem("Grupo Muscular");
    private JMenuItem jmiExercicios = new JMenuItem("Exercícios");
    private JMenuItem jmiModalidade = new JMenuItem("Modalidade");
    private JMenuItem jmiFormaDePagamento = new JMenuItem("Forma de Pagamento");
    private JMenuItem jmiFornecedor = new JMenuItem("Fornecedor");
    private JMenuItem jmiMarca = new JMenuItem("Marca");
    private JMenuItem jmiTipo = new JMenuItem("Tipo");
    private JMenuItem jmiModelo = new JMenuItem("Modelo");
    private JMenuItem jmiLote = new JMenuItem("Lote Produto");
    private JMenuItem jmiProduto = new JMenuItem("Produto");
    private JMenuItem jmiTreino = new JMenuItem("Treino");
    private JMenuItem jmiAvaliacaoFisica = new JMenuItem("Avaliação Física");
    private JMenuItem jmiItemTreino = new JMenuItem("ItemTreino");
    private JMenuItem jmiPedidoCompra = new JMenuItem("Pedido Compra");
    private JMenuItem jmiTurma = new JMenuItem("Turma");
    private JMenuItem jmiTurmaAlunos = new JMenuItem("Turma Alunos");
    private JMenuItem jmiCompra = new JMenuItem("Compra");
    private JMenuItem jmiVenda = new JMenuItem("Venda");
    private JMenuItem jmiContaPagar = new JMenuItem("Conta Pagar");
    private JMenuItem jmiContaReceber = new JMenuItem("Conta Receber");
    private JMenuItem jmiCaixa = new JMenuItem("Caixa");
    private JMenuItem jmiRelatorioCliente = new JMenuItem("Cliente");
    JPanel jmInf = new JPanel();
    private JPanel jpBotoes = new JPanel();

    public TelaSistema() {
        setTitle("FIRE FITNESS");
        getContentPane().add(jdp);
        setJMenuBar(jmb);
        jmb.add(jmCadastros);
        jmb.add(jmMovimentacoes);
        jmb.add(jmRelatorios);
      //  menuInferior();
        InsereLogo();
        InsereIcones();
        adicionaItensMenu();
        setExtendedState(MAXIMIZED_BOTH);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public void menuInferior() {
         getContentPane().add("Soult", jpBotoes);
         jpBotoes.setLayout(new GridLayout());
    }

    public void adicionaItensMenu() {
        adicionaItemNoMenu(jmCadastros, cadRegiao);
        adicionaItemNoMenu(jmCadastros, cadProduto);
        adicionaItemNoMenu(jmCadastros, cadAlunoF);
        adicionaItemNoMenu(jmCadastros, cadExercicio);
        adicionaItemNoMenu(cadRegiao, jmiPais);
        adicionaItemNoMenu(cadRegiao, jmiEstado);
        adicionaItemNoMenu(cadRegiao, jmiCidade);
        adicionaItemNoMenu(cadAlunoF, jmiAluno);
        adicionaItemNoMenu(cadAlunoF, jmiMatricula);
        JSeparator sep = new JSeparator();
        jmiMatricula.add(sep);
        adicionaItemNoMenu(cadAlunoF, jmiGrupoMatricula);
        adicionaItemNoMenu(cadExercicio, jmiGrupoMuscular);
        adicionaItemNoMenu(cadAlunoF, jmiMatriculaModalidade);
        adicionaItemNoMenu(cadAlunoF, jmiTurmaAlunos);
        adicionaItemNoMenu(cadExercicio, jmiExercicios);
        adicionaItemNoMenu(cadExercicio, jmiModalidade);
        adicionaItemNoMenu(jmCadastros, jmiFormaDePagamento);
        adicionaItemNoMenu(cadAlunoF, jmiFornecedor);
//        JSeparator sep1 = new JSeparator();
//        cadProduto.add(sep1);        
        adicionaItemNoMenu(cadProduto, jmiMarca);
        adicionaItemNoMenu(cadProduto, jmiTipo);
        adicionaItemNoMenu(cadProduto, jmiModelo);
        adicionaItemNoMenu(cadProduto, jmiLote);
        adicionaItemNoMenu(cadProduto, jmiProduto);
        adicionaItemNoMenu(cadExercicio, jmiTreino);
        adicionaItemNoMenu(jmCadastros, jmiPedidoCompra);
        adicionaItemNoMenu(cadAlunoF, jmiAvaliacaoFisica);
        //adicionaItemNoMenu(jmCadastros, jmiTreino);
        adicionaItemNoMenu(cadAlunoF, jmiTurma);
        adicionaItemNoMenu(jmMovimentacoes, jmiCompra);
        adicionaItemNoMenu(jmMovimentacoes, jmiVenda);
        adicionaItemNoMenu(jmMovimentacoes, jmiContaPagar);
        adicionaItemNoMenu(jmMovimentacoes, jmiContaReceber);
        adicionaItemNoMenu(jmMovimentacoes, jmiCaixa);
        adicionaItemNoMenu(jmRelatorios, jmiRelatorioCliente);
    }

    public void adicionaItemNoMenu(JMenu menu, JMenuItem menuItem) {
        menu.add(menuItem);
        menuItem.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent ae) {
        if (ae.getSource() == jmiPais) {
            TelaCadastroPais.getTela();

        } else if (ae.getSource() == jmiEstado) {
            TelaCadastroEstado.getTela();

        } else if (ae.getSource() == jmiCidade) {
            TelaCadastroCidade.getTela();

        } else if (ae.getSource() == jmiAluno) {
            TelaCadastroAluno.getTela();

        } else if (ae.getSource() == jmiMatricula) {
            TelaCadastroMatricula.getTela();

        } else if (ae.getSource() == jmiGrupoMatricula) {
            TelaCadastroGrupoMatricula.getTela();

        } else if (ae.getSource() == jmiGrupoMuscular) {
            TelaCadastroGrupoMuscular.getTela();

        } else if (ae.getSource() == jmiExercicios) {
            TelaCadastroExercicios.getTela();

        } else if (ae.getSource() == jmiModalidade) {
            TelaCadastroModalidade.getTela();

        } else if (ae.getSource() == jmiMatriculaModalidade) {
            TelaCadastroMatriculaModalidade.getTela();

        } else if (ae.getSource() == jmiFormaDePagamento) {
            TelaCadastroFormaPagamento.getTela();

        } else if (ae.getSource() == jmiFornecedor) {
            TelaCadastroFornecedor.getTela();

        } else if (ae.getSource() == jmiMarca) {
            TelaCadastroMarca.getTela();

        } else if (ae.getSource() == jmiTipo) {
            TelaCadastroTipo.getTela();

        } else if (ae.getSource() == jmiModelo) {
            TelaCadastroModelo.getTela();

        } else if (ae.getSource() == jmiLote) {
            TelaCadastroLoteProduto.getTela();

        } else if (ae.getSource() == jmiProduto) {
            TelaCadastroProduto.getTela();

        } else if (ae.getSource() == jmiTreino) {
            TelaCadastroTreino.getTela();

        } else if (ae.getSource() == jmiPedidoCompra) {
            TelaCadastroPedidoCompra.getTela();

        } else if (ae.getSource() == jmiAvaliacaoFisica) {
            TelaCadastroAvaliacaoFisica.getTela();

        } else if (ae.getSource() == jmiTurma) {
            TelaCadastroTurma.getTela();

        } else if (ae.getSource() == jmiTurmaAlunos) {
            TelaCadastroTurmaAlunos.getTela();

        } else if (ae.getSource() == jmiCompra) {
            TelaCadastroCompra.getTela();

        } else if (ae.getSource() == jmiVenda) {
            //  TelaCadastroVenda.getTela();
        } else if (ae.getSource() == jmiContaPagar) {
            TelaCadastroContaPagar.getTela();

        } else if (ae.getSource() == jmiContaReceber) {
            //  TelaCadastroContaReceber.getTela();

        } else if (ae.getSource() == jmiCaixa) {
            //TelaCadastroCaixa.getTela();

        } else if (ae.getSource() == jmiRelatorioCliente) {

        }
    }

    public void InsereIcones() {
        jmCadastros.setIcon(new ImageIcon(getClass().getResource("/imagens/cadastros.png")));
        jmMovimentacoes.setIcon(new ImageIcon(getClass().getResource("/imagens/movimentacao.png")));
        jmRelatorios.setIcon(new ImageIcon(getClass().getResource("/imagens/relatorio.png")));
        jmiFornecedor.setIcon(new ImageIcon(getClass().getResource("/imagens/fornecedor.png")));
        // jmiFuncionario.setIcon(new ImageIcon(getClass().getResource("/imagens/funcionario.png")));
        jmiProduto.setIcon(new ImageIcon(getClass().getResource("/imagens/produto1.png")));
        jmiEstado.setIcon(new ImageIcon(getClass().getResource("/imagens/cidade.png")));
        cadRegiao.setIcon(new ImageIcon(getClass().getResource("/imagens/Regiao.png")));
        jmiEstado.setIcon(new ImageIcon(getClass().getResource("/imagens/cidade.png")));
        jmiCidade.setIcon(new ImageIcon(getClass().getResource("/imagens/cidade.png")));
        jmiPais.setIcon(new ImageIcon(getClass().getResource("/imagens/Regiao.png")));
        jmiExercicios.setIcon(new ImageIcon(getClass().getResource("/imagens/exercicio.png")));
        jmiCompra.setIcon(new ImageIcon(getClass().getResource("/imagens/compra1.png")));
        jmiMarca.setIcon(new ImageIcon(getClass().getResource("/imagens/marca.png")));
        //  jmiTipo.setIcon(new ImageIcon(getClass().getResource("/imagens/tipo.png")));
    }

    public void InsereLogo() {
        URL url = this.getClass().getResource("/imagens/FireFitness.png");
        Image iconeTitulo = Toolkit.getDefaultToolkit().getImage(url);
        this.setIconImage(iconeTitulo);
    }

    public void centralizar(JInternalFrame janela) {
        Dimension d = janela.getDesktopPane().getSize();
        janela.setLocation((d.width - janela.getSize().width) / 2, (d.height - janela.getSize().height) / 2);
    }
}
  • import componente.MeuJDesktopPane; - Put the code of this class in the question too.

  • There are a lot of classes in this ai code that prevent you from running it. Either you remove the dependencies or add all these classes to the question.

  • Are you sure you want a menu, not just a status bar you need? A Jpanel would be enough for that, as the answers show.

1 answer

2

to set time and runtime or even some part of configuration.

For this quoted, just put one JPanel in the BorderLayout.SOUTH, and give it an edge to visually separate from the rest of the content.

Ex:

public class FrmExemploStatusBar {

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setLayout(new BorderLayout());
    frame.setSize(200, 200);

    // cria a barra inferior e insere no espaço inferior do JFrame
    JPanel statusPanel = new JPanel();
    statusPanel.setBorder(new BevelBorder(BevelBorder.LOWERED));
    frame.add(statusPanel, BorderLayout.SOUTH);
    statusPanel.setPreferredSize(new Dimension(frame.getWidth(), 16));
    statusPanel.setLayout(new BoxLayout(statusPanel, BoxLayout.X_AXIS));
    JLabel statusLabel = new JLabel("status");
    statusLabel.setHorizontalAlignment(SwingConstants.LEFT);
    statusPanel.add(statusLabel);

    frame.setVisible(true);
    }
}

Example result:

inserir a descrição da imagem aqui

Using Jmenubar

public class FrmExemploStatusBar {

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setLayout(new BorderLayout());
    frame.setSize(200, 200);

    JMenuBar menuBar = new JMenuBar();
    JMenu menu = new JMenu("Teste");
    menuBar.add(menu);
    JMenuItem menuItem = new JMenuItem("Stack");
    menu.add(menuItem);
    frame.add(menuBar, BorderLayout.SOUTH); 

    frame.setVisible(true);
    }
}

inserir a descrição da imagem aqui

  • You don’t add Jmenubar that way, only with setJMenuBar(). No wonder there is a method just to add it on the screen.

  • Can you explain why not? To say that it is wrong only because there is a method that facilitates the insertion of a standard form is a mistake.

  • Did you ever test? The menu is down, but look at the way the options open. If it is a full screen application it will not even be possible to access some options, because the orientation of the items is still vertical below the screen. Although it works, it doesn’t mean it’s right to do.

  • It’s like this because the dimension of JFrame in the code is 200x200, if it is full screen the same opens up. Only test.

  • The correct one would be the menu always follow its positioning orientation, and not only according to the size of the screen. That is, it should always open up, as occurs when adding on top. There is another small detail, if you add the menu using borderlayout, how will you display a panel of hours, as the OP said? The borderlayout will "inflate" the menu all over the bottom.

  • The "correct" would be to put Jmenu at the top. This is if it is what the question requires (it is not clear), because from what I understand, the first solution is what the question needs (we wait for the author). What I have presented are ideas that can help and forward the question so he doesn’t have to develop a component from scratch. I find your notes on undesirable behavior valid, but not affirmations without testing at least the code.

  • Everything I said was based on tests. Why would I make statements without certainty? It is no use giving an answer that answers the question but will generate other problems. "Teaching wrong" is not the way. If the question is not clear, the recommended is to do as I and the Victor did, request better clarification. Anyway, there’s no point in beating my mouth here, my intention was to warn errors in response, but you have every right to disagree, just as I have to vote negative because I think it’s not good.

  • I meant the part about saying JMenu would open down even in full screen. I agree with you on everything, and I say that these discussions reinvigorate our reasoning. Let’s wait for clarification

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.