java 为什么我的项目没有显示在 JFrame 中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16409526/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why are my items not showing up in JFrame?
提问by Col1107
I'm fairly new to JFrame and I want to know why my items are not showing up on the window. I know i dont have a ActionHandler but I just want my textfield's to show up on my window. Here's my code:
我对 JFrame 还很陌生,我想知道为什么我的项目没有显示在窗口上。我知道我没有 ActionHandler 但我只是希望我的文本字段显示在我的窗口上。这是我的代码:
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class FirstGUI extends JFrame{
public void GUI(){
setTitle("Welcome");
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setSize(600,600);
JLabel title = new JLabel();
title.setText("Apple Inc. Member Login Port");
title.setFont(new Font("Arial", Font.PLAIN, 24));
JTextField login = new JTextField("Login",10);
JPasswordField pass = new JPasswordField("Password");
add(title);
add(login);
add(pass);
}
public static void main(String[] args){
FirstGUI a = new FirstGUI();
a.GUI();
}
}
but when i run it i get this:
但是当我运行它时,我得到了这个:
回答by camickr
but when i run it i get this:
但是当我运行它时,我得到了这个:
You get an empty screen because you add the components to the frame after the frame is visible.
您会得到一个空白屏幕,因为您在框架可见后将组件添加到框架中。
- As has already been suggested you need to use an appropriate layout manager. FlowLayout is the easiest to start with.
- invoke
setVisible(true)
AFTER adding the components to the frame.
- 正如已经建议的那样,您需要使用适当的布局管理器。FlowLayout 是最容易开始的。
setVisible(true)
将组件添加到框架后调用。
So the code should be more like:
所以代码应该更像:
panel.add(...);
panel.add(...);
add(panel);
pack();
setVisible(true);
回答by Madhur Mehta
I agree to MadProgrammer's suggestions (+1)
我同意 MadProgrammer 的建议 (+1)
Well, lets take a look at your program though
好吧,让我们看看你的程序
You actually have created a JFrame with components in it. Its working fine as well, but your question of "why are my items not showing up in the JFrame" is not because you did something wrong but because missed out something i.e. revalidate()
您实际上已经创建了一个带有组件的 JFrame。它也可以正常工作,但是您关于“为什么我的项目没有出现在 JFrame 中”的问题不是因为您做错了什么,而是因为错过了一些东西,即 revalidate()
Try:
尝试:
public static void main(String[] args){
FirstGUI a = new FirstGUI();
a.GUI();
a.revalidate();
}
I'm not saying this will give you perfect UI.. what I'm trying to say is this will help you understand the Swing better. Learn about Swing Layout managers and then work on your UI to have better results
我并不是说这会给你完美的用户界面。我想说的是这将帮助你更好地理解 Swing。了解 Swing 布局管理器,然后处理您的 UI 以获得更好的结果
revalidate(): This component and all parents above it are marked as needing to be laid out. This means the Layout Manager will try to realign the components. Often used after removing components. It is possible that some really sharp swing people may miss this. I would think that you will only know this if you are actually using Swing.
revalidate():这个组件和它上面的所有父组件都被标记为需要布局。这意味着布局管理器将尝试重新对齐组件。通常在移除组件后使用。一些真正剧烈摇摆的人可能会错过这一点。我认为您只有在实际使用 Swing 时才会知道这一点。
回答by Madhur Mehta
The only one reason :
唯一的一个原因:
setVisible(True); method for the frame should be put on the end of the code.
if you give this line on the top of the code that is when you create a frame. This will cause that problem.
如果您在创建框架时的代码顶部给出这一行。这会导致那个问题。
回答by MadProgrammer
The default layout manager for JFrame
is BorderLayout
.
的默认布局管理器JFrame
是BorderLayout
。
This means that your components are essentially all been added ontop of each other.
这意味着您的组件基本上都是相互叠加的。
Try changing the layout manager to something like FlowLayout
(for example)...
尝试将布局管理器更改为FlowLayout
(例如)...
Take a look at A Visual Guide to Layout Managersand Using Layout Managersfor more details.
Also, avoid setSize
where possible, use Window#pack
instead
此外,setSize
尽可能避免,Window#pack
改用
Update
更新
I'd also like to introduce you to Initial Threadswhich should be used to launch your UI code...
我还想向您介绍初始线程,它应该用于启动您的 UI 代码...
回答by ApproachingDarknessFish
Don't add the components directly to your frame. Instead add to the content pane, which is where a JFrame stores all of the components that it draws. Usually this is a JPanel.
不要将组件直接添加到您的框架中。而是添加到内容窗格,这是 JFrame 存储它绘制的所有组件的地方。通常这是一个 JPanel。
Here is an example:
下面是一个例子:
public class GUI
{
private JPanel content;
public void GUI
{
/*Other code*/
content = new JPanel();
add(content); //make content the content pane
content.add(title);
content.add(login);
content.add(pass);
}
If that fails, call setVisible(true)
and setEnabled(true)
on all of your components.
如果失败,请在所有组件上调用setVisible(true)
和setEnabled(true)
。
On a side note you may want to make your GUI
function a constructor.
附带说明一下,您可能希望将GUI
函数设为构造函数。
回答by Mohan Bera
import javax.swing.*;
import java.awt.*;
class Myframec extends JFrame
{
Myframec()
{
Container c = this.getContentPane();
c.setLayout(null);
this.setBounds(10,10,700,500);
this.setTitle("Welcome");
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setBounds(0,0,700,500);
panel.setBackground(Color.gray);
panel.setLayout(null);
c.add(panel);
Font f = new Font("Arial",Font.BOLD,25);
Font f1 = new Font("Arial",Font.BOLD,20);
JLabel lable = new JLabel();
lable.setBounds(130,10,400,100);
lable.setText("Apple Inc. Member Login Port");
lable.setFont(f);
panel.add(lable);
JTextField login = new JTextField("Login",10);
login.setBounds(120,150,400,30);
login.setFont(f1);
panel.add(login);
JPasswordField pass =new JPasswordField("Password");
pass.setBounds(120,200,400,30);
pass.setFont(f1);
lable.setFont(f);
panel.add(pass);
c.setVisible(true);
this.setVisible(true);
}
public static void main(String[] argm)
{
Myframec frame = new Myframec();
frame.setVisible(true);
}
}