62,628
社区成员
发帖
与我相关
我的任务
分享
package com.src.self;
import java.awt.*;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
public class LoginFrame {
//添加组件的方法
public void addComponentsToPane(Container pane) {
JButton buttonOk,buttonCancel;
JLabel labelName,labelPassword;//账户名和密码标签足
JTextField textFieldName;
JPasswordField textFieldPwd;
//创建一个顶部面板
JPanel topPane=new JPanel();
topPane.setLayout(new GridBagLayout());
//创建一个底部面板
JPanel bottomPane=new JPanel();
FlowLayout flowLayout=new FlowLayout();
flowLayout.setHgap(20);
flowLayout.setVgap(10);
bottomPane.setLayout(flowLayout);
//标签账户
GridBagConstraints conLabelName=new GridBagConstraints();
conLabelName.fill=GridBagConstraints.NONE;
labelName=new JLabel("账户:");
conLabelName.weightx=0.2;
conLabelName.gridx=0;
conLabelName.gridy=0;
conLabelName.anchor=GridBagConstraints.LINE_END;
topPane.add(labelName,conLabelName);
//账户文本框
GridBagConstraints conTextFieldName=new GridBagConstraints();
conTextFieldName.fill=GridBagConstraints.HORIZONTAL;
textFieldName=new JTextField();
conTextFieldName.weightx=0.8;
conTextFieldName.weighty=0.5;
conTextFieldName.gridx=1;
conTextFieldName.gridy=0;
conTextFieldName.insets=new Insets(10, 0, 10, 20);
topPane.add(textFieldName,conTextFieldName);
//标签密码
GridBagConstraints conLabelPassword=new GridBagConstraints();
conLabelPassword.fill=GridBagConstraints.NONE;
labelPassword=new JLabel("密码:");
conLabelPassword.gridx=0;
conLabelPassword.gridy=1;
conLabelPassword.anchor=GridBagConstraints.LINE_END;
topPane.add(labelPassword,conLabelPassword);
//密码框
GridBagConstraints conTextFieldPwd=new GridBagConstraints();
conTextFieldName.fill=GridBagConstraints.HORIZONTAL;
textFieldPwd=new JPasswordField("");
conTextFieldPwd.weighty=0.5;
conTextFieldPwd.gridx=1;
conTextFieldPwd.gridy=1;
conTextFieldPwd.insets=new Insets(0, 0, 0, 20);
topPane.add(textFieldPwd,conTextFieldPwd);
//添加确定取消面板
buttonOk=new JButton("确定");
buttonCancel=new JButton("取消");
bottomPane.add(buttonOk);
bottomPane.add(buttonCancel);
pane.add(topPane,BorderLayout.CENTER);
pane.add(bottomPane,BorderLayout.PAGE_END);
}
private void createAndShowGUI() {
JFrame frame=new JFrame("LoginFrame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addComponentsToPane(frame.getContentPane());
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
UIManager.put("swing.boldMetal", Boolean.FALSE);//关闭粗体字
new LoginFrame().createAndShowGUI();
}
});
}
}
