Okay, ive got a project and would like to know how to position Jpanels in a Jframe.
I'm making a fruit machine and cannot posistion the buttons and frames as i do not know how too, please may i have some help
Many Thanks
Printable View
Okay, ive got a project and would like to know how to position Jpanels in a Jframe.
I'm making a fruit machine and cannot posistion the buttons and frames as i do not know how too, please may i have some help
Many Thanks
I recommend that you avoid using absolute positioning and instead learn to use the AWT/Swing layout managers. As always, the Sun Swing tutorials will show you the way. Please start here.
Gridlayout() is the easiest way
if you want all of your buttons in one row add them to their own panel and it will automatically do flowlayout. but you shoould read the api on gridlayout do you have the api on your computer? ill post a link
Java 2 Platform SE 5.0
scroll down on the left window till you see gridlayout
they all mention buttons but i want to have Jpanels there as im making a fruit machine and i need images in them
here is my code it wont work, dont kno why. so far only one column and a exit button.
Code:mport java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TheElectronicBandit extends JFrame implements ActionListener
{
private JPanel panelLine1Top, panelLine1Middle, panelLine1Bottom;
private JButton btnClose;
public static void main(String[] args)
{
TheElectronicBandit fruitMachine = new TheElectronicBandit();
}
public void fruitmachine()
{
JPanel Line1 = new JPanel();
setLayout(new BoxLayout(Line1, BoxLayout.X_AXIS));
panelLine1Top = new JPanel();
panelLine1Top.setSize(50, 50);
panelLine1Middle = new JPanel();
panelLine1Middle.setSize(50, 50);
panelLine1Middle.setBackground(Color.blue);
panelLine1Bottom = new JPanel();
panelLine1Bottom.setSize(50, 50);
add(panelLine1Top); add(panelLine1Middle); add(panelLine1Bottom); add(btnClose);
setTitle("The Electronic Bandit - Can you beat the bandit?!");
setSize(500, 500);
setVisible(true);
}
public void actionPerformed(ActionEvent event)
{
if(event.getSource()== btnClose)
{
System.exit(0);
}
}
}
I don't know what you mean here as the tutorials in my link above can be applied to JPanels or any other Swing component.Quote:
they all mention buttons but i want to have Jpanels
Where do you call code that actually creates and displays a GUI? Your main method creates and initializes a TheElectronicBandit object, but this object has no constructor. So what happens is a JFrame-derived object is created, but since it is never displayed, the program just ends.
You need to give this class a constructor and construct your GUI within this constructor.
This code is a bit suspect:
You create a JPanel Line1 and never use it, but then create a BoxLayout which you are adding by default to a JFrame's contentPane, but then tell the box layout to use the Line1 JPanel instead. This will surely cause an error and isn't how BoxLayout is to be used. Please check out the BoxLayout tutorial (you can get to it from my first link above) as it will show you how to use this layout. First and foremost, the component that is within the BoxLayout constructor call must be the same component that sets its layout to boxlayout.Code:JPanel Line1 = new JPanel();
setLayout(new BoxLayout(Line1, BoxLayout.X_AXIS));