java任意窗口布局
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
package login;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Login extends JFrame{
/**
* @param args
* 登陆窗口
*/
private JLabel userId_label;
private JLabel userPass_label;
private JTextField userId_field;
private JTextField userPass_field;
private JButton ok_button;
private JButton cancle_button;
private JPanel mainPanel;
JFrame frame;
int frame_width;//窗口的宽度
int frame_height;//窗口的高度
public Login(){
Toolkit toolKit = Toolkit.getDefaultToolkit();
Dimension dimension = toolKit.getScreenSize();
int width =dimension.width; //屏幕的宽度
int height =dimension.height;//屏幕的宽度
frame_width = width/3;
frame_height = height/3;
buildPanel(frame_width,frame_height);//新建面板
this.setTitle("酒店管理系统");
this.setSize(frame_width,frame_height);
this.setLocation(frame_width, frame_height);
this.setContentPane(mainPanel);
this.setResizable(false);
this.setVisible(true);
}
public void buildPanel(int frame_width,int frame_height){
mainPanel = new JPanel();
mainPanel.setSize(frame_width, frame_height);
mainPanel.setLayout(null);
//新建帐号、密码标签
userId_label = new JLabel("帐号:");
userId_label.setSize(100, 20);
userId_label.setLocation(frame_width/4, frame_height/4);
userPass_label = new JLabel("密码:");
userPass_label.setSize(100, 20);
userPass_label.setLocation(frame_width/4, frame_height/4+40);
//新建帐号、密码文本域
userId_field = new JTextField();
userId_field.setSize(150, 20);
userId_field.setLocation(frame_width/4+50, frame_height/4);
userId_field.setBackground(Color.WHITE);
userPass_field = new JTextField();
userPass_field.setSize(150, 20);
userPass_field.setLocation(frame_width/4+50, frame_height/4+40);
//新建确定、取消按钮
ok_button = new JButton("确定");
ok_button.setSize(70, 20);
ok_button.setLocation(frame_width/4+10, frame_height/4+80);
cancle_button = new JButton("取消");
cancle_button.setSize(70, 20);
cancle_button.setLocation(frame_width/4+120, frame_height/4+80);
mainPanel.add(userId_label);
mainPanel.add(userPass_label);
mainPanel.add(userId_field);
mainPanel.add(userPass_field);
mainPanel.add(ok_button);
mainPanel.add(cancle_button);
}