Java只用一个timer来跑屏幕

package a;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.Hashtable;
import java.util.Random;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.Timer;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;

public class Main extends JFrame implements ActionListener {

	private static GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
	private static GraphicsDevice[] graphicsDevices = graphicsEnvironment.getScreenDevices();

	private static String[] s = { "创世记", "出埃及记", "利未记", "民数记", "申命记", "约书亚记", "士师记", "路得记", "撒母耳记上", "撒母耳记下", "列王纪上", "列王纪下", "历代志上", "历代志下", "以斯拉记", "尼希米记", "以斯帖记", "约伯记", "诗篇", "箴言", "传道书", "雅歌", "以赛亚书", "耶利米书", "耶利米哀歌", "以西结书", "但以理书", "何西阿书", "约珥书", "阿摩司书", "俄巴底亚书", "约拿书", "弥迦书", "那鸿书", "哈巴谷书", "西番雅书", "哈该书", "撒迦利亚书", "玛拉基书", "马太福音", "马可福音", "路加福音", "约翰福音", "使徒行传", "罗马书", "哥林多前书", "哥林多后书", "加拉太书", "以弗所书", "腓立比书", "歌罗西书", "帖撒罗尼迦前书", "帖撒罗尼迦后书", "提摩太前书", "提摩太后书", "提多书", "腓利门书", "希伯来书", "雅各书", "彼得前书", "彼得后书", "约翰壹书", "约翰贰书", "约翰叁书", "犹大书", "启示录" };
	private static String[] s0 = { "律法书", "王国前历史书", "王国时历史书", "被掳后历史书", "诗歌·智慧书", "大先知书", "王国时小先知书", "被掳后小先知书", "四福音", "教会历史", "保罗书信", "其他使徒书信", "对约翰的启示" };
	private static String[] s1 = { "创世记", "出埃及记", "利未记", "民数记", "申命记" };
	private static String[] s2 = { "约书亚记", "士师记", "路得记" };
	private static String[] s3 = { "撒母耳记上", "撒母耳记下", "列王纪上", "列王纪下", "历代志上", "历代志下" };
	private static String[] s4 = { "以斯拉记", "尼希米记", "以斯帖记" };
	private static String[] s5 = { "约伯记", "诗篇", "箴言", "传道书", "雅歌" };
	private static String[] s6 = { "以赛亚书", "耶利米书", "耶利米哀歌", "以西结书", "但以理书" };
	private static String[] s7 = { "何西阿书", "约珥书", "阿摩司书", "俄巴底亚书", "约拿书", "弥迦书", "那鸿书", "哈巴谷书", "西番雅书" };
	private static String[] s8 = { "哈该书", "撒迦利亚书", "玛拉基书" };
	private static String[] s9 = { "马太福音", "马可福音", "路加福音", "约翰福音" };
	private static String[] s10 = { "使徒行传" };
	private static String[] s11 = { "罗马书", "哥林多前书", "哥林多后书", "加拉太书", "以弗所书", "腓立比书", "歌罗西书", "帖撒罗尼迦前书", "帖撒罗尼迦后书", "提摩太前书", "提摩太后书", "提多书", "腓利门书" };
	private static String[] s12 = { "希伯来书", "雅各书", "彼得前书", "彼得后书", "约翰壹书", "约翰贰书", "约翰叁书", "犹大书" };
	private static String[] s13 = { "启示录" };
	private static String[][] sHolyBible = { s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12, s13 };

	private static Color[] c4 = { new Color(255, 127, 127), new Color(127, 255, 127), new Color(127, 255, 255), new Color(255, 255, 127) };

	private static JMenuBar jMenuBar = new JMenuBar();
	private static JPopupMenu jPopupMenu = new JPopupMenu();
	private static JMenu[] jMenusjMenuBar = new JMenu[s0.length];
	private static JMenu[] jMenusjPopupMenu = new JMenu[s0.length];
	private static Font font20 = new Font("微软雅黑", Font.BOLD, 20);
	private static Font font100 = new Font("微软雅黑", Font.BOLD, 100);
	private static JTextPane jTextPane = new JTextPane();
	private static StyledDocument styledDocument = jTextPane.getStyledDocument();
	private static Style style = jTextPane.addStyle(null, null);
	private static SimpleAttributeSet sas = new SimpleAttributeSet();
	private static JScrollPane jScrollPane = new JScrollPane(jTextPane);
	private static JScrollBar jScrollBar = jScrollPane.getVerticalScrollBar();

