今天我们开始了IM项目的开发,以此作为我们近阶段学习的一次综合性的实践。
项目需求如下:
由于之前SVN项目我是负责项目后台逻辑处理,于是这次我想尝试前台UI设计。在经过今晚近三个多小时的奋斗,我也查漏补缺了不少知识点,在此总结一下:
1.
//去掉窗口装饰 this.setUndecorated(true);
2.
JComboBox jcb = new JComboBox(); jcb.setEditable(true); jcb.setBounds(new Rectangle(260, 75, 240, 35));
这是JComboBox
写
getSelectedItem()
setSelectedItem(String str)
3.
JLabel jl3 = new JLabel("注册"); jl3.setBounds(new Rectangle(100, 188, 190, 35)); jl3.setFont(new Font("Dialog", 1, 15)); jl3.setForeground(Color.blue);
4.
设置窗口最小化
setState(Frame.ICONIFIED);设置程序关闭
System.exit(0);用于自定义最小化和关闭按钮,DIY是件很酷的事情哦!
5.
// 将图片加载到JLabel上 JLabel label_background = new JLabel(new ImageIcon("image/2.jpg")); label_background.setBounds(0, 0, this.getWidth(), this.getHeight()); // 相当于将JLabel贴在第二层面板的底层 this.getLayeredPane().add(label_background, new Integer(Integer.MIN_VALUE)); // JComponent是JPanel的父类,用于拿到窗口的第一层面板 JComponent jc = (JComponent) this.getContentPane(); // 设置第一层面板透明化 jc.setOpaque(false);
package view.start; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Font; import java.awt.Rectangle; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JTextField; import view.listener.MyActionListener; import view.listener.MyMouseAdapter; public class StartFrame extends JFrame { public static StartFrame sf; public static void main(String[] args) { sf = new StartFrame(); } public StartFrame() { initStartFrame(); } private void initStartFrame() { this.setSize(550, 280); this.setLocationRelativeTo(null); this.setResizable(false); this.setLayout(null); this.setDefaultCloseOperation(3); // 去掉窗口装饰 this.setUndecorated(true); JLabel jl1 = new JLabel("用户名"); jl1.setBounds(new Rectangle(200, 65, 50, 50)); JComboBox jcb = new JComboBox(); jcb.setEditable(true); jcb.setBounds(new Rectangle(260, 75, 240, 35)); JLabel jl2 = new JLabel("密 码"); jl2.setBounds(new Rectangle(200, 120, 50, 50)); JPasswordField jpf = new JPasswordField(18); jpf.setBounds(new Rectangle(260, 130, 240, 35)); JButton jb1 = new JButton("登录"); jb1.setBounds(new Rectangle(260, 187, 240, 35)); JLabel jl3 = new JLabel("注册"); jl3.setBounds(new Rectangle(100, 188, 190, 35)); jl3.setFont(new Font("Dialog", 1, 15)); jl3.setForeground(Color.blue); MyMouseAdapter mma = new MyMouseAdapter(jcb, jpf); jl3.addMouseListener(mma); // 添加图片 JPanel jp = new JPanel(); jp.setBounds(new Rectangle(70, 75, 88, 88)); jp.setBackground(Color.white); // 缩小按钮 JButton button_narrow = new JButton("narrow"); button_narrow.setBounds(new Rectangle(450, 0, 50, 35)); // 关闭按钮 JButton button_close = new JButton("close"); button_close.setBounds(new Rectangle(500, 0, 50, 35)); MyActionListener mal = new MyActionListener(jcb, jpf); jb1.addActionListener(mal); button_close.addActionListener(mal); button_narrow.addActionListener(mal); // 将图片加载到JLabel上 JLabel label_background = new JLabel(new ImageIcon("image/2.jpg")); label_background.setBounds(0, 0, this.getWidth(), this.getHeight()); // 相当于将JLabel贴在第二层面板的底层 this.getLayeredPane().add(label_background, new Integer(Integer.MIN_VALUE)); // JComponent是JPanel的父类,用于拿到窗口的第一层面板 JComponent jc = (JComponent) this.getContentPane(); // 设置第一层面板透明化 jc.setOpaque(false); this.add(button_narrow); this.add(button_close); this.add(jp); this.add(jl1); this.add(jcb); this.add(jl2); this.add(jpf); this.add(jb1); this.add(jl3); this.setVisible(true); } }
package view.listener; import java.awt.Frame; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JPasswordField; import javax.swing.JTextField; import view.start.StartFrame; public class MyActionListener implements ActionListener { private JComboBox jcb; private JPasswordField jpf; public MyActionListener(JComboBox jcb, JPasswordField jpf) { this.jcb = jcb; this.jpf = jpf; } public void actionPerformed(ActionEvent e) { JButton jb = (JButton) e.getSource(); String type = jb.getText(); // 获得用户名和密码 String name = (String) jcb.getSelectedItem(); String psd = jpf.getText(); switch (type) { case "登录": // TODO 登录操作 System.out.println("login" + name + psd); // StartFrame.sf.setVisible(false); break; case"narrow": StartFrame.sf.setState(Frame.ICONIFIED); break; case"close": System.exit(0); break; } jcb.setSelectedItem(""); jpf.setText(""); } }
package view.listener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.JComboBox; import javax.swing.JPasswordField; import view.start.StartFrame; public class MyMouseAdapter extends MouseAdapter { private JComboBox jcb; private JPasswordField jpf; public MyMouseAdapter(JComboBox jcb, JPasswordField jpf) { this.jcb = jcb; this.jpf = jpf; } public void mouseClicked(MouseEvent e) { // 获得用户名和密码 String name = (String) jcb.getSelectedItem(); String psd = jpf.getText(); // TODO 注册操作 System.out.println("register" + name + psd); // StartFrame.sf.setVisible(false); } }