Reputation: 11
I got a issue here, my buttons were working fine until I added a background image, when I try to put my mouse over the buttons and textfield for them to show. My labels also don't work either regardless if my mouse is clicking it or on it? Heres the code to the class.
public class launcher extends JFrame{
private static final long serialVersionUID = 1L;
protected JPanel window = new JPanel();
protected JFrame f = new JFrame("stackoverflow");
private Rectangle rAncient, rMedieval, rModern, rFuture, rFinancial, rUpdate;
private JButton ancient, medieval, modern, future, financial, update;
private JLabel time, name;
private JTextField tName;
protected int width = 600;
protected int height = 400;
protected int button_width = 80;
protected int button_height = 40;
public launcher(int id) throws IOException{
try{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e){
e.printStackTrace();
}
setTitle("Choose your Path");
setSize(new Dimension(width, height));
setDefaultCloseOperation(EXIT_ON_CLOSE);
getContentPane().add(window);
setLocationRelativeTo(null);
setResizable(false);
setVisible(true);
window.setLayout(null);
if(id==0){
drawButtons();
}
repaint();
}
private void drawButtons(){
ancient = new JButton("Ancient");
rAncient = new Rectangle(140, 150, 70, 40);
ancient.setBounds(rAncient);
window.add(ancient);
medieval = new JButton("Medieval");
rMedieval = new Rectangle(220, 150, 80, 40);
medieval.setBounds(rMedieval);
window.add(medieval);
modern = new JButton("Modern");
rModern = new Rectangle(310, 150, 70, 40);
modern.setBounds(rModern);
window.add(modern);
future = new JButton("Future");
rFuture = new Rectangle(350, 150, 70, 40);
future.setBounds(rFuture);
window.add(future);
financial = new JButton("Financial");
rFinancial = new Rectangle(390, 150, 70, 40);
future.setBounds(rFinancial);
window.add(financial);
update = new JButton("Update");
rUpdate = new Rectangle(250, 300, 100, 50);
update.setBounds(rUpdate);
window.add(update);
time = new JLabel("Choose your desired time period");
time.setBounds(220, 90, 200, 50);
window.add(time);
name = new JLabel("Name: ");
name.setBounds(210, 220, 200, 50);
window.add(name);
tName = new JTextField();
tName.setBounds(250, 235, 150, 20);
window.add(tName);
tName.setText("Bob");
ancient.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent a){
System.out.println("Starting in Ancient mode...");
}
});
medieval.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent a){
System.out.println("Starting in Medieval mode...");
}
});
modern.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent a){
System.out.println("Starting in Modern mode...");
}
});
future.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent a){
System.out.println("Starting in Future mode...");
}
});
financial.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent a){
System.out.println("Starting in Financial mode...");
}
});
update.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent a){
System.out.println("Opening browser to http://ubergamesproductions.weebly.com/adventure");
try{
String URL ="http://ubergamesproductions.weebly.com/adventure";
java.awt.Desktop.getDesktop().browse(java.net.URI.create(URL));
}catch(Exception e){
JOptionPane.showMessageDialog(null, e.getMessage());
}
}
});
}
public static void main(String[] args)throws IOException{
new launcher(0);
}
private Image backgroundImage = ImageIO.read(new File("C:/Users/Samuels Laptop/Dropbox/Java/Questor/Launcher_UGP.png"));
public void paint(Graphics g) {
super.paint(g);
g.drawImage(backgroundImage, 0, 0, null);
}
};
Upvotes: 1
Views: 333
Reputation: 324157
public launcher(int id) throws IOException{
Use standard Java naming conventions. Class names should start with an upper case character.
window.setLayout(null);
...
ancient = new JButton("Ancient");
rAncient = new Rectangle(140, 150, 70, 40);
ancient.setBounds(rAncient);
Don't use a null layout and setBounds(...). Use a proper layout manager and let the layout manager do its job. In this case you can probably use a JPanel and its default FlowLayout.
public void paint(Graphics g) {
super.paint(g);
g.drawImage(backgroundImage, 0, 0, null);
Don't override the paint() method of a top level container. In your case your code paints the frame and all of the components. Then you draw the image. So the image covers the components. The button respondes to mouseEntered events to repaint() the border so that is why it suddenly appears.
Instead, custom painting is done by overriding the paintComponent(...) method of a JPanel. Then you add the panel to the frame and you add your components to the panel. Read the section from the Swing tutorial on Custom Painting for more information. Take the time to read the whole tutorial. There is also a section on Layout Managers
.
Upvotes: 3