JTextField added to JPanel does not display properly.
(There is a problem analysis in the back, after reading will definitely help you solve the problem oh!!)
Code examples:
package CSDN;
import javax.swing.*;
public class JtextjoinJpanel {
public static void main(String[] args) {
JFrame f=new JFrame("Text Box Add Panel");
JPanel jp = new JPanel();
JTextField jt = new JTextField(). f.setBounds(400,400,500,500); // Set the window position size;
f.setBounds(400,400,500,500); // set window position size
jp.setSize(200,200); //set panel size
jt.setSize(100,100); //set textbox size
jp.add(jt); //add the text box to the panel
f.add(jp);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
Results:
solution:
- sets textbox parameters.
JTextField jt = new JTextField();
Change to:JTextField jt = new JTextField(10);
- set the panel layout to null.
Add jp.setLayout(null);
Results:
Note :10 means the number of characters that can be entered in a text box.
Reasons for the problem (personal opinion):
Due to the panel USES is flow layout, create a text box, if no input default arguments (string), then the text box default is empty, will cover its flow way, leaving only border (so see like a vertical bar, click can still input characters), if 10 to its input parameters, is equivalent to 10 characters have been input, so will not cover; Not setting it to flow layout also solves the problem.
Therefore, if
TextField jt = new JTextField();
to TextField jt = new JTextField("Hello");
The result is
Read More:
- Eclipse Error: There are no resources that can be added or removed from the server.
- Java learning unreported exception java.io.IOException ; must be caught or declared to be thrown
- [Solved] Java.util.linkedhashmap cannot be cast to entity class
- [Solved] o2o Error: addShopImg error:null / character to be escaped is missing
- Java parsing xml file encounters special symbols & will be abnormal solutions
- [Solved] Annotation Customize Error: ElementType cannot be resolved to a variable
- The java springboot websocket service server actively closes the connection and causes java.io.EOFException to be thrown
- How to Solve JAR pack error: Error resolving template [/userInfo], template might not exist or might not be accessib
- How to Close the Current Form in JAVA Swing
- [Solved] Tesseract ocr error: JFIF APP0 must be first marker after SOI
- Java error prompt….. cannot be resolved
- [Solved] Java.lang.ClassCastException: [Ljava.lang.Long; cannot be cast to java.util.List
- Error: char cannot be dereferenced [How to Solve]
- [Solved] Mybatis insert Error: Cause: java.sql.SQLException: SQL String cannot be empty
- [How to Solve Error]java.util.Date cannot be cast to java.sql.Date
- java.lang.IllegalArgumentException: adding a window to a container
- Tensorflow Error TypeError: Fetch argument XXXX has invalid typeXXXX,must be a string or Tensor
- The controller error: the XX method in the service cannot be found
- [Solved] No validator could be found for constraint ‘javax.validation.constraints.NotBlank’ validating type ‘java.lang.String’