Reputation: 1
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
public class SpeedMathGoStartScreen extends JFrame{
JLabel background;
JLabel title;
JButton play;
JPanel playPanel;
JLabel playLabel;
JButton guide;
JPanel guidePanel;
public SpeedMathGoStartScreen() {
title = new JLabel("SPEED MATH GO");
title.setFont(new Font("Comic Sans", Font.BOLD, 96));
title.setBounds(90, 0, 900, 100);
this.add(title);
ImageIcon backgroundImage = new ImageIcon("C:\\Users\\chenr\\Documents\\Speed Math Go\\iconfinder_play-circle_2561292\\ttt.png");
background = new JLabel(backgroundImage);
background.setBounds(0, 0, 1000, 700);
this.add(background);
ImageIcon playImage = new ImageIcon("C:\\Users\\chenr\\Documents\\Speed Math Go\\iconfinder_play-circle_2561292\\iconfinder_play-circle_2561292.png");
play = new JButton("Play", playImage);
playPanel = new JPanel();
playPanel.setLayout(null);
playPanel.setBounds(345, 200, 300, 150);
play.setBounds(345, 200, 300, 150);
play.setFont(new Font("Comic Sans", Font.BOLD, 48));
play.setBackground(Color.RED);
play.setVisible(true);
playPanel.add(play);
this.add(playPanel);
ImageIcon guideImage = new ImageIcon("C:\\Users\\chenr\\Documents\\Speed Math Go\\iconfinder_play-circle_2561292\\iconfinder_thefreeforty_map_1243687.png");
guide = new JButton("Guide", guideImage);
guidePanel = new JPanel();
guidePanel.setLayout(null);
guidePanel.setBounds(320, 400, 350, 150);
guide.setBounds(320, 400, 350, 150);
guide.setFont(new Font("Comic Sans", Font.BOLD, 48));
guide.setBackground(Color.CYAN);
guide.setVisible(true);
guidePanel.add(guide);
this.add(guidePanel);
//play.addActionListener(new ActionListener()
// {
// public void actionPerformed(ActionEvent e)
// {
// new SpeedMathGoGUI();
// }
// });
play.addActionListener(
new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new SpeedMathGoGUI();
dispose();
}
});
guide.addActionListener(
new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new SpeedMathGoGuide();
dispose();
}
}
);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(1000, 700);
setVisible(true);
}
}
If you run this on Eclipse, only the guide button shows. When I comment out line 56 (should be this.add(guidePanel)), only the play button shows. What adjustments I must make to my code in order for both to display on the JFrame? I would also like to know if other classes may have affected this error as this transitions to two others when the buttons are pressed.
Upvotes: 0
Views: 989
Reputation: 324098
Don't use a null layout!!! Don't use setBounds()!!!
Swing was designed to be used with layout managers, so you need to understand how layout managers work.
only the guide button shows.
this.add(playPanel);
...
this.add(guidePanel);
By default the content pane of a JFrame uses a BorderLayout
.
In your code above, when you add a component to a BorderLayout
and don't specify a constraint, then the CENTER
constraint is used. However, only one component can be displayed in the "CENTER" so you see the last one added.
How to Display Multiple Buttons Simultaneously
If you want two buttons (or any component) on a panel then add two buttons to a panel. The basic code would be:
JPanel buttonsPanel = new JPanel();
buttonsPanel.add( play );
buttonsPanel.add( guide );
this.add(buttonsPanel);
The default layout manager for a JPanel is the FlowLayout
. It is a simple layout. The components are displayed on the same line and no constraint is needed when the component is added.
Read the section from the Swing tutorial on Layout Managers for more information and working examples of other layout managers. You choose the appropriate layout manager for your requirement. You can always nest panels using different layout managers to achieve your desired layout.
Upvotes: 1