【swing应用】注册界面

 支持自定义头像,用记事本写完蹦出来40个错误然后一点点解决真真是找虐,该用Eclipse了。
java写界面真真是能让人疯掉、、




<span style="font-size:18px;">import java.io.File ;
import java.awt.event.ActionListener ;
import java.awt.event.ActionEvent ;
import javax.swing.JFrame ;
import javax.swing.* ;
import javax.swing.filechooser.* ;
import javax.swing.JLabel ;
import javax.swing.JButton ;
import javax.swing.border.TitledBorder ;
import javax.swing.JTextField ;
import javax.swing.JTextArea ;
import javax.swing.JPasswordField;
import javax.swing.BorderFactory ;
import javax.swing.ImageIcon ;
import javax.swing.JComboBox ;
import javax.swing.JCheckBox ;
import javax.swing.ButtonGroup ;
import javax.swing.JRadioButton ;
import javax.swing.JOptionPane ;
import javax.swing.JScrollPane ;
import javax.swing.JFileChooser ;
class Login extends JFrame
{
    public Login()     //构造方法
    {
      this.init() ;
    }
   
    public void init()   //初始化
    {
       this.setTitle("登陆界面") ;
       this.setBounds(100,100,340,500) ;
       this.createUI() ;
       this.setVisible(true) ;
       this.setDefaultCloseOperation(EXIT_ON_CLOSE) ;
      
    }
   
    public void createUI()  //具体界面实现
    {
       JPanel panel = new JPanel() ;
       TitledBorder tborder = BorderFactory.createTitledBorder("注册面板") ;
       //Border border = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED) ;
       //TitledBorder tborder = BorderFactory.createTitledBorder(border,"注册面板",TitledBorder.CENTER,TitledBorder.TOP) ;
       panel.setBorder(tborder) ;
       panel.setLayout(null) ;
       this.add(panel) ;
      
       JLabel namelab = new JLabel("输入姓名") ;
       namelab.setBounds(10,50,60,25) ;
       panel.add(namelab) ;
      
       JTextField nametext = new JTextField() ;
       nametext.setBounds(80,50,120,22) ;
       panel.add(nametext) ;
      
       JLabel passwordlab = new JLabel("输入密码") ;
       passwordlab.setBounds(10,80,60,25) ;
       panel.add(passwordlab) ;
      
       JPasswordField passwordtext = new JPasswordField() ;
       passwordtext.setBounds(80,80,120,22) ;
       panel.add(passwordtext) ;
      
       JLabel sexlab = new JLabel("性别") ;
       sexlab.setBounds(10,110,60,25) ;
       panel.add(sexlab) ;
      
       JRadioButton man = new JRadioButton("男",true) ;
       man.setBounds(80,110,50,25) ;
       panel.add(man) ;
       JRadioButton woman = new JRadioButton("女",false) ;
       woman.setBounds(140,110,60,25) ;
       panel.add(woman) ;
       ButtonGroup group = new ButtonGroup() ;
       group.add(man) ;
       group.add(woman) ;
      
       JLabel xueli = new JLabel("学历") ;
       xueli.setBounds(10,140,60,25) ;
       panel.add(xueli) ;
      
       JComboBox xue = new JComboBox() ;
       xue.addItem("小学") ;
       xue.addItem("初中") ;
       xue.addItem("高中") ;
       xue.addItem("大学") ;
       xue.setBounds(80,140,80,22) ;
       panel.add(xue) ;
      
       JLabel hobby = new JLabel("爱好") ;
       hobby.setBounds(10,170,60,25) ;
       panel.add(hobby) ;
       JCheckBox choice1 = new JCheckBox("写代码",true) ;
       choice1.setBounds(80,170,70,25) ;
       panel.add(choice1) ;
       JCheckBox choice2 = new JCheckBox("写代码",true) ;
       choice2.setBounds(150,170,70,25) ;
       panel.add(choice2) ;
       JCheckBox choice3 = new JCheckBox("写代码",true) ;
       choice3.setBounds(220,170,70,25) ;
       panel.add(choice3) ;
      
       JLabel introduce = new JLabel("自我介绍") ;
       introduce.setBounds(10,200,60,25) ;
       panel.add(introduce) ;
       JTextArea ta = new JTextArea() ;
       JScrollPane scr = new JScrollPane(ta,
       JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
       JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED) ;
       scr.setBounds(20,230,240,110) ;
       panel.add(scr) ;
      
       JLabel imglab = new JLabel() ;
       imglab.setBounds(5,350,324,47) ;
       panel.add(imglab) ;
      
       ImageIcon icon = new ImageIcon("d:"+File.separator+"12.gif") ;
       imglab.setIcon(icon) ;
      
       JButton submit = new JButton("提交") ;
       submit.setBounds(50,420,70,22) ;
       submit.addActionListener(new ActionListener()
       {
          public void actionPerformed(ActionEvent e)
          {
             int option = JOptionPane.showConfirmDialog(null,"确定提交吗?","提交提示",JOptionPane.OK_CANCEL_OPTION,JOptionPane.WARNING_MESSAGE) ;
             if(option == JOptionPane.OK_OPTION)
             {
                System.out.println("提交成功") ;
             }
             else System.out.println("退出操作") ;
          }
       }) ;
      
       panel.add(submit) ;
      
       JButton reset = new JButton("换头像") ;
       reset.setBounds(150,420,85,22) ;
       panel.add(reset) ;  
       reset.addActionListener(new ActionListener()
       {
          public void actionPerformed(ActionEvent e)
          {
              JFileChooser chooser = new JFileChooser() ;
              FileNameExtensionFilter filter = new FileNameExtensionFilter("jpg & gif images","jpg","gif") ;
              chooser.setFileFilter(filter) ;
              int res = chooser.showOpenDialog(null) ;
              if(res == JFileChooser.APPROVE_OPTION)
              {
                 System.out.println("选择的文件为"+chooser.getSelectedFile().getName()) ;
              }
          }
       }) ;
             
    }
}

class Tester
{
   public static void main(String args[])
   {
      new Login() ;
   }
}


</span>


原文地址:https://www.cnblogs.com/emoji/p/4436802.html