2016-12-07 49 views
-2

我想構建一個測驗應用程序,並設法構建數據庫。我使用卡布局創建了一個GUI來在面板之間切換,並試圖從數據庫中將問題和選項從JtextArea中檢索到,但沒有成功。我的問題是如何獲得可視化組件(jtext區域,jbutton顯示來自數據庫的內容)我希望它們根據問題編號自動設置文本。如何在Java中的JButton,JTextArea上顯示從數據庫檢索的文本?

這是我的代碼

package quizzGUI; 

import java.awt.EventQueue; 

import javax.swing.JFrame; 
import java.awt.CardLayout; 
import javax.swing.JPanel; 
import javax.swing.JTextArea; 
import javax.swing.JLabel; 
import javax.swing.JOptionPane; 

import java.awt.Font; 
import java.awt.List; 

import javax.swing.SwingConstants; 
import javax.swing.JButton; 
import java.awt.Color; 
import javax.swing.JRadioButton; 
import javax.swing.JTextPane; 
import java.awt.event.ActionListener; 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 
import java.sql.Statement; 
import java.util.ArrayList; 
import java.awt.event.ActionEvent; 

import javax.management.Query; 
import javax.swing.ButtonGroup; 

public class quizzMain { 

    private JFrame frame; 
    private JPanel Menu; 
    private JPanel Playing; 
    private JPanel Score; 
    private final ButtonGroup buttonGroup = new ButtonGroup(); 
    public JTextPane textPane; 
    public JButton btnA; 
    public JRadioButton rdbtnB; 
    public JRadioButton rdbtnC; 
    public JRadioButton rdbtnD; 
    public JTextArea textArea; 


    /** 
    * Launch the application. 
    */ 
    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       try { 
        quizzMain window = new quizzMain(); 
        window.frame.setVisible(true); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 

    /** 
    * Create the application. 
    */ 

    Connection connection = null; 
    public quizzMain() { 
     initialize(); 
     connection = sqlConnection.dbConnector(); 

    } 

    private void initialize() { 

     Connection connection = sqlConnection.dbConnector(); 
     Statement stmt=null; 

     frame = new JFrame(); 
     frame.setBounds(100, 100, 450, 300); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().setLayout(new CardLayout(0, 0)); 

     final JPanel Menu = new JPanel(); 
     frame.getContentPane().add(Menu, "name_668313932787145"); 
     Menu.setLayout(null); 
     Menu.setVisible(true); 

     final JPanel Playing = new JPanel(); 
     Playing.setBackground(Color.CYAN); 
     frame.getContentPane().add(Playing, "name_668321772390383"); 
     Playing.setLayout(null); 
     Playing.setVisible(false); 

     final JPanel Score = new JPanel(); 
     frame.getContentPane().add(Score, "name_668324579994343"); 
     Score.setLayout(null); 
     Score.setVisible(false); 

     JLabel lblNewLabel = new JLabel("C QUIZZ"); 
     lblNewLabel.setFont(new Font("Times New Roman", Font.BOLD | Font.ITALIC, 14)); 
     lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER); 
     lblNewLabel.setBounds(123, 47, 169, 40); 
     Menu.add(lblNewLabel); 

     JButton btnStart = new JButton("Start"); 
     btnStart.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent arg0) { 
       Integer i=0; 

       Playing.setVisible(true); 
       Menu.setVisible(false); 
        try{ 
         String query = ("SELECT * FROM Question "); 
         PreparedStatement pst=connection.prepareStatement(query); 
         ResultSet rs=pst.executeQuery(); 
         do { 

           btnA.setText(rs.getString("ANSWERA")); 



         }while(rs.next()); 

        }catch(Exception e){ 
         JOptionPane.showMessageDialog(null, e); 
        e.printStackTrace(); 
        }   

      } 
     }); 
     btnStart.setFont(new Font("Tahoma", Font.BOLD, 12)); 
     btnStart.setBounds(167, 176, 89, 23); 
     Menu.add(btnStart); 

     JButton btnFinish = new JButton("Finish"); 
     btnFinish.setBounds(335, 227, 89, 23); 
     Playing.add(btnFinish); 
     btnFinish.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent arg0) { 
       Score.setVisible(true); 
       Playing.setVisible(false); 
      } 
     }); 





     JButton btnNextQuestion = new JButton("Next"); 
     btnNextQuestion.setBounds(10, 227, 89, 23); 
     Playing.add(btnNextQuestion); 

     JTextArea textArea = new JTextArea(); 
     textArea.setBounds(20, 11, 387, 41); 
     Playing.add(textArea); 

     JButton btnA = new JButton("New button"); 
     btnA.setBounds(20, 84, 381, 41); 
     Playing.add(btnA); 





     JTextArea txtrScore = new JTextArea(); 
     txtrScore.setBounds(87, 74, 244, 43); 
     txtrScore.setText("Score:"); 
     Score.add(txtrScore); 
    } 
} 
+0

'textArea.setText(「something」)'? –

+0

您首先將DB中的文本選擇爲字符串。然後,根據組件,使用適當的方法(Jbutton - 'setLabel',JTextArea --' setText'等) –

+0

''我試圖從數據庫中檢索問題和選項到沒有成功的JtextArea .. 「 - 沒有告訴我們細節並向我們展示相關的代碼,我們無法知道自己可能做錯了什麼,我們最好的建議是讓您檢查教程。如果您仍然需要更具體的幫助,請考慮改進此問題,包括告訴細節並創建並顯示您的[mcve]。 –

回答

0

因爲你沒有提到你的代碼,它很難給你一個準確的短例子。我建議你通過這個tutorial檢索信息(姓名和號碼)並將其顯示在JTextArea中。

提取從下面的教程中的關鍵點:

數據庫連接的建立和獲取信息

String url = "jdbc:mysql://localhost:3306/"; 
String db = "databasename"; 
String driver = "com.mysql.jdbc.Driver"; 
String user = "xxxx"; 
String pass = "xxxx"; 
Connection con = null; 
PreparedStatement pstatement = null; 

Class.forName(driver).newInstance(); 
con = DriverManager.getConnection(url + db , user, pass); 

pstatement = con.prepareStatement("SELECT name, phonenumber FROM tablename"); 
con = DriverManager.getConnection(url + db, user, pass); 
ResultSet rs = pstatement.executeQuery(); 

while(rs.next()){ 
    String name = rs.getString(1); 
    int phonenumber = rs.getInt(2); 
} 

設置文本中的JTextArea

然後使用JTextAreasetText方法來設置文本。例如,recordTextArea.setText(name + " " + phonenumber);

+0

對不起,沒有把代碼 – carlos

+0

@卡洛斯沒有問題,通過教程,這很容易,並會給你一個全面的想法。 –

相關問題