Reputation: 17
So I'm trying to create a simple game in java for practice and i keep encountering a problem. The first JLabel(text) that should appear on the program isn't showing and I don't know what to do. I hope it doesn't have to do with the other JLabels because that would screw my whole game. Does anyone have any idea why it isn't showing up?
Also if you have more questions feel free to comment below. I apologize for my bad english
my code
import java.awt.Color;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class MyGame extends JFrame {
JButton wwBtnSelect;
JButton wwBtnNo;
JButton wwBtnExit;
JButton wwBtnYes;
MyGame(){
// creates the window
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(500, 500);
this.setVisible(true);
this.setTitle("LoL Game");
this.setResizable(false);
this.setLayout(null);
this.setLocationRelativeTo(null);
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenu editMenu = new JMenu("Edit");
JMenu helpMenu = new JMenu("Help");
JMenuItem exitItem = new JMenuItem("Exit");
exitItem.addActionListener(e -> System.exit(0));
fileMenu.add(exitItem);
menuBar.add(fileMenu);
menuBar.add(editMenu);
menuBar.add(helpMenu);
this.setJMenuBar(menuBar);
this.setVisible(true);
// creates the text label
JLabel label = new JLabel();
label.setText("Choose your Champion");
label.setBounds(170, 50, 134, 100);
label.setVisible(true);
//creates buttons
wwBtnSelect = new JButton("Warwick");
wwBtnNo = new JButton();
wwBtnNo.setVisible(false);
wwBtnNo.setBorder(BorderFactory.createLineBorder(Color.WHITE));
wwBtnYes = new JButton();
wwBtnYes.setVisible(false);
wwBtnYes.setBorder(BorderFactory.createLineBorder(Color.WHITE));
wwBtnYes.setFocusable(false);
wwBtnYes.setForeground(Color.WHITE);
wwBtnYes.setBackground(Color.BLACK);
wwBtnYes.setOpaque(true);
wwBtnExit = new JButton();
wwBtnExit.setVisible(false);
wwBtnExit.setFocusable(false);
wwBtnExit.setForeground(Color.WHITE);
wwBtnExit.setBackground(Color.BLACK);
wwBtnExit.setOpaque(true);
wwBtnExit.setBorder(BorderFactory.createLineBorder(Color.WHITE));
wwBtnSelect.setBounds(170, 150, 134, 50);
wwBtnSelect.setFocusable(false);
wwBtnSelect.setForeground(Color.WHITE);
wwBtnSelect.setBackground(Color.BLACK);
wwBtnSelect.setOpaque(true);
wwBtnSelect.setBorder(BorderFactory.createLineBorder(Color.WHITE));
wwBtnSelect.addActionListener(
e -> {
label.setBounds(120, 75, 250, 50);
label.setText("<html><div style='text-align: center;'>Oh, a Lee Sin just challenged you to a 1v1! <br/>Will you take the fight?</div></html>");
wwBtnYes.setVisible(true);
wwBtnYes.setText("Yes");
wwBtnYes.setBounds(170, 150, 134, 50);
wwBtnNo.setVisible(true);
wwBtnSelect.setVisible(false);
}
);
// player refuses the challenge
wwBtnNo.setText("No");
wwBtnNo.setBounds(170, 200, 134, 50);
wwBtnNo.setFocusable(false);
wwBtnNo.setForeground(Color.WHITE);
wwBtnNo.setBackground(Color.BLACK);
wwBtnNo.setOpaque(true);
wwBtnNo.addActionListener(
e -> {
wwBtnYes.setVisible(false);
wwBtnNo.setVisible(false);
label.setText("<html><div style='text-align: center;'>What a coward.</div></html>");
label.setBounds(190, 75, 250, 50);
wwBtnExit.setVisible(true);
wwBtnExit.setText("Exit");
wwBtnExit.setBounds(170, 200, 134, 50);
wwBtnExit.addActionListener(r -> this.dispose());
}
);
//player accepts the challange
ImageIcon QAbility = new ImageIcon( getClass().getResource("images/Jaws_of_the_Beast.png"));
ImageIcon WAbility = new ImageIcon( getClass().getResource("images/Blood_Hunt.png"));
ImageIcon EAbility = new ImageIcon( getClass().getResource("images/Primal_Howl.png"));
ImageIcon RAbility = new ImageIcon( getClass().getResource("images/Infinite_Duress.png"));
JLabel Qability = new JLabel();
Qability.setText("Q");
Qability.setIcon(QAbility);
Qability.setVisible(false);
Qability.setBounds(90, 350, 80, 80);
Qability.setHorizontalTextPosition(JLabel.CENTER);
Qability.setVerticalTextPosition(JLabel.TOP);
JLabel Wability = new JLabel();
Wability.setText("W");
Wability.setIcon(WAbility);
Wability.setVisible(false);
Wability.setBounds(160, 350, 80, 80);
Wability.setHorizontalTextPosition(JLabel.CENTER);
Wability.setVerticalTextPosition(JLabel.TOP);
JLabel Eability = new JLabel();
Eability.setText("E");
Eability.setIcon(EAbility);
Eability.setVisible(false);
Eability.setBounds(230, 350, 80, 80);
Eability.setHorizontalTextPosition(JLabel.CENTER);
Eability.setVerticalTextPosition(JLabel.TOP);
JLabel Rability = new JLabel();
Rability.setText("R");
Rability.setIcon(RAbility);
Rability.setVisible(false);
Rability.setBounds(300, 350, 80, 80);
Rability.setHorizontalTextPosition(JLabel.CENTER);
Rability.setVerticalTextPosition(JLabel.TOP);
wwBtnYes.addActionListener(
r -> {
wwBtnNo.setVisible(false);
wwBtnYes.setVisible(false);
label.setVisible(false);
Qability.setVisible(true);
Wability.setVisible(true);
Eability.setVisible(true);
Rability.setVisible(true);
}
);
// adds the elements
this.add(label);
this.add(wwBtnSelect);
this.add(wwBtnNo);
this.add(wwBtnExit);
this.add(wwBtnYes);
this.add(Qability);
this.add(Wability);
this.add(Eability);
this.add(Rability);
}
}
Upvotes: 0
Views: 57
Reputation: 11
Not using a LayoutManager is both ugly and error prone. By that typically alle components added to the frame are placed on top of each other on the default location (0,0).
I would highly recommend to at least
setLayout(new FlowLayout());
to help the layout manager to place sub-components. For better control over placement I highly recommend using the GridBagLayout like this setLayout(new GridBagLayout()); Adding a component requires you to add some hint on how to place the component relative to each other. Use this example to get the idea. (Btw: I did use a dedicated JFrame in my example not extending the class JFrame for no reason. But that's just personal taste)
JFrame f = new JFrame();
f.setLayout(new GridBagLayout());
GridBagConstraints gbc= new GridBagConstraints();
gbc.gridx = 1;
gbc.gridy = 1;
f.add(new JLabel("test"), gbc);
gbc.gridy++;
f.add(new JLabel("below"), gbc);
f.pack();
f.setVisible(true);
Also I would first place all components, then pack the frame to layout it and display it at the very end. Once a frame is displayed adding components later on isn't instant. You must inform the frame of changes...
Upvotes: 1