简体   繁体   English

简单的Java GUI显示不起作用

[英]simple java gui display not working

import javax.swing.*;
import java.awt.*;
public class gui {
    JFrame f;
    JLabel fname,fsex,age;
    JTextField t1;
    JTextField t2;
    JTextField t3;
     gui(){
        frame();
    }

    private void frame() {
        // TODO Auto-generated method stub
        f = new JFrame();
        f.setVisible(true);
        f.setSize(500,500);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        fname = new JLabel("First Name");
        fname.setSize(100,100);
        fsex = new JLabel("Sex");
        fsex.setSize(100,100);
        age = new JLabel("Age");
        age.setSize(100,100);
        t1 = new JTextField();
        t1.setSize(100,100);
        t2 = new JTextField();
        t2.setSize(100,100);
        t3 = new JTextField();
        t3.setSize(100,100);
        JPanel panel = new JPanel();
        panel.setSize(100,100);
        panel.add(fname);
        panel.add(t1);
        panel.add(fsex);
        panel.add(t2);
        panel.add(fname);
        panel.add(age);
        panel.add(t3);
        f.add(panel);


    }
}

I was wondering that if i could the reason that why am i getting a display of such kind!I am getting unsized small line shaped widgets in the top centre of the screen and not in theorder in which they have been coded. 我想知道我能否得到这样的显示!我在屏幕的顶部中心而不是按照编码的顺序获得了大小不一的小线状小部件。 Am i not supposed to get properly shaped widgets once i have set sizes.And also the widgets arent invisible,they are visible but unordered. 一旦设置了大小,我是否不应该得到形状合适的小部件。这些小部件也看不见,它们可见但无序。

Am i not supposed to get properly shaped widgets once i have set sizes. 设置大小后,我是否不应该得到形状合适的小部件。

No, you are not supposed to set the sizes, that is the job of the layout manager. 不,您不应该设置大小,这是布局管理器的工作。 A panel uses a FlowLayout which just displays all the components horizontally at there preferred size. 面板使用FlowLayout,它仅以首选大小水平显示所有组件。

A JLabel will have a preferred size based on the text you assign to the label. 根据您分配给标签的文本,JLabel将具有首选大小。

For a text field you need to indicate the approximate size by specify the number of characters to display. 对于文本字段,您需要通过指定要显示的字符数来指示近似大小。 You do this by using: 您可以使用以下方法完成此操作:

JTextField textField = new JTextField(10);

Start by reading the Swing tutorial on How to Use Flow Layout for an example and a better structured program. 首先阅读有关如何使用流布局的Swing教程的示例和结构更好的程序。 That is your code should be executed on the Event Dispatch Thread. 那就是您的代码应在事件调度线程上执行。

You have to call setVisible(true) in last. 您必须最后调用setVisible(true)

private void frame() {
        // TODO Auto-generated method stub
        f = new JFrame();
        f.setSize(500,500);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        fname = new JLabel("First Name");
        fname.setSize(100,100);
        fsex = new JLabel("Sex");
        fsex.setSize(100,100);
        age = new JLabel("Age");
        age.setSize(100,100);
        t1 = new JTextField();
        t1.setSize(100,100);
        t2 = new JTextField();
        t2.setSize(100,100);
        t3 = new JTextField();
        t3.setSize(100,100);
        JPanel panel = new JPanel();
        panel.setSize(100,100);
        panel.add(fname);
        panel.add(t1);
        panel.add(fsex);
        panel.add(t2);
        panel.add(fname);
        panel.add(age);
        panel.add(t3);
        f.add(panel);
        f.setVisible(true); //here set visible true after adding components
    }

setVisible(true) must be the last method call in the frame() method. setVisible(true)必须是frame()方法中的最后一个方法调用。 Please see the following thread: Why shouldn't I call setVisible(true) before adding components? 请参阅以下线程: 为什么在添加组件之前不应该调用setVisible(true)?

您必须通过在代码的frame()方法末尾调用setVisible( true )来设置可见性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM