package text1; import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Font; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.FileReader; import java.io.Serializable; import java.net.URL; import java.util.ArrayList; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.text.html.StyleSheet; public class Headmaster extends JFrame implements ActionListener, Serializable { private static final String CREATE = "创建单个对象"; private static final String name = "陈晓阳"; ArrayList<String> manyArray = new ArrayList<String>(); private static String instance; private JLabel june = new JLabel("June"); // 按钮 private JButton createOne = new JButton("创建单个对象"); private JButton createMany = new JButton("创建多个对象"); private JButton createInterface = new JButton("接口编程"); private JButton seeOne = new JButton("查看对象"); private JButton seeMany = new JButton("查看对象"); private JButton seeInterface = new JButton("查看对象"); private JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 20)); // 字体颜色自我定义 Color juneFont = new Color(85, 0, 119); Color createFont = new Color(102, 102, 187); Color backFont = new Color(238, 204, 187); Color backFont2 = new Color(204, 238, 85); Color backFont3 = new Color(221, 255, 255); public Headmaster() { add(panel); panel.setBackground(Color.LIGHT_GRAY); // panel.setLayout(new GridLayout(3,2,20,20)); panel.setOpaque(false); // 让他透明 panel.setLayout(null); // 不采用GridLayout()来定位,因为那样整个按钮会被填充,不好看 // 按钮添加及相应监听器的添加 panel.add(june); createOne.addActionListener(this); panel.add(createOne); seeOne.addActionListener(this); panel.add(seeOne); createMany.addActionListener(this); panel.add(createMany); seeMany.addActionListener(this); panel.add(seeMany); createInterface.addActionListener(this); panel.add(createInterface); seeInterface.addActionListener(this); panel.add(seeInterface); // 按钮的定位,要精确计算 june.setBounds(10, 15, 200, 70); createOne.setBounds(28, 148, 130, 40); seeOne.setBounds(172, 148, 110, 40); createMany.setBounds(28, 216, 130, 40); seeMany.setBounds(172, 216, 110, 40); createInterface.setBounds(28, 284, 130, 40); seeInterface.setBounds(172, 284, 110, 40); // 字体设置 Font fontLabel = new Font("Serif", Font.BOLD, 90); june.setFont(fontLabel); june.setForeground(juneFont); Font font = new Font("Serif", Font.BOLD, 15); createOne.setFont(font); createMany.setFont(font); seeOne.setFont(font); seeMany.setFont(font); createInterface.setFont(font); seeInterface.setFont(font); createOne.setForeground(createFont); seeOne.setForeground(createFont); createMany.setForeground(createFont); seeMany.setForeground(createFont); createInterface.setForeground(Color.GREEN); seeInterface.setForeground(Color.GREEN); // 按钮背景的设置 createOne.setBackground(backFont); createMany.setBackground(backFont2); seeOne.setBackground(backFont); seeMany.setBackground(backFont2); createInterface.setBackground(backFont3); seeInterface.setBackground(backFont3); } public static String getInstance(String name) { if (instance == null && !name.equals(null)) instance = Headmaster.name; return instance; } public String getManyInstance(int num, String name) { if (num != 0 && !name.equals(null)) instance = name; manyArray.add(instance); return instance; } public void actionPerformed(ActionEvent evt) { if (evt.getSource() == createOne) { try { createOne(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }// 创建唯一对象 } if (evt.getSource() == createMany) { try { createMany(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }// 创建限定对象 } if (evt.getSource() == seeOne) { try { seeOne(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }// 查看唯一对象 } if (evt.getSource() == seeMany) { try { seeMany(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }// 查看多个对象 } } public void seeOne() { JOptionPane.showMessageDialog(null, this.name, "单例模式--创建唯一个对象--创建结果", JOptionPane.INFORMATION_MESSAGE); } public void seeMany() { String all = ""; for (int i = 0; i < manyArray.size(); i++) { all = all + manyArray.get(i) + "\n"; } JOptionPane.showMessageDialog(null, all, "单例模式--创建" + manyArray.size() + "个对象--创建结果", JOptionPane.INFORMATION_MESSAGE); } public String createOne() { String inputName = JOptionPane.showInputDialog(null, "输入所创建的校长名字", "单例模式--创建唯一对象", JOptionPane.QUESTION_MESSAGE); if (inputName != null) { String name = (String) this.getInstance(inputName); JOptionPane.showMessageDialog(null, name.toString(), "单例模式--创建唯一对象--创建结果", JOptionPane.INFORMATION_MESSAGE); } if (inputName == null) JOptionPane.showMessageDialog(null, "输入为空,请重新输入!", "警告!", JOptionPane.ERROR_MESSAGE); return inputName; } public void createMany() { int inputNumbers = Integer.parseInt(JOptionPane.showInputDialog(null, "输入所要创建对象的限定个数", "单例模式--创建限定个数的对象", JOptionPane.QUESTION_MESSAGE)); if (JOptionPane.OK_OPTION == 0) if (inputNumbers != 0) { many(inputNumbers); } if (inputNumbers == 0) JOptionPane.showMessageDialog(null, "输入为空,请重新输入!", "警告!", JOptionPane.ERROR_MESSAGE); } public void many(int num) { for (int i = num; i > 0; i--) { String inputName = JOptionPane.showInputDialog(null, "输入所创建的对象名字", "单例模式--创建" + num + "对象", JOptionPane.QUESTION_MESSAGE); if (inputName != null) { String name = (String) this.getManyInstance(i, inputName); JOptionPane.showMessageDialog(null, name.toString(), "单例模式--创建" + num + "个对象--创建结果", JOptionPane.INFORMATION_MESSAGE); } if (inputName == null) JOptionPane.showMessageDialog(null, "输入为空,请重新输入!", "警告!", JOptionPane.ERROR_MESSAGE); } } public static void main(String[] args) { JFrame frame = new Headmaster(); frame.setSize(530, 530); frame.setTitle("实验一:单例模式+接口编程"); frame.setVisible(true); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //背景图片设置 ImageIcon bg = new ImageIcon("jay.jpg");// 把背景图片显示在一个标签里 JLabel label = new JLabel(bg); // 把标签的大小位置设置为图片刚好填充整个面 label.setBounds(0, 0, bg.getIconWidth(), bg.getIconHeight()); // 添加frame的第二层 frame.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE)); // 获取frame的最上层面板为了设置其背景颜色(JPanel有设置透明的方法) JPanel jp = (JPanel) frame.getContentPane(); jp.setOpaque(false);// 设置透明 } }
实验一:单例模式
(1) 单例模式(必做 80分)
建立一个界面,不管创建多少次现任校长,都只能创建一个“陈晓阳”对象,并验证获取的校长都是“陈晓阳”。
(2) 单例模式(选做 10分)
使用单例模式的思想实现多例模式,确保系统中某个类的对象只能存在有限个,如两个或三个,设计并编写代码实现一个多例类。
(3) 接口编程模式(选做 5分)
接口编程的木有做,先这样啦!