用Java编写GUI但我的标签似乎永远不会出现

时间:2014-05-02 13:23:24

标签: java swing user-interface jlabel bluej

我正在使用BlueJ创建一个GUI - Java,我已经创建了输入框但是我似乎无法在每个或左侧添加标签。任何人都可以帮助我,告诉我哪里出错了?我的代码如下:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.JLabel;


public class Log extends JFrame {


    public static void main(String[] args){
        Log frameTabel = new Log();
    }

    JButton Confirm = new JButton("Confirm");
    JPanel panel = new JPanel();
    JLabel label1 = new JLabel("Name", JLabel.RIGHT);
    JTextField FullName = new JTextField(15);
    JTextField Address1line = new JTextField(15);
    JTextField postcode = new JTextField(15);
    JTextField Destination = new JTextField(15);
    JTextField Date = new JTextField(15);
    JTextField MilesTravelling = new JTextField(15);
    JLabel lblMsg = new JLabel ("Name",JLabel.LEFT);

    Log(){
        super("Customer GUI");
        setSize(300,400);
        setLocation(400,250);
        panel.setLayout(null);

        FullName.setBounds(70,30,150,20);
        Address1line.setBounds(70,80,150,20);
        postcode.setBounds(70,130,150,20);
        Destination.setBounds(70,180,150,20);
        Date.setBounds(70,230,150,20);
        MilesTravelling.setBounds(70,280,150,20);
        Confirm.setBounds(105,320,80,20);

        panel.add(lblMsg);
        panel.add(Confirm);
        panel.add(FullName);
        panel.add(Address1line);
        panel.add(postcode);
        panel.add(Destination);
        panel.add(Date);
        panel.add(MilesTravelling);
        getContentPane().add(label1);

        getContentPane().add(panel);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);

    }
}

1 个答案:

答案 0 :(得分:1)

getContentPane().add(label1); //JFrames CENTER area
getContentPane().add(panel);
  • 然后最后添加的JComponent可见

getContentPane().add(panel);
  • 建议不要使用NullLayoutLog frameTabel = new Log();应该包含在invokeLater中(应该在EventDispatchThread创建和初始化Swing GUI),更多内容在Oracle教程Initial Thread