【发布时间】:2019-11-04 12:23:09
【问题描述】:
我正在为某人的毕业创建一个登录应用程序,我需要几个文本字段和一个背景,我已经添加了背景,现在需要添加文本字段,问题是它们似乎不会在上面彼此的。
我已经分别尝试了它们,并且没有彼此它们都可以完美地工作,但我无法将它们堆叠起来,我在这个网站上看到了几个解决类似问题的答案,但对于这个应用程序,我需要放置几个背景上的文本字段仅与一个文本字段相对,这是我迄今为止所拥有的...
//creates the frame with a title as a parameter
JFrame frame = new JFrame("Sign In Sheet");
//sets the size
frame.setSize(1000, 556);
//makes it so the application stops running when you close it
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//puts it in the center of the screen
frame.setLocationRelativeTo(null);
//makes it so you can't resize it
frame.setResizable(false);
//setting the background by looking for the image
try{
frame.setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("C:/Users/Gabriel R. Warner/Desktop/clouds.png")))));
}catch(IOException e){
//and prints an error message if it's not found
System.out.println("well it didn't work");
}
//adding text fields with names apropriate to function
JTextField name1 = new JTextField();
name1.setPreferredSize(new Dimension(200, 15));
name1.setBackground(Color.WHITE);
frame.add(name1);
//makes frame visible
frame.setVisible(true);
简单地说,文本字段不会与背景一起显示,所有结果仅提供单个文本字段的答案
【问题讨论】:
-
1) 请注意您的语言,2) 您只添加了一个
JTextField,并没有您提到的那么多,发布一个正确的minimal reproducible example 来证明您的问题。 3)不要显式地setPreferredSize或setSize你的组件,因为JTextField作为参数传入它应该使用多少列(15在你的情况下)并调用pack()而不是你的JFrame. -
4) 当您的代码被包装为 JAR 时,图像将成为嵌入式资源,因此明智的做法是将它们视为已经存在,请参阅embedded-resource 标签信息了解如何使用它而不是完整路径。这是example,介绍如何通过在另一个
JLabel中添加JLabel或使用自定义绘画来添加图像作为背景
标签: java swing jframe jpanel jtextfield