Practicing, making a window with a button

0

I'm a newbie in Java and my first code, is to make a JFrame modal window that you can modify the background and have a button, but I do not get it, I do not know why:

package practica1;
import javax.swing.*;
import java.awt.Color;

public class pantalla extends JFrame{
   public static void main(String[] args) {
       pantalla v = new pantalla12();
       v.setVisible(true);
       v.setBounds(300,400,2000, 1000);
       v.setTitle("hola");
       v.setBackground(Color.green);
   }    
}

pantalla12.java

package practica1;

public class pantalla12 extends pantalla {

  JButton boton;
  JPanel panel;

  this(panel);
  panel.add(boton);

}

I only get the modal window without the background but the rest is not.

Why will it be?

Thanks

    
asked by cat_12 17.06.2018 в 01:23
source

3 answers

0

Well first to modify the background of a JFrame you must place the following line.

getContentPane().setBackground(aqui el color que quieres);

To add components such as JPanel, JButton, JTextField, you must first instantiate and initialize them in order to add them. Example:

For a button:

JButton elBoton = new JButton("Soy un Boton");

For a panel:

JPanel elPanel = new JPanel();

Then you can add them

add(elBoton);
add(elPanel);

The setVisible () method I recommend that you always leave it to the last, after having made all the modifications.

Here is a brief example:

package Ejemplos;

import java.awt.*;

import javax.swing.*;

public class VentanaConBoton extends JFrame {

private JButton boton;

public VentanaConBoton() {

    setTitle("Ejemplo");
    setSize(500, 500);
    setLayout(null);
    getContentPane().setBackground(Color.GREEN);

    boton = new JButton("Boton Ejemplo");
    boton.setBounds(50, 50, 180, 180);

    add(boton);

    setVisible(true);

}

public static void main(String[] args) {

    VentanaConBoton v = new VentanaConBoton();

}
}

When you run it would look like this:

    
answered by 17.06.2018 в 03:44
0

In the end it worked out for me:

pantalla.java:

package practica1;
import javax.swing.*;
import java.awt.Color;
import java.awt.Rectangle;

public class pantalla extends JFrame{
   public static void main(String[] args) {
      pantalla v = new backend();
      v.setVisible(true);
      v.setSize(2000, 1000);
      v.setLocationRelativeTo(null);
      v.setTitle("Primera Practica");
   }    
}

backend.java:

package practica1;
import javax.swing.*;
import java.awt.Color;
import java.awt.Rectangle;
public class backend extends pantalla{
   JPanel panel;
   JButton boton;
   JLabel texto;
   public backend() {
      JPanel panel = new JPanel();
      JLabel texto = new JLabel();
      JButton boton = new JButton();

      add(panel);
      panel.add(boton);
      panel.add(texto);
      panel.setBackground(Color.CYAN);
      boton.setText("Soy un boton");
      boton.setBackground(Color.green);
      boton.setSize(500,500);
      texto.setSize(40,40);
      texto.setBounds(10,10,100, 40);
      texto.setText("Soy un texto");
   }
}

But I want to enlarge the objects, but does not it go because it happens?

Thanks

    
answered by 18.06.2018 в 01:58
0

To your panel you must place the Layout in null so that the setBounds of the components serve properly.

panel.setLayout(null); 

With that line and your setBounds of the components you modify the size and place it in the position that you indicate.

    
answered by 20.06.2018 в 23:20