【发布时间】:2012-10-24 22:13:24
【问题描述】:
JTextField 之所以存在,是因为当我将鼠标移到应该是鼠标图标的位置时,鼠标图标会变为光标,然后当我单击它时就会出现。但它在启动时是不可见的。我错过了什么?
public class JavaSwingTextfield extends JFrame {
private static final long serialVersionUID = 1L;
JTextField myTextField;
public JavaSwingTextfield(){
/***** JFrame setup *****/
// Set the size of the window
setSize(600,600);
// Make the application close when the X is clicked on the window
setDefaultCloseOperation(EXIT_ON_CLOSE);
// Makes the JFrame visible to the user
setVisible(true);
/***** JFrame setup END *****/
/***** JButton setup *****/
// Create a JLabel and set its label to "Start"
myTextField = new JTextField("Start");
// Set the label's size
myTextField.setSize(100, 50);
// Put the label in a certain spot
myTextField.setLocation(200, 50);
// Set a font type for the label
//Font myFont = new Font("Serif", Font.BOLD, 24);
//myTextField.setFont(myFont);
// Add the label to the JFrame
add(myTextField);
/***** JButton setup END *****/
}
/***** The main method *****/
public static void main(String[] args){
new JavaSwingTextfield();
}
}
【问题讨论】:
-
1)
myTextField.setLocation(200, 50);不要那样做。使用布局,请致电pack()如果这不能解决问题,请发布SSCCE。 2)myTextField.setSize(100, 50);也不要那样做。宽度可以通过Font和列来建议。高度取自Font。 -
@AVD 我在
add(myTextField);之前和之后调用了myTextField.setVisible(true);。什么都没有。
标签: java swing jpanel jtextfield layout-manager