Illegalargumentexception when adding components to Jframe

Asked

Viewed 64 times

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)

1 answer

3


This line here is cause of error:

add(board,buttons);

You didn’t put in a complete code, so I’m guessing this class Board be some other component you have created.

If you look at the class documentation JFrame, you will see that none of the methods add that it inherits gets 2 components at the same time, that line is wrong. If you want to add board and Buttons in the JFrame, need to be one at a time, you need to break this addition:

add(board);
add(buttons);

Only it would still make a mistake because the class Buttons is a Jframe and you cannot add a Jframe within another. You will have to transform these classes Buttons and Board in JPanel if you want to add them this way.


It is worth mentioning the recommendation below:

Avoid using absolute layout, unless it is of extreme necessity and you know the consequences of it, because absolute layout makes it difficult to maintain the screen and makes your application look different depending on the monitor and resolution that is running.

There are several layouts so you don’t have to worry about positioning or manually organizing components. Not to mention that using layouts makes your code easier to maintain than inserting a lot of setbounds, and if you need to change the position of any component, in the absolute layout, you will have to reposition all manually.

And it’s also worth noting that all graphic applications involving the swing API should be dispatched to the EDT.

I recommend for study: Creating a GUI With JFC/Swing

  • 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 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.

  • 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 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.

  • @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.

  • I put a new question related to this problem

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.