Java Swing中setLayout(null)不能显示按钮
importjava.awt.*;importjavax.swing.*;publicclassLoginFaceextendsJFrame{publicLoginFac...
import java.awt.*;
import javax.swing.*;
public class LoginFace extends JFrame {
public LoginFace() {
this.setLayout(null);
this.setTitle("运动会管理系统");
this.setResizable(false);
this.setVisible(true);
this.setSize(700, 500);
IDLabel.setLocation(40, 40);
this.add(IDLabel);
}
public static void main(String args[]) {
LoginFace login = new LoginFace();
}
private JLabel IDLabel=new JLabel("账号:");
private JLabel PsdLabel=new JLabel("密码:");
private JLabel RemindLabel=new JLabel();
private JTextField IDField=new JTextField();
private JPasswordField PsdField=new JPasswordField();
private JButton LoginButton=new JButton("登录");
} 展开
import javax.swing.*;
public class LoginFace extends JFrame {
public LoginFace() {
this.setLayout(null);
this.setTitle("运动会管理系统");
this.setResizable(false);
this.setVisible(true);
this.setSize(700, 500);
IDLabel.setLocation(40, 40);
this.add(IDLabel);
}
public static void main(String args[]) {
LoginFace login = new LoginFace();
}
private JLabel IDLabel=new JLabel("账号:");
private JLabel PsdLabel=new JLabel("密码:");
private JLabel RemindLabel=new JLabel();
private JTextField IDField=new JTextField();
private JPasswordField PsdField=new JPasswordField();
private JButton LoginButton=new JButton("登录");
} 展开
3个回答
展开全部
你对setLayout方法不太了解,我从java_api给你copy出了setLayout的用法如下:
setLayout(LayoutManager mgr)
Sets the layout manager for this container.
以为着你才必须传布局对象进去,布局对象可以不写,但不可传空值啊.
其他的控件你自己加咯.
import java.awt.*;
import javax.swing.*;
public class LoginFace extends JFrame {
public LoginFace() {
//this.setLayout(null);
this.setTitle("运动会管理系统");
this.setResizable(false);
this.setVisible(true);
this.setSize(700, 500);
IDLabel.setLocation(40, 40);
this.add(IDLabel);
}
public static void main(String args[]) {
LoginFace login = new LoginFace();
}
private JLabel IDLabel=new JLabel("账号:");
private JLabel PsdLabel=new JLabel("密码:");
private JLabel RemindLabel=new JLabel();
private JTextField IDField=new JTextField();
private JPasswordField PsdField=new JPasswordField();
private JButton LoginButton=new JButton("登录");
}
或者可以:
import java.awt.*;
import javax.swing.*;
public class LoginFace extends JFrame {
public LoginFace() {
this.setLayout(new FlowLayout());
this.setTitle("运动会管理系统");
this.setResizable(false);
this.setVisible(true);
this.setSize(700, 500);
IDLabel.setLocation(40, 40);
this.add(IDLabel);
}
public static void main(String args[]) {
LoginFace login = new LoginFace();
}
private JLabel IDLabel=new JLabel("账号:");
private JLabel PsdLabel=new JLabel("密码:");
private JLabel RemindLabel=new JLabel();
private JTextField IDField=new JTextField();
private JPasswordField PsdField=new JPasswordField();
private JButton LoginButton=new JButton("登录");
}
setLayout(LayoutManager mgr)
Sets the layout manager for this container.
以为着你才必须传布局对象进去,布局对象可以不写,但不可传空值啊.
其他的控件你自己加咯.
import java.awt.*;
import javax.swing.*;
public class LoginFace extends JFrame {
public LoginFace() {
//this.setLayout(null);
this.setTitle("运动会管理系统");
this.setResizable(false);
this.setVisible(true);
this.setSize(700, 500);
IDLabel.setLocation(40, 40);
this.add(IDLabel);
}
public static void main(String args[]) {
LoginFace login = new LoginFace();
}
private JLabel IDLabel=new JLabel("账号:");
private JLabel PsdLabel=new JLabel("密码:");
private JLabel RemindLabel=new JLabel();
private JTextField IDField=new JTextField();
private JPasswordField PsdField=new JPasswordField();
private JButton LoginButton=new JButton("登录");
}
或者可以:
import java.awt.*;
import javax.swing.*;
public class LoginFace extends JFrame {
public LoginFace() {
this.setLayout(new FlowLayout());
this.setTitle("运动会管理系统");
this.setResizable(false);
this.setVisible(true);
this.setSize(700, 500);
IDLabel.setLocation(40, 40);
this.add(IDLabel);
}
public static void main(String args[]) {
LoginFace login = new LoginFace();
}
private JLabel IDLabel=new JLabel("账号:");
private JLabel PsdLabel=new JLabel("密码:");
private JLabel RemindLabel=new JLabel();
private JTextField IDField=new JTextField();
private JPasswordField PsdField=new JPasswordField();
private JButton LoginButton=new JButton("登录");
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
在主框架中你没有添加按钮这个组件,你只添加了IDLabel这个组件了,所以看不到按钮。。。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
this.setLayout(new FlowLayout());
别设置null,不设置还有默认展示的
别设置null,不设置还有默认展示的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询