Reputation: 269
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
}
}
Upvotes: 1
Views: 976
Reputation: 18929
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
}
Upvotes: 0
Reputation: 13194
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");
}
}
Upvotes: -1
Reputation: 54725
You should change:
jj.add(new JButton("North",BorderLayout.NORTH));
... to:
jj.add(new JButton("North"),BorderLayout.NORTH);
Upvotes: 0
Reputation: 81164
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.
Upvotes: 3