I am creating a GUI application. I have a background image and a title at the top of the page. At the bottom, i want to place to JButtons next to each other. This is my code
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.swing.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import javax.imageio.ImageIO;
public class TitlePage extends JPanel
{
private static final String FILE_PATH = "C:\\Documents and Settings\\Maureen\\Desktop\\java coursework\\nina\\yr3 work\\olympic.png";
private JButton registErJButton;
private JButton logInJButton;
private BufferedImage image = null;
public TitlePage()
{
try
{
image = ImageIO.read(new File(FILE_PATH));// load the image
}
catch (IOException e)
{
e.printStackTrace();
}
// set up the JPanel.
JLabel titleLabel = new JLabel("LONDON 2012");
titleLabel.setFont(titleLabel.getFont().deriveFont(Font.BOLD, 48));
titleLabel.setForeground(Color.blue);
JPanel titlePanel = new JPanel();
titlePanel.setOpaque(false);
titlePanel.add(titleLabel);
registErJButton = new JButton();
registErJButton.setText( "REGISTER" );
registErJButton.setFont(new Font( "Default", Font.BOLD, 16 ) );
registErJButton.addActionListener(
new ActionListener()
{
public void actionPerformed( ActionEvent event )
{
registErJButtonActionPerformed( event );
}
}
);
logInJButton = new JButton();
logInJButton.setText( "LOGIN" );
logInJButton.setFont(new Font( "Default", Font.BOLD, 16 ) );
logInJButton.addActionListener(
new ActionListener()
{
public void actionPerformed( ActionEvent event )
{
logInJButtonActionPerformed( event );
}
}
);
setPreferredSize(new Dimension(800, 590));
setLayout(new BorderLayout());
add(titlePanel, BorderLayout.NORTH);
//THIS IS WHERE MY PROBLEM LIES
//FIRST BUTTON DISPLAYS, I NEED THE SECOND BUTTON NEXT OR ONTOP OF IT
JPanel pan = new JPanel(new FlowLayout());
registErJButton.setPreferredSize(new Dimension(200,50));
pan.add(registErJButton);
add(pan, BorderLayout.SOUTH);
/*
JPanel pan1 = new JPanel(new FlowLayout());
logInJButton.setPreferredSize(new Dimension(200,50));
pan1.add(logInJButton);
add(pan1, BorderLayout.LINE_START);
*/
}
protected void paintComponent(Graphics g) //overriding paint method
{
super.paintComponent(g);
if (image != null)
{
g.drawImage(image, 104, -100, this); //applying my image
}
}
private static void createAndShowUI()
{
JFrame frame = new JFrame("Nerkesa Pignatelli");
frame.getContentPane().add(new TitlePage()); // adding the JPanel to the JFrame here
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private void registErJButtonActionPerformed( ActionEvent event )
{
}
private void logInJButtonActionPerformed( ActionEvent event )
{
}
public static void main(String[] args)
{
java.awt.EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowUI();
}
});
}
}
I can place one button at the bottom using SOUTH. How do i place another button next to this or stacked above it? I know i have to use a different layout style, maybe grid, but i not sure how and the tutorials are hard for me to follow.
cheers