GUI Layout - HELP!!!! :)

Hi, for some reason, i have created many frames but this particular one displays the title (information) puts a jbutton next to it and then 2 more underneath and 2 more under that. I wanted it sort of list form where title appears at top then 2 next row and 2 next. hpe u get me, the code used is below

public InformationGUI(){

super("Information");

Label title =new Label(" INFORMATION", JLabel.CENTER);

font =new Font("Ariel", Font.BOLD, 20);

title.setBackground(Color.red);

title.setFont(font);

viewinfo=new JButton("view");

viewinfo.addActionListener(this);

addinfo =new JButton("add");

addinfo.addActionListener(this);

deleteinfo =new JButton("delete");

deleteinfo.addActionListener(this);

amendinfo=new JButton("amend");

amendinfo.addActionListener(this);

back =new JButton("GO BACK");

back.addActionListener(this);

Container contentPane = this.getContentPane();

contentPane.setLayout(new BorderLayout());

this.setBackground(Color.blue);

contentPane.setLayout(new GridLayout(5,1,5,1));

contentPane.add(title);

this.setLayout(new GridLayout(5,2));

this.add(addinfo);

this.add(viewinfo);

this.add(deleteinfo);

this.add(amendinfo);

this.add(back);

this.pack();

this.setVisible(true);

ClosingWindow close =new ClosingWindow();

addWindowListener(close);

}

[2343 byte] By [javaking312a] at [2007-11-26 13:02:51]
# 1

This is a bit messy. Here are my reasons to say this:

- Your code mixes AWT and Swing(Label and JButton);

- Are you sure there is a font called Ariel or you meant Arial;

- getContentPane() and this.add(...)? What do you mean by that? What did you extend - JFrame or Frame?

Mike

bellyrippera at 2007-7-7 17:06:28 > top of Java-index,Java Essentials,Java Programming...
# 2
put the label in the North pane and the buttons in the center?I thought that the code looked funny. I don't know anything about awt so I didn't say anything.Message was edited by: Ledition
Leditiona at 2007-7-7 17:06:28 > top of Java-index,Java Essentials,Java Programming...
# 3
Use a BorderLayout. Put your title in BorderLayout.NORTH.Make a new panel. Set its layout to GridLayout. Add your four buttonsto this panel. Add the pane to BorderLayout.CENTER.
es5f2000a at 2007-7-7 17:06:28 > top of Java-index,Java Essentials,Java Programming...
# 4

do you mean like this below. i have added title to pane. how do i set north. am i on the right lines

Container contentPane = this.getContentPane();

contentPane.setLayout(new BorderLayout());

this.setBackground(Color.blue);

this.setLayout(new GridLayout(3,3));

this.add(title);

this.setLayout(new GridLayout(3,3));

this.add(addinfo);

this.add(deleteinfo);

this.add(viewinfo);

this.add(amendinfo);

this.add(back);

javaking312a at 2007-7-7 17:06:28 > top of Java-index,Java Essentials,Java Programming...
# 5

Why don't you just use swing? You are already using JButtons, just make a

JFrame

2 JPanels

add the title to the first JPanel

add the buttons to the second JPanel

add the first JPanel to the JFrame's North pane

add the second JPanel to the JFrame's Center pane

yourJFrame.getContentPane().add(BorderLayout.NORTH,panel1);

yourJFrame.getContentPane().add(borderLayout.CENTER,panel2);

Leditiona at 2007-7-7 17:06:28 > top of Java-index,Java Essentials,Java Programming...
# 6

ok i have done this and an getting the following error

Here is the code, error is undrerneath it.

JPanel p = new JPanel();

Label info = new Label(" INFORMATION", Label.CENTER);

font = new Font("Arial", Font, 10);

info.setBackground(Color.green);

info.setFont(font);

p.add(info);

JPanel q = new JPanel();

add= new JButton("ADD");

add.addActionListener(this);

