- <p style="line-height: 30px; text-indent: 2em;"></p><p style="line-height: 30px; text-indent: 2em;"></p><p style="line-height: 30px; text-indent: 2em;"></p><p style="line-height: 30px; text-indent: 2em;"><img border="0" alt="" src="http://bbs.itheima.com/forum.php?mod=image&aid=21183&size=300x300&key=8251943c17637265&nocache=yes&type=fixnone" aid="attachimg_21183"></p><p> </p><p> </p><p>上面是我要实现的程序窗口,基本界面已经实现了。</p><p> </p><p>我的设想是:点<font color="red">击下一题或者答对了题自动切换下一题的题目。</font></p><p><font color="#ff0000"></font> </p><p><font color="#ff0000">我在实现的过程中,用重新new 一次的方法,但是感觉这样不是正确的步骤。</font></p><p><font color="#ff0000"></font> </p><p><font color="#ff0000">求思想,求实现。</font></p><p><font color="#ff0000"></font> </p><p><font color="#ff0000"></font> </p><p><font color="#ff0000">代码被放到了一个文件中 ↓↓↓↓↓↓↓↓↓↓</font></p><div class="blockcode"><blockquote>
复制代码 import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
import org.sqlite.JDBC;
class MainWindow extends JFrame
{
MainWindow()
{
super("欢迎使用!");
setSize(400,400);
setLocation(400,100);
JPanel jp = new JPanel();
ImageIcon im= new ImageIcon("asd.jpg");
JLabel jl = new JLabel(im);
JButton jb = new JButton("打开主程序");
jb.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
dispose();
new MyWindow();
}
});
add(jp);
jp.add(jl);
jp.add(jb);
setVisible(true);
}
}
class MyWindow extends JFrame
{
MyWindow()
{
super("计算机基础知识学习");
setSize(500,700);
setLocation(400,5);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(3,1));
final String[] str = new String[6];
getString(str);
JLabel j1 = new JLabel("正确数:");
j1.setForeground(Color.red);
JLabel j2 = new JLabel("错误数:");
JLabel j3 = new JLabel("做题总数:");
j2.setForeground(Color.red);
JTextArea ja = new JTextArea(str[0],3,20);
ja.setBackground(Color.green);
ja.setFont(new Font("Serif",0,30));
ja.setEditable(false);
ja.setLineWrap(true);
ja.setWrapStyleWord(true);
JButton jb1 = new JButton(str[1]);
jb1.setHorizontalAlignment(JButton.LEFT);
jb1.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//dispose();
String st="A";
if(st.equals(str[5]))
{
new True();
}
else
{
new False();
setjb1();
}
}
});
JButton jb2 = new JButton(str[2]);
jb2.setHorizontalAlignment(JButton.LEFT);
jb2.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//dispose();
String st="B";
if(st.equals(str[5]))
new True();
else
new False();
}
});
JButton jb3 = new JButton(str[3]);
jb3.setHorizontalAlignment(JButton.LEFT);
jb3.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//dispose();
String st="C";
if(st.equals(str[5]))
new True();
else
new False();
}
});
JButton jb4 = new JButton(str[4]);
jb4.setHorizontalAlignment(JButton.LEFT);
jb4.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//dispose();
String st="D";
if(st.equals(str[5]))
new True();
else
new False();
}
});
JButton jb5 = new JButton("下一题");
jb5.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
getString(str);
repaint();
//dispose();
//new MyWindow();
}
});
JPanel jp1 = new JPanel();
JPanel jp2 = new JPanel(new GridLayout(1,2));
JPanel jp3 = new JPanel(new GridLayout(4,1));
JPanel jp4 = new JPanel();
jp4.setBackground(Color.gray);
jp4.setLayout(new GridLayout(4,1));
jp2.setLayout(new GridLayout());
jp2.add(ja);
jp3.add(jb1);
jp3.add(jb2);
jp3.add(jb3);
jp3.add(jb4);
jp4.add(jb5);
jp4.add(j1);
jp4.add(j2);
jp4.add(j3);
//add(jp1);
add(jp2);
add(jp3);
add(jp4);
setVisible(true);
}
/*
这块程序的作用是,连接数据库,读取一条记录,并将记录的各个字段内容赋给Sting数组。
*/
public String[] getString(String[] str)
{
try{
//连接SQLite的JDBC
Class.forName("org.sqlite.JDBC");
//建立一个数据库名zieckey.db的连接,如果不存在就在当前目录下创建之
Connection conn = DriverManager.getConnection("jdbc:sqlite:tiku.db");
Statement stat = conn.createStatement();
ResultSet rs = stat.executeQuery("select * from shitiku order by random() limit 1"); //查询数据
str[0]="题目: "+rs.getString("题目");
str[1]="A."+rs.getString("A");
str[2]="B."+rs.getString("B");
str[3]="C."+rs.getString("C");
str[4]="D."+rs.getString("D");
str[5]=rs.getString("答案");
rs.close();
conn.close(); //结束数据库的连接
}
catch( Exception e )
{
e.printStackTrace ( );
}
return str;
}
}
class True extends JFrame
{
True()
{
super("");
setSize(150,150);
setLocation(500,100);
setVisible(true);
JPanel jp = new JPanel();
JLabel jb = new JLabel("恭喜,答对了!");
jb.setFont(new Font("Serif",0,20));
add(jp);
jp.add(jb);
}
}
class False extends JFrame
{
False()
{
super("");
setSize(150,150);
setLocation(500,100);
setVisible(true);
JPanel jp = new JPanel();
JLabel jb = new JLabel("Sorry,选错了!");
jb.setFont(new Font("Serif",0,20));
add(jp);
jp.add(jb);
}
}
public class MyApp
{
public static void main(String[] args)
{
MainWindow mw = new MainWindow();
}
}
|
|