JFrames Edition

2

I have the following code that corresponds to the Header Panel but does not give me the desired output in the image. Can anyone help me?

publicclassPainelEstadoContactoextendsJPanel{/****/privatestaticfinallongserialVersionUID=1L;privateJanelaUtilizadorjanelaUtilizador;privateJButtonoffline;privateJButtononline;publicPainelEstadoContacto(JanelaUtilizadorjanelaUtilizador){this.janelaUtilizador=janelaUtilizador;offline=newJButton("OFFLINE");
    offline.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {

        }
    });
    online= new JButton("ONLINE");
    online.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub

        }
    });

    setLayout(new GridLayout(1,3));

    JPanel painel = new JPanel();
    painel.setLayout(new BorderLayout());

    JLabel utilizador = new JLabel("Utilizador:");
    JTextField user = new JTextField();

    painel.add(utilizador, BorderLayout.NORTH);
    painel.add(user, BorderLayout.SOUTH);

    add(painel);
    add(online);
    add(offline);
}

}

My result of the code was this:

    
asked by anonymous 03.12.2015 / 23:33

1 answer

1

A GridLayout simply divides the available space into the specified number (in this case, 1 row and 3 columns), and causes each element to occupy this whole space. In your case this gives two problems because:

  • The text box becomes too small, the same size as each button;
  • The buttons occupy the entire space, becoming "stretched."
  • In addition, I notice that you placed the label and user box inside a panel, and used this panel in GridLayout . Ideally, the label should be on the main panel, and the box and two buttons inside it.

    I'm going to give a partial answer (because I can not test this now, and I have not worked with Java for years, I'm not familiar with the new layout manager and 3 but not 1. I will also try to risk a code to solve 1, using GridBagLayout , but I can not guarantee that it will work because my memory is half failing ...

    setLayout(new BorderLayout());
    
    // O label vai no norte do cabeçalho
    JLabel utilizador = new JLabel("Utilizador:");
    add(utilizador, BorderLayout.NORTH);
    
    // E o resto vai no centro
    JPanel painel = new JPanel();
    add(painel, BorderLayout.CENTER);
    
    // O centro se divide em 3
    painel.setLayout(new GridLayout(1,3));
    
    // A caixa de texto e os 3 botões vão no centro
    JTextField user = new JTextField();
    JPanel auxOnline = new JPanel();
    JPanel auxOffline = new JPanel();
    
    painel.add(user);
    painel.add(auxOnline);
    painel.add(auxOffline);
    
    // Mas os botões não vão diretamente pro centro; em vez disso, um
    // painel auxiliar é criado para cada um deles de modo que não estiquem.
    auxOnline.setLayout(new FlowLayout());
    auxOnline.add(online);
    auxOffline.setLayout(new FlowLayout());
    auxOffline.add(offline);
    

    To make the text box grow and occupy as much space as possible, but the buttons remain their preferred size, an alternative is to use GridBagLayout . Specifying where each element is is a bit more annoying, but the end result is a much greater flexibility in positioning:

    setLayout(new BorderLayout());
    
    // O label vai no norte do cabeçalho
    JLabel utilizador = new JLabel("Utilizador:");
    add(utilizador, BorderLayout.NORTH);
    
    // E o resto vai no centro
    JPanel painel = new JPanel();
    add(painel, BorderLayout.CENTER);
    
    // Em vez de Grid, usa GridBag
    painel.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    
    c.insets = new Insets(5, 5, 5, 5); // Margem externa de todos os componentes
    
    // Coloca a caixa de texto; cresce horizontalmente
    c.gridx = 0;
    c.gridy = 0;
    c.weightx = 1;
    painel.add(user, c);
    
    // Coloca os botões; não cresce
    c.weightx = 0;
    
    c.gridx++;
    painel.add(online, c); // Não precisa de painel auxiliar
    c.gridx++;
    painel.add(offline, c);
    

    In the end, it will still be a 1x3 grid, but the first column will grow by the size of the window, while the other two will keep their preferred size.

    GridBagLayout tutorial

        
    04.12.2015 / 00:20