2013-04-14 113 views
1

我想創建在用戶按下一個按鈕,在文本框中輸入一個單詞一個程序中的字符串相匹配,一旦他們進入他們的文字按下輸入按鈕,他們輸入的單詞將被另一個字符串檢查。我可以得到它來檢查他們輸入的字符串,但我不知道我會怎麼做,所以用戶必須先選擇一個按鈕,然後在文本中輸入,然後按下輸入按鈕。檢查是否在一個文本框的文本一旦按鈕被按下

將會有多個按鈕供用戶選擇,它們將會有圖像,用戶需要寫入這些圖像在文本框中的內容以檢查該單詞是否正確,他們將按下另一個按鈕去檢查。

例如在bagcathouselamp post四個按鈕與圖像用戶選擇一個按鈕,然後他們需要使用文本框拼寫單詞,他們請按Enter檢查在文本框中的文本是否有一定的字符串匹配。

感謝

這裏是我曾嘗試:

public class Textb extends JPanel{ 

JFrame frame =new JFrame(); 
JPanel panel =new JPanel(); 
JButton enter =new JButton("Enter"); 
JButton wordBtn =new JButton("Cat"); 
JTextField tb =new JTextField(); 

public Textb() { 

    // Panel and button layout 

    panel.setLayout(null); 
    panel.setBackground(Color.WHITE); 
    panel.setCursor(new Cursor(Cursor.HAND_CURSOR)); // set the cursor to a hand 

    Insets insets = panel.getInsets(); 

    tb.setVisible(true); 
    tb.setBounds(200 + insets.left, 5 + insets.top, 110,60); 
    tb.setBackground(Color.YELLOW); 


    enter.setLayout(null); 
    enter.setBounds(10 + insets.left, 5 + insets.top, 110,60); 
    enter.setBackground(Color.WHITE); 
    enter.setBorder(BorderFactory.createEmptyBorder()); 
    enter.setFocusPainted(false); 

    wordBtn.setLayout(null); 
    wordBtn.setBounds(10 + insets.left, 70 + insets.top, 110,60); 
    wordBtn.setBackground(Color.WHITE); 
    wordBtn.setBorder(BorderFactory.createEmptyBorder()); 
    wordBtn.setFocusPainted(false); 


    panel.add(tb); 
    panel.add(enter); 
    panel.add(wordBtn); 
    frame.add(panel); 
    frame.setTitle("Matching"); 
    frame.setSize(800, 600); 
    frame.setLocationRelativeTo(null); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setVisible(true); 



    // This is where i did the action listener 
    enter.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent ae) 
     { 
      if(ae.getSource().equals(wordBtn)) 
      { 
       if(tb.getText().equals("cat")){ 
        tb.setText("Correct"); 
       } 
      } 
     } 
    }); 
} 


public static void main(String[] args) { 
    new Textb(); 
} 
} 
+1

究竟是什麼你想,當用戶按下第一個按鈕的發生呢? –

+0

我不確定你在問什麼,你嘗試過JOptionPane.showInputDialog()嗎? –

+0

對不起,但我什麼都不明白,請你簡短明瞭地問你的問題。 –

回答

1

一個簡單flag變量應該工作:

enter image description here

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

public class TestingCat extends JPanel{ 


JFrame frame =new JFrame(); 
JPanel panel =new JPanel(); 
private int flag=0; 



JButton enter =new JButton("Enter"); 
JButton wordBtn =new JButton("Cat"); 

JTextField tb =new JTextField(); 




public TestingCat() { 


// Panel and button layout 

panel.setLayout(null); 
panel.setBackground(Color.WHITE); 
panel.setCursor(new Cursor(Cursor.HAND_CURSOR)); // set the cursor to a hand 

Insets insets = panel.getInsets(); 

tb.setVisible(true); 
tb.setBounds(200 + insets.left, 5 + insets.top, 110,60); 
tb.setBackground(Color.YELLOW); 


enter.setLayout(null); 
enter.setBounds(10 + insets.left, 5 + insets.top, 110,60); 
enter.setBackground(Color.WHITE); 
enter.setBorder(BorderFactory.createEmptyBorder()); 
enter.setFocusPainted(false); 

wordBtn.setLayout(null); 
wordBtn.setBounds(10 + insets.left, 70 + insets.top, 110,60); 
wordBtn.setBackground(Color.WHITE); 
wordBtn.setBorder(BorderFactory.createEmptyBorder()); 
wordBtn.setFocusPainted(false); 


panel.add(tb); 
panel.add(enter); 
panel.add(wordBtn); 
frame.add(panel); 
frame.setTitle("Matching"); 
frame.setSize(800, 600); 
frame.setLocationRelativeTo(null); 
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
frame.setVisible(true); 



// This is where i did the action listener 
wordBtn.addActionListener(new ActionListener(){ 

public void actionPerformed(ActionEvent ae) 

{ 
flag=1; 
} 

}); 

enter.addActionListener(new ActionListener(){ 
public void actionPerformed(ActionEvent ae) 
{ 

JFrame f=new JFrame(); 
if(ae.getSource().equals(enter)) 
    { 
     if(flag==1) 
     { 
      flag=0; 
    if(tb.getText().equals("cat")){ 
      tb.setText("Correct"); 
     } 
    } 
     else 
      JOptionPane.showMessageDialog(f,"enter cat 1st"); 
} 

} 
}); 


} 


public static void main(String[] args) { 
new TestingCat(); 
} 
} 
2

