A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 胡滨 中级黑马   /  2013-6-15 07:33  /  2652 人查看  /  12 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. <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();       
                       
        }
}


捕获.JPG (47.28 KB, 下载次数: 0)

捕获.JPG

12 个回复

倒序浏览
编辑按钮在哪里!!!!!!!!!!!!

点评

在帖子下边  发表于 2013-6-15 07:47
回复 使用道具 举报


没有编辑按钮,在哪啊、、、、、、、、、、、
回复 使用道具 举报

回帖奖励 +5

找到编辑没啊,就在你的帖子下边,仔细找找,看见你的问题挺好,过来抢 楼
回复 使用道具 举报
Renica 发表于 2013-6-15 08:26
找到编辑没啊,就在你的帖子下边,仔细找找,看见你的问题挺好,过来抢 楼 ...

:Q  还是没有。另外几个帖子都能编辑,就这个不能,不知道咋回事
回复 使用道具 举报
胡滨 发表于 2013-6-15 08:32
还是没有。另外几个帖子都能编辑,就这个不能,不知道咋回事

估计你的代码长,在图片下面找找
回复 使用道具 举报

回帖奖励 +5

{:soso_e110:}
回复 使用道具 举报

回帖奖励 +5

楼主您好~帖子长时间未作出回答,我已经将您的帖子改成已解决。如果有问题的话可以私密我哦~
回复 使用道具 举报

回帖奖励 +5

看了好久没看明白
回复 使用道具 举报

回帖奖励 +5

没有看懂:L
回复 使用道具 举报
表示没看懂,还要继续努力!
回复 使用道具 举报
好乱!没有注释吗
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马