Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Java
//Main Class:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Order
{
	public static void main(String[] args)
	{
		OrderFrame frame = new OrderFrame();
	}
}

//Frame Class:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class OrderFrame extends JFrame
{
	OrderFrame()
	{
		setTitle("Place Order");
		setLayout(null);
		setSize(500,500);
		this.add(new OrderPanel());
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
}

//Panel Class:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class OrderPanel extends JPanel implements ActionListener
{	
	JLabel l1,l2;
	JButton b1;
	public OrderPanel()
	{
		l1 = new JLabel("Welcome to Online Food Delivery System");
		b1 = new JButton("Close Window");
		b1.addActionListener(this);
		add(l1);
		add(b1);
	}
	public void actionPerformed(ActionEvent e) 
	{
		System.exit(0);
	}
}


What I have tried:

I still have to write a lot of code, but I if I run the Main Class(Class name - order) I should get a GUI with a Label and a button. But I get a blank GUI. I tried writing this.add(b1) and this.add(l1) but with no change. What am I missing?
Posted
Updated 26-May-17 21:20pm
v2

1 solution

I'd suggest to read this: Java JButton - javatpoint[^]

Java
import java.awt.event.*;
import javax.swing.*;
public class ButtonExample {
public static void main(String[] args) {
    JFrame f=new JFrame("Button Example");
    final JTextField tf=new JTextField();
    tf.setBounds(50,50, 150,20);
    JButton b=new JButton("Click Here");
    b.setBounds(50,100,95,30);
    b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
            tf.setText("Welcome to Javatpoint.");
        }
    });
    f.add(b);f.add(tf);
    f.setSize(400,400);
    f.setLayout(null);
    f.setVisible(true);
}
}


Based on above code, you missed this line:
Java
jcontrol.setBounds(50,50, 150,20);
 
Share this answer
 
v3
Comments
Member 13225387 27-May-17 7:27am    
If you want to say I add final keyword before the Label and Button and add the b1.setBounds(x,y,width,height) and l1.setBounds(x,y,width,height), I did that but still did not get anything in the GUI

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900