2012-06-14 96 views
2

我想要做的是這個, 當我輸入的細節,它將驗證如果textFiled爲空時按下按鈕,如果它是空的它將顯示一條消息這樣說。 然後它將移動到下一個文本文件類似於許多基於web的註冊表格, 我試圖找出的是爲什麼不會改變消息?錯誤與邏輯或重新繪製/重新驗證Java JFrame

將此代碼粘貼到ecilpse文件中並運行它應顯示簡單框架和我正在嘗試執行的操作。

當名字字段爲空時,該消息顯示在幀的底部, 任何人都可以解釋爲什麼當名字字段包含文本並且中間名不包含文本時它不顯示下一條消息?

大部分邏輯位於代碼的底部。

import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.Toolkit; 
import javax.swing.JFrame; 
import javax.swing.JMenuBar; 
import javax.swing.JMenu; 
import javax.swing.JMenuItem; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 
import java.awt.Color; 
import javax.swing.UIManager; 
import javax.swing.JPanel; 
import com.jgoodies.forms.layout.FormLayout; 
import com.jgoodies.forms.layout.ColumnSpec; 
import com.jgoodies.forms.layout.RowSpec; 
import com.jgoodies.forms.factories.FormFactory; 
import javax.swing.JLabel; 
import javax.swing.JTextField; 
import java.awt.Font; 
import javax.swing.SwingConstants; 
import javax.swing.JRadioButton; 
import java.awt.event.ItemListener; 
import java.awt.event.ItemEvent; 
import javax.swing.JButton; 
import javax.swing.JComboBox; 

