[英]Coding a GUI in Java however my labels never seem to appear
I am creating a GUI using BlueJ - Java, i have made the entry boxes however i cant seem to add a label to go either above each one or to the left. 我正在使用BlueJ-Java创建GUI,我已经创建了输入框,但是我似乎无法在每个上方或左侧添加标签。 Could anyone help me out and tell me where im going wrong ? 谁能帮助我,并告诉我我哪里出问题了? My code is below: 我的代码如下:
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);
}
}
. 。
getContentPane().add(label1); //JFrames CENTER area
getContentPane().add(panel);
. 。
getContentPane().add(panel);
NullLayout
and Log frameTabel = new Log();
建议不要使用NullLayout
和Log frameTabel = new Log();
should be wrapped into invokeLater
(Swing GUI should be created and intialized on EventDispatchThread
), more to see in Oracle tutorial Initial Thread
应该包装到invokeLater
(应在EventDispatchThread
上创建并初始化SWing GUI),有关更多信息,请参见Oracle教程Initial Thread
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.