2
I’m doing a project of the game Sokoban, and I got to the part where I created 2 Jbuttons to select the level. Now when I try to run a program, it’s a mistake. I created both buttons and action in a class Buttons and named it that class in another class that starts the program. I used the following for the buttons:
public class Buttons extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
public Buttons(){
this.getContentPane().setLayout(new FlowLayout());
JButton lvl1btn = new JButton("Level 1");
JButton lvl2btn = new JButton("Level 2");
lvl1btn.setBounds(700,0,100,25);
lvl1btn.setBounds(800,0,100,25);
lvl1btn.addActionListener(this);
lvl2btn.addActionListener(this);
lvl1btn.setActionCommand("Level 1");
lvl2btn.setActionCommand("Level 2");
}
@Override
public void actionPerformed(ActionEvent e) {
String action = e.getActionCommand();
if(action.equals("Level 1")){
Board board = new Board();
board.createWorldOne();
}else if(action.equals("Level 2")){
Board board = new Board();
board.createWorldTwo();
}
}
And I called it that:
public final class SokobanStart extends JFrame {
public SokobanStart() {
InitUI();
}
public void InitUI() {
Board board = new Board();
Buttons buttons = new Buttons();
add(board,buttons);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(board.getBoardWidth(),board.getBoardHeight());
setLocationRelativeTo(null);
setTitle("Sokoban Game!");
}
public static void main(String[] args) {
SokobanStart sokobanStart = new SokobanStart();
sokobanStart.setVisible(true);
}
}
And from the following thread:
Exception in thread "main" java.lang.Illegalargumentexception: cannot add to layout: Constraint must be a string (or null) at java.desktop/java.awt.Borderlayout.addLayoutComponent(Borderlayout.java:431) java.desktop/javax.swing.Jrootpane$1.addLayoutComponent(Jrootpane.java:507) at java.desktop/java.awt.Container.addImpl(Container.java:1148) at java.desktop/java.awt.Container.add(Container.java:1025) at java.desktop/javax.swing.Jframe.addImpl(Jframe.java:553) at java.desktop/java.awt.Container.add(Container.java:993) At Sokoban.SokobanStart.Initui(Sokobanstart.java:16) At Sokoban.SokobanStart.(Sokobanstart.java:10) At Sokoban.SokobanStart.main(Sokobanstart.java:25)
Good. I thank help the error no longer exists. However, now the window appears blank. I have been doing some research and can not find the reason.
– Phil
@Phil If this answer solved the error, it is interesting to close this question by clicking on
v
on the side. If there is a new doubt, it is recommended to open a new question containing a complete code that is [mcve], so it’s easier to help you.– user28595
I was still editing the commentary, but the five minutes went by. Continuing ,I would say that I have several classes that define the various objects and these are called in the Board class except the Buttons class because I can’t get it to run this class by calling it on the board. So I call them both at Start and add them to the Jpanel. However if I delete the buttons the game already appears as planned.With the buttons appears the blank window.
– Phil
@Phil as I have already said, Oce is questioning a problem completely different from the question he asked, as I said, it is more interesting for the organization of the site Oce end this question as solved by clicking on
v
and asking a new question, containing a [mcve] or it becomes complicated to help without being able to execute.– user28595
@Phil without a [mcve] really has no way of telling you the reason for the problem. But I suspect, just reading what you commented, that the cause is the manager of layouts. If you provide a minimal and executable example of the code with the mentioned problem, I can suggest some changes that solve this.
– user28595
I put a new question related to this problem
– Phil