public class start { 

private JFrame frame; 
private JTextField tfFirstname; 
private JTextField tfMiddlenames; 
private JTextField tfSurname; 

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

/** 
* Create the application. 
*/ 
public start() { 
    initialize(); 
} 

/** 
* Initialize the contents of the frame. 
*/ 
private void initialize() { 
    frame = new JFrame(); 

    frame.setBounds(100, 100, 505, 429); 

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 


    frame.getContentPane().setLayout(null); 

    final JPanel panelClientNew = new JPanel(); 
    panelClientNew.setBackground(new Color(0, 102, 255)); 
    panelClientNew.setBounds(10, 11, 469, 299); 
    frame.getContentPane().add(panelClientNew); 
    panelClientNew.setLayout(null); 

    JLabel lblFirstname = new JLabel("Firstname :"); 
    lblFirstname.setHorizontalAlignment(SwingConstants.RIGHT); 
    lblFirstname.setVerticalAlignment(SwingConstants.BOTTOM); 
    lblFirstname.setForeground(new Color(255, 255, 255)); 
    lblFirstname.setFont(new Font("Tahoma", Font.BOLD, 13)); 
    lblFirstname.setBounds(10, 16, 163, 14); 
    panelClientNew.add(lblFirstname); 

    tfFirstname = new JTextField(); 
    tfFirstname.setFont(new Font("Tahoma", Font.PLAIN, 13)); 
    tfFirstname.setBounds(177, 10, 282, 27); 
    panelClientNew.add(tfFirstname); 
    tfFirstname.setColumns(10); 

    JLabel lblMiddlenames = new JLabel("Middlenames :"); 
    lblMiddlenames.setHorizontalAlignment(SwingConstants.RIGHT); 
    lblMiddlenames.setForeground(new Color(255, 255, 255)); 
    lblMiddlenames.setFont(new Font("Tahoma", Font.BOLD, 13)); 
    lblMiddlenames.setBounds(10, 47, 163, 14); 
    panelClientNew.add(lblMiddlenames); 

    tfMiddlenames = new JTextField(); 
    tfMiddlenames.setFont(new Font("Tahoma", Font.PLAIN, 13)); 
    tfMiddlenames.setBounds(177, 41, 282, 27); 
    panelClientNew.add(tfMiddlenames); 
    tfMiddlenames.setColumns(10); 

    JLabel lblSurname = new JLabel("Surname :"); 
    lblSurname.setHorizontalAlignment(SwingConstants.RIGHT); 
    lblSurname.setForeground(new Color(255, 255, 255)); 
    lblSurname.setFont(new Font("Tahoma", Font.BOLD, 13)); 
    lblSurname.setBounds(10, 78, 163, 14); 
    panelClientNew.add(lblSurname); 

    tfSurname = new JTextField(); 
    tfSurname.setFont(new Font("Tahoma", Font.PLAIN, 13)); 
    tfSurname.setBounds(177, 72, 282, 27); 
    panelClientNew.add(tfSurname); 
    tfSurname.setColumns(10); 



    JButton btnAdd = new JButton("Add"); 
    btnAdd.addMouseListener(new MouseAdapter() { 
     @Override 
     public void mousePressed(MouseEvent arg0) { 
      /* 
      * 
      * 
      * 
      *I am trying to create a message that validates on certain circumstances 
      * 
      * 
      * 
      */ 

      if(tfFirstname.getText().equals(null) || tfFirstname.getText().equals("") || tfFirstname.getText().equals(false)) { 
       JPanel panelMessage = new JPanel(); 
       panelMessage.setBackground(new Color(30, 144, 255)); 
       panelMessage.setBounds(10, 321, 469, 29); 
       frame.getContentPane().add(panelMessage); 

       JLabel lblPersonSaved = new JLabel("Please Enter Firstname :"); 
       lblPersonSaved.setForeground(new Color(255, 255, 255)); 
       lblPersonSaved.setFont(new Font("Tahoma", Font.BOLD, 15)); 
       panelMessage.add(lblPersonSaved); 
       frame.revalidate(); 
       panelMessage.revalidate(); 
       frame.repaint(); 

      } 
      else if (tfMiddlenames.getText().equals(null) || tfMiddlenames.getText().equals("") || tfMiddlenames.getText().equals(false)) { 
       JPanel panelMessage = new JPanel(); 
       panelMessage.setBackground(new Color(30, 144, 255)); 
       panelMessage.setBounds(10, 321, 469, 29); 
       frame.getContentPane().add(panelMessage); 

       JLabel lblPersonSaved = new JLabel("Please Enter Middlenames :"); 
       lblPersonSaved.setForeground(new Color(255, 255, 255)); 
       lblPersonSaved.setFont(new Font("Tahoma", Font.BOLD, 15)); 
       panelMessage.add(lblPersonSaved); 

       frame.revalidate(); 
       panelMessage.revalidate(); 
       frame.repaint(); 

      } 
      else if (tfSurname.getText().equals(null) || tfSurname.getText().equals("") || tfSurname.getText().equals(false)) { 
       JPanel panelMessage = new JPanel(); 
       panelMessage.setBackground(new Color(30, 144, 255)); 
       panelMessage.setBounds(10, 321, 469, 29); 
       frame.getContentPane().add(panelMessage); 

       JLabel lblPersonSaved = new JLabel("Please Enter Surname :"); 
       lblPersonSaved.setForeground(new Color(255, 255, 255)); 
       lblPersonSaved.setFont(new Font("Tahoma", Font.BOLD, 15)); 
       panelMessage.add(lblPersonSaved); 
       frame.revalidate(); 
       panelMessage.revalidate(); 
       frame.repaint(); 

      } 
      else { 


       //Validation has passed 

      } 
     } 
    }); 
    btnAdd.setBounds(370, 265, 89, 23); 
    panelClientNew.add(btnAdd); 





} 
} 
+2

如果JTextField是空的,不應該將焦點保持與JTextField中使得誤差可以是固定的?順便說一句,您需要閱讀並學習使用AWT/Swing佈局管理器,而不是此「setBounds(...)」業務。它看起來可能不是這樣,但長期的絕對定位會讓你的工作變得更加困難。 –

+0

是的,先生,這是我的目標,因爲我認爲這將使數據驗證更安全,如果我阻止進入下一個文本字段,如果以前的文本字段無效。我也認爲,從長遠來看,它會讓它更加用戶友好,我對擺動GUI編程還很新,並且還沒有探索除基礎之外的所有功能。 – Aaron

+0

考慮爲您的JTextFields使用InputVerifier。 –

