如何在Java秋千上创建分页?
您好,我正在尝试在Java Swing中创建一个考试系统,一旦用户提供了一个良好的答案并单击下一个按钮,我将尝试从问题上更改为同一框架的问题。
这是我要做的事情的一个示例:
public static int i = 1;
btnEnter.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(btnEnter)) {
frame.setVisible(false);
JFrame exam = new JFrame("Exam has started.");
exam.setVisible(true);
exam.setLayout(null);
exam.setExtendedState(JFrame.MAXIMIZED_BOTH);
try {
String query = "SELECT * FROM questions WHERE QuestionId = " + i;
PreparedStatement pstmt = ConnectDb.getConnection().prepareStatement(query);
ResultSet rslt = pstmt.executeQuery();
if (rslt.next()) {
// Question label
JLabel Questionlbl = new JLabel();
Questionlbl.setForeground(Color.BLACK);
Questionlbl.setFont(new Font("Arial", Font.PLAIN, 25));
Questionlbl.setBounds(40, 90, 900, 95);
// some more components here
Questionlbl.setText("Question " + i + " " + rslt.getString(2));
btnNext.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(btnNext)) {
//Verify answer, increase counter and update query here...
SwingUtilities.updateComponentTreeUI(exam);
exam.invalidate();
exam.validate();
exam.repaint();
}
}
});
}
catch (Exception ex) {
JOptionPane.showMessageDialog(null,ex,"Error",JOptionPane.ERROR_MESSAGE);
exam.setVisible(true);
}
});
从技术上讲,我有一个带有按钮(例如btnenter)的主框架,我可以单击该按钮以访问一个包含从数据库中检索到的MCQ问题的帧。现在,一旦我单击下一个按钮(例如btnnext),我想它应该能够验证答案(基于用户选择的广播按钮),同时每次提供一个好的答案,同时增加计数器或索引由用户,然后单击框架上的按钮。
问题是当我单击下一个按钮时,它仅验证第一个问题的第一个答案,并且在同一Jframe(例如考试)上没有显示下一个问题(RadioButtons)。总而言之,我无法将计数器从下一个按钮增加来更新SQL查询,以便指向下一个记录。
我之前曾尝试使用循环,但徒劳无功。我想也许我做错了。
有人知道如何确切地做到这一点吗?
Hello I am trying to create an examination system in Java swing and I am trying to change from question to question on the same frame once a user provides a good answer and clicks on a Next button.
Here is an example of what I am trying to do:
public static int i = 1;
btnEnter.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(btnEnter)) {
frame.setVisible(false);
JFrame exam = new JFrame("Exam has started.");
exam.setVisible(true);
exam.setLayout(null);
exam.setExtendedState(JFrame.MAXIMIZED_BOTH);
try {
String query = "SELECT * FROM questions WHERE QuestionId = " + i;
PreparedStatement pstmt = ConnectDb.getConnection().prepareStatement(query);
ResultSet rslt = pstmt.executeQuery();
if (rslt.next()) {
// Question label
JLabel Questionlbl = new JLabel();
Questionlbl.setForeground(Color.BLACK);
Questionlbl.setFont(new Font("Arial", Font.PLAIN, 25));
Questionlbl.setBounds(40, 90, 900, 95);
// some more components here
Questionlbl.setText("Question " + i + " " + rslt.getString(2));
btnNext.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(btnNext)) {
//Verify answer, increase counter and update query here...
SwingUtilities.updateComponentTreeUI(exam);
exam.invalidate();
exam.validate();
exam.repaint();
}
}
});
}
catch (Exception ex) {
JOptionPane.showMessageDialog(null,ex,"Error",JOptionPane.ERROR_MESSAGE);
exam.setVisible(true);
}
});
Technically, I had a main frame with a button (e.g btnEnter) which I could click to access another frame containing MCQ questions retrieved from a database. Now, once I click on the Next button (e.g btnNext) I guess it should be able to verify the answer (based on a radio button selected by the user) and at the same time increase the counter or index everytime a good answer is provided by the user and clicks on the button on the frame.
The problem is when I click on the Next button it only verifies the first answer of the first question and it does not display the next question with its options (radiobuttons) on the same Jframe (e.g exam). To sum up, I cannot increase the counter from the next button to update the SQL query so that it points to the next record.
I previously tried to use loops but in vain. I guess maybe I've been doing it wrong.
Does anyone knows how to exactly do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
让我们尝试将代码解除倒计时并使用单个责任原则使用委托和依赖注入之类的东西。
,我们将需要某种“容器”类,以管理一个问题和答案,例如
...
首先 ,我们需要某种类似的类,这些类充当我们的数据源,我们可以加载问题
为什么使用
接口
这样?您会在一分钟内看到为什么,但是首先,让我们创建一个基于SQL数据库的数据源的实现:我没有SQL数据库可以测试,因此您必须填写Core详细信息
上面的演示是由SQL数据库支持的问题/答案数据源的基本概念。
关于此事的好处是,任何
QUASICE andSwerDataSource
是可以预期的,我们可以通过UI来使用它...核心功能围绕
nextQuestion
和updateUistate
方法。这些负责加载下一个问题,如果存在并相应地更新UI。现在,正如我说的那样,我没有SQL数据库可以进行测试,因此,我创建了自己的“测试”数据源...
然后可以将其插入UI并允许进行控制的测试工作流程。 。
现在
Let's try and decouple the code and make use of the Single Responsibility Principle and make use things like delegation and dependency injection.
First, we're going to need some kind of "container" class which manages a single question and answer, for example...
nb: I'm assuming a multiple choice style question/answer workflow
Now, we need some kind class which acts as our data source, through which we can load questions
Why make use of
interface
like this? You'll see why in a minute, but first, lets create a SQL database based implementation of the data sourcenb: I don't have a SQL database to test against, so you'll have to fill out the core details
The above demonstrations a basic concept of a question/answer data source which is backed by a SQL database.
The good thing about this is, anywhere
QuestionAnswerDataSource
is expected, we can make use of it, for example, through the UI...The core functionality revolves around the
nextQuestion
andupdateUIState
methods. These are responsible for loading the next question, if one exists and updating the UI correspondingly.Now, as I said, I don't have a SQL database to test against, so, instead, I created my own "test" datasource...
This can then be plugged into the UI and allows for a controlled testing workflow...
Now, when you're ready, you can replace
TestQuestionAnswerDataSource
withSQLQustionAnswerDataSource
and the UI should continue to operate without (????) issue