	private static JLabel jLabel = new JLabel("彩虹圣经投屏");

	private static Main main = new Main();

	private static int speed = 0;
	private static Timer timer = new Timer(100, new ActionListener() {
		@Override
		public void actionPerformed(ActionEvent e) {
			int getValue = Main.jScrollBar.getValue();
			System.out.println(e.getSource());
			Main.jScrollBar.setValue(getValue += speed);
		}
	});

	public Main() {
		this.setBounds(0, 0, 1024, 768);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setExtendedState(JFrame.MAXIMIZED_BOTH);
		// this.setUndecorated(true);

		Main.jTextPane.addKeyListener(new KeyListener() {
			@Override
			public void keyTyped(KeyEvent e) {
			}

			@Override
			public void keyReleased(KeyEvent e) {
			}

			@Override
			public void keyPressed(KeyEvent e) {
				int i = e.getKeyCode();
				int getValue = Main.jScrollBar.getValue();
				if (i == KeyEvent.VK_E) {
					Main.jScrollBar.setValue(getValue -= 4);
				} else if (i == KeyEvent.VK_D) {
					Main.jScrollBar.setValue(getValue += 4);
				} else if (i == KeyEvent.VK_1) {
					Main.speed = 1;
					timer.restart();
					Main.jLabel.setVisible(false);
				} else if (i == KeyEvent.VK_2) {
					Main.speed = 2;
					timer.restart();
					Main.jLabel.setVisible(false);
				} else if (i == KeyEvent.VK_3) {
					Main.speed = 3;
					timer.restart();
					Main.jLabel.setVisible(false);
				} else if (i == KeyEvent.VK_4) {
					Main.speed = 4;
					timer.restart();
					Main.jLabel.setVisible(false);
				} else if (i == KeyEvent.VK_5) {
					Main.speed = 5;
					timer.restart();
					Main.jLabel.setVisible(false);
				} else if (i == KeyEvent.VK_6) {
					Main.speed = 6;
					timer.restart();
					Main.jLabel.setVisible(false);
				} else if (i == KeyEvent.VK_7) {
					Main.speed = 7;
					timer.restart();
					Main.jLabel.setVisible(false);
				} else if (i == KeyEvent.VK_8) {
					Main.speed = 8;
					timer.restart();
					Main.jLabel.setVisible(false);
				} else if (i == KeyEvent.VK_9) {
					Main.speed = 9;
					timer.restart();
					Main.jLabel.setVisible(false);
				} else if (i == KeyEvent.VK_0) {
					Main.speed = 0;
					Main.timer.stop();
					Main.jLabel.setVisible(true);
				} else if (i == KeyEvent.VK_SPACE) {
					if (Main.jLabel.isVisible()) {
						Main.jLabel.setVisible(false);
					} else {
						Main.jLabel.setVisible(true);
					}
				} else if (i == KeyEvent.VK_UP || i == KeyEvent.VK_DOWN || i == KeyEvent.VK_LEFT || i == KeyEvent.VK_RIGHT || i == KeyEvent.VK_PAGE_UP || i == KeyEvent.VK_PAGE_DOWN) {
					e.consume();// 限定某些按键不可用
				} else if (i == KeyEvent.VK_ESCAPE) {
					int result = JOptionPane.showConfirmDialog(main, "退出", "退出", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
					if (result == JOptionPane.OK_OPTION) {
						setVisible(false);
						dispose();
						System.exit(0);
						return;
					}
				}
			}
		});
		Main.jTextPane.setBackground(Color.BLACK);
		Main.jTextPane.setFont(font100);
		Main.jTextPane.setEditable(false);
		Main.jTextPane.setText("选择章节:鼠标右键\n手动翻页:按住E或D不松手\n自动翻页:大键盘1~5(数值越大速度越快)\n停止自动翻页:大键盘0\n显示当前章节:空格\n退出:ESC");
		this.add(Main.jScrollPane, BorderLayout.CENTER);

		for (int i = 0; i < Main.s0.length; i++) {
			Main.jMenusjMenuBar[i] = new JMenu(Main.s0[i]);
			Main.jMenusjPopupMenu[i] = new JMenu(Main.s0[i]);
			Main.jMenusjMenuBar[i].setFont(font20);
			Main.jMenusjPopupMenu[i].setFont(font20);
			if (i > 7) {
				Main.jMenusjMenuBar[i].setForeground(Color.RED);
				Main.jMenusjPopupMenu[i].setForeground(Color.RED);
			}
			Main.jMenuBar.add(Main.jMenusjMenuBar[i]);
			Main.jPopupMenu.add(Main.jMenusjPopupMenu[i]);
			JMenuItem[] jMenuItemsjMenuBar = new JMenuItem[Main.sHolyBible[i].length];
			JMenuItem[] jMenuItemsjPopupMenu = new JMenuItem[Main.sHolyBible[i].length];
			for (int j = 0; j < Main.sHolyBible[i].length; j++) {
				jMenuItemsjMenuBar[j] = new JMenuItem(Main.sHolyBible[i][j]);
				jMenuItemsjPopupMenu[j] = new JMenuItem(Main.sHolyBible[i][j]);
				if (i > 7) {
					jMenuItemsjMenuBar[j].setForeground(Color.RED);
					jMenuItemsjPopupMenu[j].setForeground(Color.RED);
				}
				jMenuItemsjMenuBar[j].setFont(font20);
				jMenuItemsjPopupMenu[j].setFont(font20);

				jMenuItemsjMenuBar[j].addActionListener(this);
				jMenuItemsjPopupMenu[j].addActionListener(this);

				Main.jMenusjPopupMenu[i].add(jMenuItemsjMenuBar[j]);
				Main.jMenusjMenuBar[i].add(jMenuItemsjPopupMenu[j]);
			}
		}
		this.setJMenuBar(Main.jMenuBar);
		Main.jTextPane.addMouseListener(new MouseListener() {

			@Override
			public void mouseReleased(MouseEvent e) {
				if (e.isPopupTrigger()) {
					Main.jPopupMenu.show(e.getComponent(), e.getX(), e.getY());
				}
			}

			@Override
			public void mousePressed(MouseEvent e) {
			}

			@Override
			public void mouseExited(MouseEvent e) {
			}

			@Override
			public void mouseEntered(MouseEvent e) {
			}

			@Override
			public void mouseClicked(MouseEvent e) {
			}

		});

		Main.jLabel.setBackground(new Color(new Random().nextInt(100), new Random().nextInt(100), new Random().nextInt(100)));
		Main.jLabel.setFont(font100);
		Main.jLabel.setForeground(Color.WHITE);
		Main.jLabel.setOpaque(true);
		this.add(Main.jLabel, BorderLayout.SOUTH);

	}

	public static void main(String[] args) {
		main.setVisible(true);

	}

	@Override
	public void actionPerformed(ActionEvent actionEvent) {
		String s = actionEvent.getActionCommand();
		if (!s.equals("")) {
			Main.jspFill(input(s));

		}
		Main.speed = 0;
		Main.timer.stop();
	}

	public static String[] input(String FullName) {
		String ChapterSN = "";
		String VerseSN = "";

		while (ChapterSN.equals("")) {
			String jopid = JOptionPane.showInputDialog("请输入要查询的章数", "1");
			if (jopid == null || jopid.equals("") || jopid.length() > 3 || !jopid.trim().matches("^[0-9]*$")) {
				continue;
			}
			ChapterSN = jopid.trim();
		}

		while (VerseSN.equals("")) {
			String jopid = JOptionPane.showInputDialog("请输入要查询的节数", "1");
			if (jopid == null || jopid.equals("") || jopid.length() > 3 || !jopid.trim().matches("^[0-9]*$")) {
				continue;
			}
			VerseSN = jopid.trim();
		}
		String[] slist = { FullName, ChapterSN, VerseSN };
		return slist;
	}

	public static void jspFill(String[] slist) {
		Main.jTextPane.setText("");
		try {
			Main.jLabel.setBackground(new Color(new Random().nextInt(100), new Random().nextInt(100), new Random().nextInt(100)));
			Main.jLabel.setFont(Main.font100);
			Main.jLabel.setText(slist[0] + "    第" + slist[1] + "章");
			Main.jLabel.setVisible(true);

			StyleConstants.setLineSpacing(sas, 0.1f);
			Main.styledDocument.setParagraphAttributes(0, Main.styledDocument.getLength(), sas, false);

			Vector<Hashtable<String, Object>> vhso = Main.selectSqlite3(slist);

			StyleConstants.setForeground(Main.style, new Color(255, 255, 255));
			StyleConstants.setFontSize(Main.style, 70);

			for (int i = 0; i < vhso.size(); i++) {
				String jie = vhso.get(i).get("VerseSN").toString();
				String jingwen = vhso.get(i).get("strjw").toString();

				StyleConstants.setForeground(Main.style, Main.c4[i % Main.c4.length]);
				StyleConstants.setFontSize(Main.style, 35);
				Main.styledDocument.insertString(Main.styledDocument.getLength(), jie + "\t", Main.style);
				StyleConstants.setFontSize(Main.style, 70);
				Main.styledDocument.insertString(Main.styledDocument.getLength(), jingwen + "\n", Main.style);

			}
			Main.styledDocument.insertString(Main.styledDocument.getLength(), "\n\n\n\n\n\n\n\n\n\n", Main.style);
		} catch (Exception exception) {
			exception.printStackTrace();
		}
		Main.jTextPane.setCaretPosition(0);
		Main.jTextPane.requestFocusInWindow();

	}

	public static Vector<Hashtable<String, Object>> selectSqlite3(String slist[]) {

		Vector<Hashtable<String, Object>> vhso = new Vector<Hashtable<String, Object>>();

		try {
			Class.forName("org.sqlite.JDBC");
			try (Connection connection = DriverManager.getConnection("jdbc:sqlite:HolyBible.sqlite3"); PreparedStatement preparedStatement = connection.prepareStatement("select * from t_HolyBible where FullName = ? and ChapterSN = ? and VerseSN >= ? order by rowid asc");) {
				for (int i = 0; i < slist.length; i++) {
					preparedStatement.setString(i + 1, slist[i]);
				}
				try (ResultSet resultSet = preparedStatement.executeQuery();) {
					ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
					int getColumnCount = resultSetMetaData.getColumnCount();

					while (resultSet.next()) {
						Hashtable<String, Object> hso = new Hashtable<String, Object>();
						for (int i = 1; i <= getColumnCount; i++) {
							String getColumnName = resultSetMetaData.getColumnName(i);
							Object sCellValue = resultSet.getObject(getColumnName);
							if (sCellValue == null) {
								sCellValue = "";
							}
							hso.put(getColumnName, sCellValue);
						}
						vhso.add(hso);
					}
				}
			}
		} catch (Exception exception) {
			exception.printStackTrace();
		}

		return vhso;

	}

	public static Vector<Hashtable<String, Object>> selectAccess(String[] slist) {

		Vector<Hashtable<String, Object>> vhso = new Vector<Hashtable<String, Object>>();

		try {
			Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
			try (Connection connection = DriverManager.getConnection("jdbc:ucanaccess://bible7.dat", "admin", "gqrjabcde"); PreparedStatement preparedStatement = connection.prepareStatement("select * from bible, bibleid where bible.VolumeSN = bibleid.SN and FullName = ? and ChapterSN = ? and VerseSN >= ? order by bible.id asc");) {
				for (int i = 0; i < slist.length; i++) {
					preparedStatement.setString(i + 1, slist[i]);
				}
				try (ResultSet resultSet = preparedStatement.executeQuery();) {
					ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
					int getColumnCount = resultSetMetaData.getColumnCount();
					while (resultSet.next()) {
						Hashtable<String, Object> hso = new Hashtable<String, Object>();
						for (int i = 1; i <= getColumnCount; i++) {
							String getColumnName = resultSetMetaData.getColumnName(i);
							Object sCellValue = resultSet.getObject(getColumnName);
							if (sCellValue == null) {
								sCellValue = "";
							}
							hso.put(getColumnName, sCellValue);
						}
						vhso.add(hso);
					}
				}
			}
		} catch (Exception exception) {
			exception.printStackTrace();
		}

		return vhso;
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值