网格包,需要加card布局

package a;

import javax.swing.*;
import javax.swing.plaf.FontUIResource;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class Main extends JFrame implements ActionListener, KeyListener {
	public static void main(String[] args) {
		Main m = new Main();
		m.setBounds(0, 0, 1000, 700);
		m.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		m.setExtendedState(JFrame.MAXIMIZED_BOTH);
// m.pack();
		m.setVisible(true);

	}

	public Main() {
		this.setJMenuBar(this.jmb());
		this.addKeyListener(this);
		this.jp.addKeyListener(this);
		this.add(this.jp(), BorderLayout.NORTH);
		this.add(this.jspCenter(), BorderLayout.CENTER);
	}

	public JMenuBar jmb() {
		JMenuBar jmb = new JMenuBar();
		JButton jb = new JButton("start");
		jb.addKeyListener(this);
		jb.addActionListener(this);
		jmb.add(jb);
		return jmb;
	}

	JPanel jp = new JPanel();

	public JPanel jp() {
		jp.addKeyListener(this);
		String[] s = { "创世记", "出埃及记", "利未记", "民数记", "申命记", "约书亚记", "士师记", "路得记", "撒母耳记上", "撒母耳记下", "列王纪上", "列王纪下",
				"历代志上", "历代志下", "以斯拉记", "尼希米记", "以斯帖记", "约伯记", "诗篇", "箴言", "传道书", "雅歌", "以赛亚书", "耶利米书", "耶利米哀歌", "以西结书",
				"但以理书", "何西阿书", "约珥书", "阿摩司书", "俄巴底亚书", "约拿书", "弥迦书", "那鸿书", "哈巴谷书", "西番雅书", "哈该书", "撒迦利亚书", "玛拉基书",
				"马太福音", "马可福音", "路加福音", "约翰福音", "使徒行传", "罗马书", "哥林多前书", "哥林多后书", "加拉太书", "以弗所书", "腓立比书", "歌罗西书",
				"帖撒罗尼迦前书", "帖撒罗尼迦后书", "提摩太前书", "提摩太后书", "提多书", "腓利门书", "希伯来书", "雅各书", "彼得前书", "彼得后书", "约翰壹书", "约翰贰书",
				"约翰叁书", "犹大书", "启示录" };
		JButton[] jb = new JButton[s.length];
		GridBagLayout gbl = new GridBagLayout();

		GridBagConstraints gbc = new GridBagConstraints();
		jp.setLayout(gbl);
		// gbc.fill = GridBagConstraints.BOTH;
		// gbc.anchor = GridBagConstraints.CENTER;

		JLabel jl1 = new JLabel("律法书");
		jl1.setFont(new Font("微软雅黑", Font.BOLD, 20));
		gbc.gridx = 0;
		gbc.gridy = 0;
		jp.add(jl1, gbc);
		for (int i = 0; i < 5; i++) {
			jb[i] = new JButton(s[i]);
			jb[i].setFont(new Font("", Font.BOLD, 14));
			jb[i].setBackground(Color.RED);
			jb[i].setForeground(Color.WHITE);
			gbc.gridx = 0;
			gbc.gridy = i + 1;
			// gbc.weightx = 1;
			// gbc.weighty = 1;

			jp.add(jb[i], gbc);
		}
		JLabel jl2 = new JLabel("历史书");
		jl2.setFont(new Font("微软雅黑", Font.BOLD, 20));
		gbc.gridx = 1;
		gbc.gridy = 0;
		jp.add(jl2, gbc);
		for (int i = 5; i < 17; i++) {
			gbc.gridx = 1;
			gbc.gridy = i - 4;
//gbc.weightx = 1;
			// gbc.weighty = 1;
			jb[i] = new JButton(s[i]);
			jb[i].setFont(new Font("", Font.BOLD, 14));
			jb[i].setBackground(Color.ORANGE);
			jb[i].setForeground(Color.BLACK);
			jp.add(jb[i], gbc);
		}
		JLabel jl3 = new JLabel("诗歌·智慧书");
		jl3.setFont(new Font("微软雅黑", Font.BOLD, 20));
		gbc.gridx = 2;
		gbc.gridy = 0;
		jp.add(jl3, gbc);
		for (int i = 17; i < 22; i++) {
			gbc.gridx = 2;
			gbc.gridy = i - 16;
//gbc.weightx = 1;
			// gbc.weighty = 1;
			jb[i] = new JButton(s[i]);
			jb[i].setFont(new Font("", Font.BOLD, 14));
			jb[i].setBackground(Color.YELLOW);
			jb[i].setForeground(Color.BLACK);
			jp.add(jb[i], gbc);
		}
		JLabel jl4 = new JLabel("先知书");
		jl4.setFont(new Font("微软雅黑", Font.BOLD, 20));
		gbc.gridx = 3;
		gbc.gridy = 0;
		jp.add(jl4, gbc);
		for (int i = 22; i < 39; i++) {
			gbc.gridx = 3;
			gbc.gridy = i - 21;
//gbc.weightx = 1;
			// gbc.weighty = 1;
			jb[i] = new JButton(s[i]);
			jb[i].setFont(new Font("", Font.BOLD, 14));
			jb[i].setBackground(Color.GREEN);
			jb[i].setForeground(Color.BLACK);
			jp.add(jb[i], gbc);
		}
		JLabel jl5 = new JLabel("四福音");
		jl5.setFont(new Font("微软雅黑", Font.BOLD, 20));
		gbc.gridx = 4;
		gbc.gridy = 0;
		jp.add(jl5, gbc);
		for (int i = 39; i < 43; i++) {
			gbc.gridx = 4;
			gbc.gridy = i - 38;
//gbc.weightx = 1;
			// gbc.weighty = 1;
			jb[i] = new JButton(s[i]);
			jb[i].setFont(new Font("", Font.BOLD, 14));
			jb[i].setBackground(Color.CYAN);
			jb[i].setForeground(Color.BLACK);
			jp.add(jb[i], gbc);
		}
		JLabel jl6 = new JLabel("教会历史");
		jl6.setFont(new Font("微软雅黑", Font.BOLD, 20));
		gbc.gridx = 5;
		gbc.gridy = 0;
		jp.add(jl6, gbc);
		for (int i = 43; i < 44; i++) {
			gbc.gridx = 5;
			gbc.gridy = i - 42;
//gbc.weightx = 1;
			// gbc.weighty = 1;
			jb[i] = new JButton(s[i]);
			jb[i].setFont(new Font("", Font.BOLD, 14));
			jb[i].setBackground(Color.BLUE);
			jb[i].setForeground(Color.WHITE);
			jp.add(jb[i], gbc);
		}
		JLabel jl7 = new JLabel("书信");
		jl7.setFont(new Font("微软雅黑", Font.BOLD, 20));
		gbc.gridx = 6;
		gbc.gridy = 0;
		jp.add(jl7, gbc);
		for (int i = 44; i < 65; i++) {
			gbc.gridx = 6;
			gbc.gridy = i - 43;
//gbc.weightx = 1;
			// gbc.weighty = 1;
			jb[i] = new JButton(s[i]);
			jb[i].setFont(new Font("", Font.BOLD, 14));
			jb[i].setBackground(Color.MAGENTA);
			jb[i].setForeground(Color.BLACK);
			jp.add(jb[i], gbc);
		}
		JLabel jl8 = new JLabel("对约翰的启示");
		jl8.setFont(new Font("微软雅黑", Font.BOLD, 20));
		gbc.gridx = 7;
		gbc.gridy = 0;
		jp.add(jl8, gbc);
		for (int i = 65; i < 66; i++) {
			gbc.gridx = 7;
			gbc.gridy = i - 64;
//gbc.weightx = 1;
			// gbc.weighty = 1;
			jb[i] = new JButton(s[i]);
			jb[i].setFont(new Font("", Font.BOLD, 14));
			jb[i].setBackground(Color.PINK);
			jb[i].setForeground(Color.BLACK);
			jp.add(jb[i], gbc);
		}
		for (int i = 0; i < s.length; i++) {
			jb[i].addActionListener(this);
			jb[i].addKeyListener(this);
		}
		return jp;
	}

	JTextPane jtp = new JTextPane();
	JScrollPane jsp = new JScrollPane(jtp);

	public JScrollPane jspCenter() {
		this.jtp.setEditable(false);
		this.jtp.setBackground(Color.BLACK);
		this.jtp.setFont(new FontUIResource("微软雅黑", Font.BOLD, 80));
		return this.jsp;
	}

	public void actionPerformed(ActionEvent e) {
		String s = e.getActionCommand();
		System.out.println(s);
		if (s.equals("start")) {
			if (this.jp.isVisible()) {
				this.jp.setVisible(false);
			} else {
				this.jp.setVisible(true);
			}
		} else {
			this.jp.setVisible(false);
		}
	}

	public void keyPressed(KeyEvent e) {
		int i = e.getKeyCode();
		if (i == 10) {
			if (this.jp.isVisible()) {
				this.jp.setVisible(false);
			} else {
				this.jp.setVisible(true);
			}
		}
	}

	public void keyTyped(KeyEvent e) {

	}

	public void keyReleased(KeyEvent e) {

	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值