回答

4

我建議你使用InputVerifier,因爲這將驗證JTextField的內容是正確的(即要定義這個任何方式),讓您在之前HFOE同意甚至離開JTextField。現在它不會阻止你按其他JButtons和其他的東西,所以如果你有一個提交按鈕,你需要採取其他的預防措施。它檢查以查看是否JTextField是空的簡單InputVerifier的一個例子如下所示:

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

@SuppressWarnings("serial") 
public class InputVerifierExample extends JPanel { 
    public static final Color WARNING_COLOR = Color.red; 
    private JTextField firstNameField = new JTextField(10); 
    private JTextField middleNameField = new JTextField(10); 
    private JTextField lastNameField = new JTextField(10); 
    private JTextField[] nameFields = { 
     firstNameField, 
     middleNameField, 
     lastNameField }; 
    private JLabel warningLabel = new JLabel(" "); 

    public InputVerifierExample() { 
     warningLabel.setOpaque(true); 

     JPanel namePanel = new JPanel(); 
     namePanel.add(new JLabel("Name:")); 
     MyInputVerifier verifier = new MyInputVerifier(); 
     for (JTextField field : nameFields) { 
     field.setInputVerifier(verifier); 
     namePanel.add(field); 
     } 
     namePanel.add(new JButton(new SubmitBtnAction())); 

     setLayout(new BorderLayout()); 
     add(namePanel, BorderLayout.CENTER); 
     add(warningLabel, BorderLayout.SOUTH); 
    } 

    private class SubmitBtnAction extends AbstractAction { 
     public SubmitBtnAction() { 
     super("Submit"); 
     } 

     @Override 
     public void actionPerformed(ActionEvent e) { 
     // first check all fields aren't empty 
     for (JTextField field : nameFields) { 
      if (field.getText().trim().isEmpty()) { 
       return; // return if empty 
      } 
     } 
     String name = ""; 
     for (JTextField field : nameFields) { 
      name += field.getText() + " "; 
      field.setText(""); 
     } 
     name = name.trim(); 
     JOptionPane.showMessageDialog(InputVerifierExample.this, name, "Name Entered", 
       JOptionPane.INFORMATION_MESSAGE); 
     } 
    } 

    private class MyInputVerifier extends InputVerifier { 

     @Override 
     public boolean verify(JComponent input) { 
     JTextField field = (JTextField) input; 
     if (field.getText().trim().isEmpty()) { 
      warningLabel.setText("Please do not leave this field empty"); 
      warningLabel.setBackground(WARNING_COLOR); 
      return false; 
     } 
     warningLabel.setText(""); 
     warningLabel.setBackground(null); 
     return true; 
     } 

    } 

    private static void createAndShowGui() { 
     JFrame frame = new JFrame("InputVerifier Example"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().add(new InputVerifierExample()); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      createAndShowGui(); 
     } 
     }); 
    } 
} 
+0

失敗警告+1。 – trashgod

3
  1. 具有看看DocumentListener

  2. 上START_UP只啓用第一JTextField,如果有的話(由你)字符被輸入到第一JTextField中,然後啓用第二的JTextField,等等...,

    如果你想過濾,更改或替換輸出
  3. 從鍵盤附帶的使用DocumentFilter

  4. change background for example to Color.red(來自DocumentListeners events),在JTextFields之一包含不正確長度的情況下,

  5. LayoutManagers

+0

我不同意這種建議,因爲DocumentFilter用於在顯示文本組件之前檢查添加到文本組件的文本。我仍然認爲InputVerifier會更好。我將以一個例子爲例... –

+0

我不同意InputVerifier,文檔是JTextComponent的模型,對於真正的代碼,你必須寫兩次驗證(當然可以使用InputVerifier)DocumentFilter可以從InputMask的枚舉中獲取InputMask,很抱歉,也許我會用NavigationsFilter和文檔+1一起打擾偉大代碼 – mKorbel

+0

使用InputVerifier比DocumentFilter更容易。 1+的建議作爲DocumentFilters是有用的工具,並將有助於OP瞭解。 –