下面是對字符串測試輸入和輸出追加到一個JTextArea的一個簡單的例子。它甚至採用了LayoutManager的東西,你會發現次(至少)比null佈局更加有用。

enter image description here

public class Test { 
    private static String ENTER = "Enter"; 
    static JButton enterButton; 
    public static JTextArea output; 
    public static JTextField input; 
    static JFrame frame; 
    static JPanel panel; 
    public static String testString = "test"; 

    public static void main(String... args) 
    { 
     try 
     { 
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
     } catch (Exception ex) 
     { 
      ex.printStackTrace(); 
     } 
     createFrame(); 
    } 

    public static void createFrame() 
    { 
     frame = new JFrame("Test"); 
     frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
     panel = new JPanel(); 
     panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); 
     panel.setOpaque(true); 
     ButtonListener buttonListener = new ButtonListener(); 
     output = new JTextArea(15, 50); 
     output.setWrapStyleWord(true); 
     output.setEditable(false); 
     JScrollPane scroller = new JScrollPane(output); 
     scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); 
     scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); 
     JPanel inputpanel = new JPanel(); 
     inputpanel.setLayout(new FlowLayout()); 
     input = new JTextField(20); 
     enterButton = new JButton("Enter"); 
     enterButton.setActionCommand(ENTER); 
     enterButton.addActionListener(buttonListener); 
     // enterButton.setEnabled(false); 
     input.setActionCommand(ENTER); 
     input.addActionListener(buttonListener); 
     DefaultCaret caret = (DefaultCaret) output.getCaret(); 
     caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE); 
     panel.add(scroller); 
     inputpanel.add(input); 
     inputpanel.add(enterButton); 
     panel.add(inputpanel); 
     frame.getContentPane().add(BorderLayout.CENTER, panel); 
     frame.pack(); 
     frame.setLocationByPlatform(true); 
     // Center of screen 
     // frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
     frame.setResizable(false); 
     input.requestFocus(); 
    } 

    public static class ButtonListener implements ActionListener 
    { 

     public void actionPerformed(final ActionEvent ev) 
     { 
      Thread thread = new Thread() 
      { 

       public void run() 
       { 
        if (!input.getText().trim().equals("")) 
        { 
         String cmd = ev.getActionCommand(); 
         if (ENTER.equals(cmd)) 
         { 
          output.append(input.getText()); 
          if (input.getText().trim().equals(testString)) output.append(" = " + testString); 
          else output.append(" != " + testString); 
          output.append("\n"); 
         } 
        } 
        input.setText(""); 
        input.requestFocus(); 
       } 
      }; 
      thread.start(); 
     } 
    } 
} 
1

我不知道你想什麼做的正是,但我不認爲你的代碼將工作的這部分...

enter.addActionListener(new ActionListener(){ 
    public void actionPerformed(ActionEvent ae) 
{ 

if(ae.getSource().equals(wordBtn)) 
    { 
    if(tb.getText().equals("cat")){ 
     tb.setText("Correct"); 
} 

} 

} 
}); 

你」重新添加一個ActionListener進入,然後檢查wordBtn是否被點擊。這不是你的內心如果陳述永遠不會運行。

這裏是什麼,我認爲你試圖做一個例子。

public class textb extends JPanel { 

    int counter = 0; 

    public textb() { 
     JButton enter = new JButton("Enter"); 
     JButton wordBtn = new JButton("Cat"); 

     enter.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       counter++; 
      } 
     }); 

     wordBtn.addActionListener(new ActionListener() { 
      if (counter > 0) { 
       //insert your code to check the String here, because the 
       //only way to increment counter is by pressing the enter button 
       //this code will not run unless enter has been pressed at least once 
       //to make counter greater than zero, you could also use a boolean 
       //set it to true when enter is pressed and check to see if its true 
       //instead of checking if counter is greater than zero 
      } 
     }); 
    } 
} 
+0

我想要的是,如果輸入按鈕已被按下,wordBtn也被按下,那麼它會檢查字符串是否匹配。 – Rachel

+0

我的例子回答了你的問題嗎? –

+0

我現在已經做到了我想做的事情,謝謝你給出了答案。 – Rachel

相關問題