JAVA GUI界面组件学习
这周主要练习了GUI界面组件,设计了一个登陆界面,整体运用BorderLayout界面,将界面分为上、中、下,又将中间部分分为三层,运用了组合框来记录登陆ID,添加了复选框完善程序,基本实现了登陆界面的操作,需要进一步学习IO流让自己的程序可以添加登陆记录。以下是我的程序源代码(注:考虑到是登陆界面所以界面大小固定)。
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class loginframe extends JFrame{
String number[]={"2014330111","2014330112","2014330113","2014330114","2014330115","2014330116","2014330117"};
private JComboBox combo;
private JPasswordField passField;
public loginframe(){
this.setTitle("登录界面");
this.setSize(250, 180);
this.setResizable(false);
this.setLocationRelativeTo(this);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setContentPane(createContentPanel());
}
private JPanel createContentPanel(){
JPanel panel=new JPanel(new BorderLayout());
panel.add(BorderLayout.NORTH,createNPanel());
panel.add(BorderLayout.CENTER,createCeterPanel());
panel.add(BorderLayout.SOUTH,createSPanel());
return panel;
}
private JPanel createNPanel(){
JPanel panel=new JPanel();
panel.add(new JLabel("登陆系统"),JLabel.CENTER);
return panel;
}
private JPanel createCeterPanel(){
JPanel panel=new JPanel(new BorderLayout());
panel.add(BorderLayout.NORTH,createIDPassPanel());
panel.add(BorderLayout.SOUTH,createOptionsPanel());
return panel;
}
private JPanel createOptionsPanel() {
JPanel panel=new JPanel();
JCheckBox box1=new JCheckBox("记住密码");
JCheckBox box2=new JCheckBox("自动登录");
panel.add(box1);
panel.add(box2);
return panel;
}
private JPanel createIDPassPanel(){
JPanel panel=new JPanel(new GridLayout(2,1));
panel.add(createIDPanel());
panel.add(createPassPanel());
return panel;
}
private JPanel createPassPanel(){
JPanel panel=new JPanel(new BorderLayout());
JLabel label=new JLabel("密码:");
passField=new JPasswordField(10);
panel.add(BorderLayout.WEST,label);
panel.add(BorderLayout.CENTER,passField);
return panel;
}
private JPanel createIDPanel(){
JPanel panel=new JPanel(new BorderLayout());
JLabel label=new JLabel("账号:");
combo=new JComboBox(number);
combo.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
});
panel.add(BorderLayout.WEST,label);
panel.add(BorderLayout.CENTER,combo);
return panel;
}
private JPanel createSPanel(){
JPanel panel=new JPanel();
JButton loginBtn=new JButton("登陆");
loginBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
if(JOptionPane.YES_OPTION==JOptionPane.showConfirmDialog(null,"确认要登陆吗?")){
System.exit(0);}
}});
JButton cancelBtn=new JButton("取消");
panel.add(loginBtn);
panel.add(cancelBtn);
cancelBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
if(JOptionPane.YES_OPTION==JOptionPane.showConfirmDialog(null,"确认要退出吗?")){
System.exit(0);}
}
});
return panel;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new loginframe().setVisible(true);
}
}