2015-08-19 62 views
0
import java.awt.*; 
import java.awt.event.*; 

import javax.swing.*; 
import javax.swing.event.DocumentEvent; 
import javax.swing.text.Document; 

import java.sql.Connection; 
import java.sql.DatabaseMetaData; 
import java.sql.DriverManager; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.sql.Statement; 


@SuppressWarnings("serial") 
public class Save extends JPanel { 

Connection con; 
Statement st; 
ResultSet rs; 

String TrailerName; 
String BlockName; 
String LocationName; 
String newline = "\n"; 
String text; 

JComboBox<?> TrailerList; 
JComboBox<?> BlockList; 
JComboBox<?> LocationList; 
JTextField textField; 
JTextArea textArea; 

public Save() { 
    super(new BorderLayout()); 

    textField = new JTextField(20); 

    textArea = new JTextArea(5, 20); 
    textArea.setEditable(false); 
    textField.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent evt) { 
        text = textField.getText(); 
        textArea.append(text + newline); 
        textField.selectAll(); 

        textArea.setCaretPosition(textArea.getDocument().getLength()); 
        System.out.println("Comment: " + text); 
       } 
       }); 

    String[] Trailer = { "A1", "A2", "B1", "B2", "C1", "Please select a Trailer" }; 
    String[] Block = { "A", "B", "C", "D", "E", "Please select a Block" }; 
    String[] Location = { "Door A3", "Door B2", "Door C4", "Door D2", "Door E9", "Please select a Location" }; 

    TrailerList = new JComboBox<Object>(Trailer); 
    TrailerList.setSelectedIndex(5); 
    TrailerList.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent evt) { 
       TrailerName = (String) TrailerList.getSelectedItem(); 
       System.out.println("Trailer: " + TrailerName); 
     } 
    }); 

    BlockList = new JComboBox<Object>(Block); 
    BlockList.setSelectedIndex(5); 
    BlockList.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent evt) { 
        BlockName = (String) BlockList.getSelectedItem(); 
        System.out.println("Block: " + BlockName); 
    } 

});Jtext字段無需輸入即可收聽

LocationList = new JComboBox<Object>(Location); 
    LocationList.setSelectedIndex(5); 
    LocationList.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent evt) { 
        LocationName = (String) LocationList.getSelectedItem(); 
        System.out.println("Location: " + LocationName); 
    } 

});

JButton buttonSave = new JButton("Save"); 
    add(buttonSave); 
    buttonSave.setActionCommand("Save"); 
    add(buttonSave); 
    buttonSave.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent evt) { 
      String action = evt.getActionCommand(); 
       if (action.equals("Save")) { 
        System.out.println("Button pressed!"); 
        try { 
         String driver = "sun.jdbc.odbc.JdbcOdbcDriver"; 
         Class.forName(driver); 
         String db = "jdbc:odbc:TTracking"; 
        con = DriverManager.getConnection(db); 

        DatabaseMetaData meta = con.getMetaData(); 
        System.out.println("Server name: " + 
      meta.getDatabaseProductName()); 
        System.out.println("Server version: " + 
      meta.getDatabaseProductVersion()); 
        System.out.println(""); 

        System.out.println("Inserting records into the table..."); 

        PreparedStatement st = con.prepareStatement(
         "INSERT INTO TrailerLocation (Trailer, Block, Location, Day, SetTime, Comment) " + 
         "VALUES (?, ?, ?, NOW(), NOW(), ?)"); 

         st.setString(1, TrailerName); 
         st.setString(2, BlockName); 
         st.setString(3, LocationName); 
         st.setString(4, text); 
         st.addBatch(); 

         st.executeBatch(); 

         JOptionPane.showMessageDialog(null,"Inserted record into the table: " 
           + "\nTrailer : " + TrailerName 
           + "\nBlock : " + BlockName 
           + "\nLocation : " + LocationName 
           + "\nComment : " + text); 

         }catch(SQLException se){ 
         //Handle errors for JDBC 
         se.printStackTrace(); 
         }catch(Exception e1){ 
         //Handle errors for Class.forName 
         e1.printStackTrace(); 
         }finally{ 
         //finally block used to close resources 
         try{ 
          if(st!=null) 
          con.close(); 
         }catch(SQLException se){ 
         }// do nothing 
         try{ 
         if(con!=null) 
         con.close(); 
         }catch(SQLException se){ 
         se.printStackTrace(); 
         }//end finally try 
        }//end try 
         System.out.println("Goodbye!"); 
        }} 
       }); 

    add(TrailerList, BorderLayout.WEST); 
    add(BlockList, BorderLayout.CENTER); 
    add(LocationList, BorderLayout.EAST); 
    add(buttonSave,BorderLayout.SOUTH); 
    add(textField, BorderLayout.NORTH); 
    setBorder(BorderFactory.createEmptyBorder(20,20,20,20)); 
} 

private static void createAndShowGUI() { 
    //Create and set up the window. 
    JFrame frame = new JFrame("Save"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setLocationRelativeTo(null); 
    frame.setUndecorated(true); 


    //Create and set up the content pane. 
    JComponent newContentPane = new Save(); 
    newContentPane.setOpaque(true); //content panes must be opaque 
    frame.setContentPane(newContentPane); 

    //Display the window. 
    frame.pack(); 
    frame.setVisible(true); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
} 

public static void main(String[] args) { 
    //Schedule a job for the event-dispatching thread: 
    //creating and showing this application's GUI. 
    javax.swing.SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      createAndShowGUI(); 
      } 
     }); 
    } 
} 
  1. 爲我創造我有一個文本字段爲人們添加評論,但問題是註釋,以節省您需要按下回車鍵形式是有辦法做到這一點不按下輸入按鈕?

  2. 試圖文件,偵聽器和動作偵聽器,但不能得到代碼工作

+0

使用「保存」按鈕,收集所有信息並將其保存在一步 – MadProgrammer

+0

窗體確實有一個保存按鈕,但不能識別文本字段中的內容,除非我按下輸入,我想要避免 –

+0

那麼如何你知道用戶何時完成打字嗎? – MadProgrammer

回答

0

簡單的解決方法是,刪除的變量「文本」的使用和直接使用textField.getText(),但更新文本區域仍然需要使用DocumentListener。

例如:

st.setString(4, textField.getText()); 

但是在設計角度來看,這應該改進,我的建議是使用一個模型來維持GUI數據狀態, 可以使用beansbinding到GUI組件與模型同步。 一旦用戶點擊保存按鈕,那麼該模型應該提交給其他一些類來處理保存功能。

相關問題