delete = new JButton("DELETE");

delete.addActionListener(this);

view = new JButton("VIEW");

view.addActionListener(this);

amen = new JButton("AMEND");

amend.addActionListener(this);

back= new JButton("BACK");

back.addActionListener(this);

q.add(add);

q.add(delete);

q.add(view);

q.add(amend);

q.add(main);

Container contentPane = this.getContentPane();

contentPane.setLayout(new BorderLayout());

this.setBackground(Color.blue);

JFrame r = new JFrame();

r.getContentPane().add(BorderLayout.NORTH,p);

q.getContentPane().add(BorderLayout.CENTER,q);

this.pack();

this.setVisible(true);

error message appearing is

symbol : method getContentPane()

location: class javax.swing.JPanel

q.getContentPane().add(BorderLayout.CENTER,q);

^

ANY IDEAS? Thank you for your help

javaking312a at 2007-7-7 17:06:28 > top of Java-index,Java Essentials,Java Programming...
# 7
q is a JPanel, not a JFrame. Please read the API. APIs are your friend.
filestreama at 2007-7-7 17:06:28 > top of Java-index,Java Essentials,Java Programming...
# 8

JFrame r = new JFrame();

JPanel p = new JPanel();

JLabel info = new JLabel(" INFORMATION");

Font myFont = new Font("Arial", Font, 10);

info.setBackground(Color.green);

info.setFont(myFont);

p.add(info);

JPanel q = new JPanel(new GridLayout(5,2));

add= new JButton("ADD");

add.addActionListener(this);

delete = new JButton("DELETE");

delete.addActionListener(this);

view = new JButton("VIEW");

view.addActionListener(this);

amen = new JButton("AMEND");

amend.addActionListener(this);

back= new JButton("BACK");

back.addActionListener(this);

q.add(add);

q.add(delete);

q.add(view);

q.add(amend);

q.add(back);

q.setBackground(Color.blue);

p.setBackground(Color.blue);

r.getContentPane().add(BorderLayout.NORTH,p);

r.getContentPane().add(BorderLayout.CENTER,q);

r.setVisible(true);

try that

Leditiona at 2007-7-7 17:06:28 > top of Java-index,Java Essentials,Java Programming...
# 9

Note that the below won't make much sense unless you actually

go through it and read the relevant API documentation.

Learn to fish!

If I were going to write your code, I'd do it like this:

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Container;

import java.awt.Font;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.SwingConstants;

public class InformationView {

public static void main(final String[] argv) {

final JFrame mainFrame = new JFrame();

new InformationView(mainFrame.getContentPane());

mainFrame.pack();

mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

mainFrame.setVisible(true);

}

public InformationView(final Container container) {

// Set background color

container.setBackground(Color.GREEN);

// Add title to container

final JLabel info = new JLabel(" INFORMATION", SwingConstants.CENTER);

info.setBackground(Color.GREEN);

info.setFont(new Font("Arial", Font.PLAIN, 10));

container.setLayout(new BorderLayout());

container.add(info, BorderLayout.NORTH);

// Add buttons to container

final JButton addButton = new JButton(new AddAction());

final JButton deleteButton = new JButton("Delete");

final JButton viewButton = new JButton("View");

final JButton amendButton = new JButton("Amend");

final JButton backButton = new JButton("Back");

final JPanel buttonPanel = new JPanel(new GridLayout(3, 2));

buttonPanel.add(addButton);

buttonPanel.add(deleteButton);

buttonPanel.add(viewButton);

buttonPanel.add(amendButton);

buttonPanel.add(backButton);

container.add(buttonPanel, BorderLayout.CENTER);

}

public class AddAction extends AbstractAction {

public AddAction() {

super("Add");

}

public void actionPerformed(final ActionEvent ae) {

// Add functionality here

}

}

}

es5f2000a at 2007-7-7 17:06:28 > top of Java-index,Java Essentials,Java Programming...