I am learning Java, and, while trying to create a textbox, I expected the textbox to be 100 pixels wide and 30 pixels tall. Instead, it fills the entire window.
The code
import java.awt.*;
import java.awt.event.*;
class Main extends Frame implements ActionListener{
TextField tf;
Main(){
tf = new TextField();
tf.setBounds(0, 30, 100, 30);
Button b = new Button("Click me");
b.setBounds(0, 60, 100, 30);
b.addActionListener(this);
add(b);
add(tf);
setSize(300, 300);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
tf.setText("Thank you");
}
public static void main(String args[]){
new Main();
}
}
Tried to get textbox to be 100×30. Instead, it fits the entire window.
4
The default layout for a Frame
is BorderLayout
.
It’s javadoc tells it anchors and stretches components.
For an absolute positioning like in your case:
...
setSize(300, 300);
setLayout(null);
setVisible(true);
}
Frame’s LayoutManager
would be null
New contributor
lo-fi wi-fi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.