Question

This is my program.I want to have the button on north side of the frame but when i try to use the border layout gives an error defined at that line.

import java.awt.BorderLayout;
import java.awt.Component;
import javax.swing.*;

public class testt {

    static JFrame jj=new JFrame("Test frame");

    public static void main (String[] args){
      jj.setBounds(100, 200, 400, 300);
      jj.setVisible(true);
      jj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      jj.add(new JButton("North",BorderLayout.NORTH));  //The constructor JButton(String, String) is undefined

    }
}
Was it helpful?

Solution

jj.add(new JButton("North"), BorderLayout.NORTH);

You had the BorderLayout constraint as a parameter to the constructor, it should be a parameter to add() as above.

OTHER TIPS

You should change:

jj.add(new JButton("North",BorderLayout.NORTH));

... to:

jj.add(new JButton("North"),BorderLayout.NORTH);

you just worngly use revise your code like

public static void main(String[] args) {
        JFrame jj = new JFrame("Test frame");
        jj.setBounds(100, 200, 400, 300);
        jj.setVisible(true);
        jj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        jj.add(new JButton("North"),BorderLayout.NORTH);
        //jj.add("North", )); // The constructor
                                                            // JButton(String,
                                                            // String) is
                                                            // undefined

    }

Here is the corrected code. Try this and observe what was wrong. In case of any query, feel free to ask,

import java.awt.BorderLayout; import java.awt.Component; import javax.swing.*;

public class testt {

static JFrame jj = new JFrame("Test frame");

public static void main (String[] args) {

    jj.setBounds(100, 200, 400, 300);
    jj.setVisible(true);
    jj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jj.add(new JButton("My JButton"), "North");
}

}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top