📄 guoqi.java
字号:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class guoqi extends JFrame
{
public guoqi()
{
setTitle("国旗国旗猜猜猜...");
setSize(750,270);
getContentPane().add(new Gqmb());
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args)
{
guoqi p = new guoqi();
}
}
class Gqmb extends JPanel implements ActionListener
{
String x="0",y="0";
java.util.Random random=new java.util.Random();
int i=random.nextInt(8)+1;
xiaomb p=new xiaomb();
JLabel l2=new JLabel("请选择国家");
JLabel l3=new JLabel("测试数:");
JLabel l4=new JLabel("正确数:");
JTextField t1=new JTextField();
String name[]={"澳大利亚","中国","巴西","俄罗斯","南非","意大利","美国","西班牙"};
JComboBox box=new JComboBox(name);
JButton btn1=new JButton("提交答案");
JButton btn2=new JButton("下一面国旗");
Image img;
public Gqmb()
{
setLayout(null);
p.setBounds(20,10,200,176);
add(p);
l2.setBounds(240,10,160,40);
add(l2);
box.setBounds(240,60,160,40);
add(box);
t1.setBounds(240,110,160,70);
t1.setFocusable(false);
add(t1);
btn1.setBounds(420,20,200,40);
add(btn1);
btn2.setBounds(420,70,200,40);
add(btn2);
l3.setBounds(420,120,160,40);
add(l3);
l4.setBounds(420,160,160,40);
add(l4);
btn2.setEnabled(false);
btn1.addActionListener(this);
btn2.addActionListener(this);
}
public class xiaomb extends JPanel
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
img=Toolkit.getDefaultToolkit().getImage(i+".png");
g.drawImage(img,10,48,180,120,this);
}
}
public void actionPerformed(ActionEvent e)
{
Thread th = new Thread();
if(e.getSource().equals(btn1)){
btn1.setEnabled(false);
btn2.setEnabled(true);
x=String.valueOf(Integer.parseInt(x)+1);
repaint();
if(box.getSelectedIndex()+1==i){
t1.setForeground(Color.red);
t1.setFont(new Font("宋体",Font.ITALIC,20));
t1.setText(" 回 答 正 确");
y=String.valueOf(Integer.parseInt(y)+1);
repaint();
btn2.setEnabled(true);
}else{
t1.setForeground(Color.blue);
t1.setFont(new Font("宋体",Font.ITALIC,20));
t1.setText(" 回 答 错 误");
btn2.setEnabled(true);
}
if(Integer.parseInt(x)==8){
btn1.setEnabled(false);
btn2.setEnabled(false);
switch(Integer.parseInt(y)){
case 8:
t1.setForeground(Color.magenta);
t1.setFont(new Font("宋体",Font.ITALIC,20));
t1.setText(" 你 太 棒 了");
break;
case 7:
t1.setForeground(Color.orange);
t1.setFont(new Font("宋体",Font.ITALIC,20));
t1.setText(" 厉 害 厉 害");
break;
case 6:
t1.setForeground(Color.cyan);
t1.setFont(new Font("宋体",Font.ITALIC,20));
t1.setText(" 干 的 不 错");
break;
case 5:
t1.setForeground(Color.black);
t1.setFont(new Font("宋体",Font.ITALIC,20));
t1.setText(" 一 般 般 啦");
break;
default:
t1.setForeground(Color.red);
t1.setFont(new Font("宋体",Font.ITALIC,20));
t1.setText(" 我BS你!");
}
}
}
if(e.getSource().equals(btn2))
{
t1.setText("");
btn1.setEnabled(true);
btn2.setEnabled(false);
i=random.nextInt(8)+1;
repaint();
}
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawString(x,540,150);
g.drawString(y,540,190);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -