Java swing - exercise

Asked

Viewed 196 times

1

I’m having doubts about Container, I’d like to know if the form I’ve made is correct.

public class Exercicio2 {
public static void main(String[] args) {

    JLabel lblTitulo = new JLabel(); 
    JLabel lblLogin = new JLabel();
    JLabel lblPassword = new JLabel(); 
    JTextField jTFLogin = new JTextField();
    JPasswordField jPFPassword = new JPasswordField();
    JButton btnOk = new JButton();
    JButton btnCancelar = new JButton();        
    JFrame jFrameLogin = new JFrame();          
    Container container = jFrameLogin.getContentPane();
    jFrameLogin.setContentPane(container);
    container.setLayout(null);
    lblTitulo.setFont(new Font("Arial",Font.BOLD, 12));
    lblTitulo.setBounds(90, 20, 180, 185);
    lblTitulo.setForeground(Color.red);
    container.add(lblTitulo);
    lblLogin.setText("Insira seu login!");
    container.add(lblLogin);
    lblPassword.setBounds(20, 90, 53, 15);
    container.add(lblPassword);
    jTFLogin.setBounds(80, 90, 210, 20);
    jTFLogin.setText("Insira seu login!");
    container.add(jTFLogin);
    jPFPassword.setBounds(80, 90, 210, 20);
    container.add(jPFPassword);
    JPanel jPanelBotoes = new JPanel();
    jPanelBotoes.setBounds(30, 120, 280, 60);
    jPanelBotoes.setLayout(null);
    jPanelBotoes.setBackground(Color.blue);
    container.add(jPanelBotoes);
    btnOk.setBounds(70, 20, 47, 23);
    jPanelBotoes.add(btnOk);
    btnCancelar.setBounds(150, 20, 75, 23);
    btnCancelar.setToolTipText("Sair do Programa");
    jPanelBotoes.add(btnCancelar);   
    }}

The exercise calls for:

jFrameLogin: use pack(); Set Intermediate Container to setLayout(null);

  • What have you ever done? What do you doubt?

  • I do not know how to do the part of the letter "d", I could not do anything only created the class and the components, when it arrives in the intermediate container I could not understand how it does

  • Mainly this part: set the size (pack( )); set the visibility (setVisible(true); remember to assign an intermediate container and associate it to the container frame, using the getContentPane() method, set the Layout to null;

  • There’s no such thing as an intermediate container. What this statement means is for you to create any container(With jpanel, I imagine) and set as contentpane, which is the main container of jframe. pack() will do nothing in this code of yours, because you are not using layout and nor preferred size with setPreferredSize

  • So I have to do it this way? Jframe frame = new Jframe(); Jpanel container = new Jpanel(); container.add(frame) is that way?

  • Not. frame.setContentPane("aqui você define um container"). contentpane is frame, you do not add a frame-like window inside anything, because it is top level. You’d better edit the question and leave only the piece of exercise that gave you doubt, and add what you’ve already done with the code.

  • So I did the other exercise that’s smaller and edited the question.

  • 1

    By the way, what’s your question? I saw no problem with the code, unless you use the Container class to recover the contentpane and then set as contentpane.

  • Know if it was correct, but as you said there is no problem, blz! vlw

  • There is a little problem yes, see what I wrote at the end of the comment. Container container = jFrameLogin.getContentPane(); jFrameLogin.setContentPane(container); This section is doing nothing, it’s completely discardable in your code. I think your teacher’s idea wasn’t to create a JPANEL and use it as a container.

  • When I said Container, you were supposed to use a component with this feature, not necessarily this class. Jpanel is a container, for example. I think you were supposed to use it to add everything and then set it to jframe contentpane.

  • So I had seen this part, I discarded jFrameLogin.setContentPane(container), but I understood the question you mentioned regarding the use of the Container class, I could have used a Jpanel and use it as a container, but it worked here. Vlw

Show 7 more comments
No answers

Browser other questions tagged

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