Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<pre><code>/*Use this code as an example first run this and then use it for your requirements*/ package mypack; import javax.swing.*; import java.awt.*; public class backgroundClass extends JPanel { //Initializing the class Image Image background; //Setting up GUI public backgroundClass(interfaceAndImage iai) { //Constructing the class "Toolkit" which will be used to manipulate our images. Toolkit kit = Toolkit.getDefaultToolkit(); //Getting the "background.jpg" image we have in the folder background = kit.getImage("d:\\123.jpg"); } //Manipulate Images with JAVA2D API. . creating a paintComponent method. public void paintComponent(Graphics comp) { //Constructing the class Graphics2D. Create 2D by casting the "comp" to Graphics2D Graphics2D comp2D = (Graphics2D)comp; //creating a graphics2d using the images in the folder and place it in a specific coordinates. comp2D.drawImage(background, 0, 0, this); } } </code></pre> <p>/<em>Another class</em>/ package mypack;</p> <pre><code>//Java Core Package import javax.swing.*; //Java Extension Package import java.awt.*; public class interfaceAndImage extends JFrame { //Constructing the class we created called "backgroundClass" so we can //use it here in our main program as a parent panel. backgroundClass bc; //Initializing our JComponents and the labels of our JButton and JLabel JPanel panel1, panel2; JLabel labels[]; JButton choices[]; JTextField inputs[]; String lebelName[] = {"Name:","Age:","Sex:","Address:","Tel. No.:"}; String buttonChoice[] = {"Register","Reset","Cancel"}; //Setting up GUI public interfaceAndImage() { //Setting up the Title of the Window super("How to put a JTextField, JLabel, and JButton above image"); //Set Size of the Window (WIDTH, HEIGHT) setSize(310,170); //Exit Property of the Window setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Constructing the class "backgroundClass" and call the keyword "this" so it can //be recognizable by our main program. bc = new backgroundClass(this); //Constructing JPanel 1 and set its layout property including the background property //which is transparent panel1 = new JPanel(); panel1.setLayout(new GridLayout(5,2)); //Constructing the class Color and set its property to 0,0,0,0 means no color. Color trans = new Color(0,0,0,0); panel1.setBackground(trans); //Setting JPanel 1 background to transparent //Constructing JPanel 2 and set its layout property panel2 = new JPanel(); panel2.setLayout(new GridLayout()); //Constructing our JComponents setting its specific array size labels = new JLabel[5]; inputs = new JTextField[5]; choices = new JButton[3]; //Adding our JPanel 1 and 2 to our class "backgroundClass" which is our parent panel bc.add(panel1, BorderLayout.NORTH); bc.add(panel2, BorderLayout.SOUTH); //Setting up the container ready for the components to be added. Container pane = getContentPane(); setContentPane(pane); //Constructing JLabel and JTextField using "for loop" in their desired order for(int count=0; count&lt;inputs.length &amp;&amp; count&lt;labels.length; count++) { labels[count] = new JLabel(lebelName[count], JLabel.RIGHT); inputs[count] = new JTextField(30); //Adding the JLabel and the JTextFied in JPanel 1 panel1.add(labels[count]); panel1.add(inputs[count]); } //Constructing all 4 JButtons using "for loop" and add them in the panel 1 for(int count=0; count&lt;choices.length; count++) { choices[count] = new JButton(buttonChoice[count]); panel2.add(choices[count]); } //Adding the class "backgroundClass" to our container as parent panel pane.add(bc); /**Set all the Components Visible. * If it is set to "false", the components in the container will not be visible. */ setVisible(true); //Disable window size setResizable(false); } //Main Method public static void main (String[] args) { interfaceAndImage iai = new interfaceAndImage(); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload