import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
/**
*
* @author Li Qingguang build a intellectframe to show some interesting
* questions use the borderlayout set a cardLayout to display questions
*/
public class Intellect extends JFrame {
public Intellect() {
// set window size
Toolkit kit = Toolkit.getDefaultToolkit();
int screenHeight = 900;
int screenWidth = 1440;
// set frame width, height and location
setSize(screenWidth / 2, screenHeight / 2);
setResizable(false);
setLocation(screenWidth / 4 + 40, screenHeight / 4 + 40);
// set frame icon and title
Image img = kit.getImage(Intellect.class.getResource("bagua.jpg"));
setIconImage(img);
setTitle("necromancer");
// build cardlayout
cardLayout = new CardLayout(20, 10);
cardPanel = new JPanel(cardLayout);
// add different card to show question
Q1 q1 = new Q1();
cardPanel.add(q1, "1");
Q2 q2 = new Q2();
cardPanel.add(q2, "2");
Q3 q3 = new Q3();
cardPanel.add(q3, "3");
Q4 q4 = new Q4();
cardPanel.add(q4, "4");
Q5 q5 = new Q5();
cardPanel.add(q5, "5");
Q6 q6 = new Q6();
cardPanel.add(q6, "6");
Q7 q7 = new Q7();
cardPanel.add(q7, "7");
Q8 q8 = new Q8();
cardPanel.add(q8, "8");
Q9 q9 = new Q9();
cardPanel.add(q9, "9");
Q10 q10 = new Q10();
cardPanel.add(q10, "10");
Q11 q11 = new Q11();
cardPanel.add(q11, "11");
Q12 q12 = new Q12();
cardPanel.add(q12, "12");
Q13 q13 = new Q13();
cardPanel.add(q13, "13");
Q14 q14 = new Q14();
cardPanel.add(q14, "14");
Q15 q15 = new Q15();
cardPanel.add(q15, "15");
Q16 q16 = new Q16();
cardPanel.add(q16, "16");
JPanel p = new JPanel();
JButton First = new JButton("First");
JButton Next = new JButton("Next");
JButton Previous = new JButton("Previous");
JButton Last = new JButton("Last");
JLabel question = new JLabel("Question");
// build a JComboBox
box = new JComboBox();
for (int i = 1; i <= 16; i++)
box.addItem(String.valueOf(i));
p.add(First);
p.add(Next);
p.add(Previous);
p.add(Last);
p.add(question);
p.add(box);
// add the cardPanel to the frame
getContentPane().add(cardPanel, BorderLayout.CENTER);
getContentPane().add(p, BorderLayout.SOUTH);
ActionOne ActionOne = new ActionOne();
First.addActionListener(ActionOne);
Next.addActionListener(ActionOne);
Previous.addActionListener(ActionOne);
Last.addActionListener(ActionOne);
ActionTwo ActionTwo = new ActionTwo();
box.addItemListener(ActionTwo);
}
private CardLayout cardLayout;
JPanel cardPanel;
JComboBox box;
// Handle button actions
private class ActionOne implements ActionListener {
public void actionPerformed(ActionEvent e) {
String actionCommand = e.getActionCommand();
if (e.getSource() instanceof JButton)
if ("First".equals(actionCommand))
cardLayout.first(cardPanel);
else if ("Last".equals(actionCommand))
cardLayout.last(cardPanel);
else if ("Previous".equals(actionCommand))
cardLayout.previous(cardPanel);
else if ("Next".equals(actionCommand))
cardLayout.next(cardPanel);
}
}
// Handle selection of combo box item
private class ActionTwo implements ItemListener {
public void itemStateChanged(ItemEvent e) {
if (e.getSource() == box)
cardLayout.show(cardPanel, (String) e.getItem());
}
}
// set different question
private class Q1 extends JPanel {
public Q1() {
JLabel picture = new JLabel(new ImageIcon(Q1.class.getResource("think.jpg")));
JLabel q1 = new JLabel("狮子和猎豹在草原上进行百米赛跑,如果从同一起点起跑,狮子跑到一百米终点时,");
JLabel q2 = new JLabel("猎豹只跑到90米,现在让狮子从起点退后10米起跑,那么它们谁先到达终点呢?");
JButton answer = new JButton("Answer");
GetAnswer getAnswer = new GetAnswer();
answer.addActionListener(getAnswer);
GridBagLayout layout = new GridBagLayout();
setLayout(layout);
add(picture, new GBC(0, 0, 2, 4).setAnchor(GBC.CENTER));
add(q1, new GBC(0, 4, 2, 1).setAnchor(GBC.CENTER));
add(q2, new GBC(0, 5, 2, 1).setAnchor(GBC.WEST));
add(answer, new GBC(1, 5, 1, 1).setAnchor(GBC.EAST));
}
private class GetAnswer implements ActionListener {
public void actionPerformed(ActionEvent event) {
JOptionPane.showMessageDialog(Intellect.this, " Answer : 狮子 ",
"necromancer", JOptionPane.INFORMATION_MESSAGE);
}
}
}
private class Q2 extends JPanel {
public Q2() {
JLabel picture = new JLabel(new ImageIcon(Q2.class.getResource("think.jpg")));
JLabel q = new JLabel("爷俩娘俩兄妹俩,一共只有三个人,这是为什么?");
JButton answer = new JButton("Answer");
GetAnswer getAnswer = new GetAnswer();
answer.addActionListener(getAnswer);
GridBagLayout layout = new GridBagLayout();
setLayout(layout);
add(picture, new GBC(0, 0, 2, 4).setAnchor(GBC.CENTER));
add(q, new GBC(0, 4, 2, 1).setAnchor(GBC.CENTER));
add(answer, new GBC(1, 5, 1, 1).setAnchor(GBC.EAST));
}
private class GetAnswer implements ActionListener {
public void actionPerformed(ActionEvent event) {
JOptionPane.showMessageDialog(Intellect.this,
" Answer :儿子、母亲和舅舅 ", "necromancer",
JOptionPane.INFORMATION_MESSAGE);
}
}
}
private class Q3 extends JPanel {
public Q3() {
JLabel picture = new JLabel(new ImageIcon(Q3.class.getResource("think.jpg")));
JLabel q = new JLabel("1,2,3所能组成的最大数是多少?");
JButton answer = new JButton("Answer");
GetAnswer getAnswer = new GetAnswer();
answer.addActionListener(getAnswer);
GridBagLayout layout = new GridBagLayout();
setLayout(layout);
add(picture, new GBC(0, 0, 2, 4).setAnchor(GBC.CENTER));
add(q, new GBC(0, 4, 2, 1).setAnchor(GBC.CENTER));
add(answer, new GBC(1, 5, 1, 1).setAnchor(GBC.EAST));
}
private class GetAnswer implements ActionListener {
public void actionPerformed(ActionEvent event) {
JOptionPane.showMessageDialog(Intellect.this,
" Answer : 3的21次方 ", "necromancer",
JOptionPane.INFORMATION_MESSAGE);
}
}
}
private class Q4 extends JPanel {
public Q4() {
JLabel picture = new JLabel(new ImageIcon(Q4.class.getResource("think.jpg")));
JLabel q = new JLabel(" 什么时候,四减一等于五?");
JButton answer = new JButton("Answer");
GetAnswer getAnswer = new GetAnswer();
answer.addActionListener(getAnswer);
GridBagLayout layout = new GridBagLayout();
setLayout(layout);
add(picture, new GBC(0, 0, 2, 4).setAnchor(GBC.CENTER));
add(q, new GBC(0, 4, 2, 1).setAnchor(GBC.CENTER));
add(answer, new GBC(1, 5, 1, 1).setAnchor(GBC.EAST));
}
private class GetAnswer implements ActionListener {
public void actionPerformed(ActionEvent event) {
JOptionPane.showMessageDialog(Intellect.this,
" Answer : 一个四边形,切下一个角,还有五个角 ", "necromancer",
JOptionPane.INFORMATION_MESSAGE);
}
}
}
private class Q5 extends JPanel {
public Q5() {
JLabel picture = new JLabel(new ImageIcon(Q5.class.getResource("think.jpg")));
JLabel q = new JLabel(" 什么是倾国倾城貌?");
JButton answer = new JButton("Answer");
GetAnswer getAnswer = new GetAnswer();
answer.addActionListener(getAnswer);
GridBagLayout layout = new GridBagLayout();
setLayout(layout);
add(picture, new GBC(0, 0,
java 小游戏 趣味测试
需积分: 12 120 浏览量
2010-05-19
11:13:15
上传
评论
收藏 468KB RAR 举报
Iamtheodore
- 粉丝: 5
- 资源: 95