Your current problem is that your code above does not respect the layout managers that are in use by default.
The best solution is to use layout managers to do the laying out of components for you including nesting JPanels, each using its own layout. Some may suggest that you use a null layout, and in most cases that is the wrong thing to do as it makes your program very hard to maintain and close to impossible to upgrade.
By the way, where are you trying to place the button? In the lower right hand corner of your GUI?
Also, rather than using a MouseListener with your JButton, which is usually a bad idea, butter to add a ChangeListener to the JButton's model. Then you can see if the mouse is over the button easily.
Edit
You state:
Yes, the lower right hand corner.
Then one way is to use a GridBagLayout and place the button in the right lower corner by using appropriate constants in your GridBagConstraints parameter.
Edit 1
For example:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class StartScreen extends JPanel implements ActionListener {
private static final int PREF_W = 1200;
private static final int PREF_H = 720;
JButton buttonOptions = new JButton("Options");
@Override
public Dimension getPreferredSize() {
return new Dimension(PREF_W, PREF_H);
}
public StartScreen() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0,
GridBagConstraints.SOUTHEAST, GridBagConstraints.NONE, new Insets(
5, 5, 5, 5), 0, 0);
add(buttonOptions, gbc);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
private static void createAndShowGui() {
StartScreen mainPanel = new StartScreen();
JFrame frame = new JFrame("StartScreen");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
Edit 2
Now with Icons that change on hover or "roll-over". And I was wrong -- there's no need to listen to the ButtonModel's state. Simply set the button's Icon and its rollover Icon, and the Button swaps them for you:
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
@SuppressWarnings("serial")
public class StartScreen extends JPanel {
private static final int PREF_W = 1200;
private static final int PREF_H = 720;
private static final int BI_WIDTH = 100;
private static final int BI_HEIGHT = 30;
private JButton buttonOptions = new JButton();
private Icon nonHoveredIcon;
private Icon hoveredIcon;
@Override
public Dimension getPreferredSize() {
return new Dimension(PREF_W, PREF_H);
}
public StartScreen() {
hoveredIcon = createIcon("Hovered");
nonHoveredIcon = createIcon("Non-Hovered");
buttonOptions.setIcon(nonHoveredIcon);
buttonOptions.setRolloverIcon(hoveredIcon);
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0,
GridBagConstraints.SOUTHEAST, GridBagConstraints.NONE, new Insets(
5, 5, 5, 5), 0, 0);
add(buttonOptions, gbc);
}
private ImageIcon createIcon(String text) {
BufferedImage img = new BufferedImage(BI_WIDTH, BI_HEIGHT,
BufferedImage.TYPE_INT_ARGB);
Graphics g = img.getGraphics();
g.setColor(Color.black);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g.drawString(text, 10, 20);
g.dispose();
return new ImageIcon(img);
}
private static void createAndShowGui() {
StartScreen mainPanel = new StartScreen();
JFrame frame = new JFrame("StartScreen");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}