2016-10-13 7 views
-1

50個の質問があるプログラムを作成しているので、テキストフィールドに質問を入力してボタンをクリックします。ディレクトリに質問がある場合は、ポップアップに答えが表示されます。今私は、ユーザーがクリックすると、彼の質問と答えを追加することができるボタンを追加したい。質問を追加するのは簡単ですが、答えを追加する方法はわかりません。ここに私のプログラムはランダムな文字列を生成して値を割り当てる方法

class te extends JFrame implements ActionListener,MouseListener,KeyListener { 
    JButton bnt1; 

    JTextField tf; 

    JLabel title; 

    JLabel q1; 

    JLabel ans1; 

    JLabel q1f=new JLabel("Question displayed on jframe"); 

    JButton bnt2; 

    JButton b; 

    JLabel q2; 

    JLabel q3; 

    JButton bnt3; 

    JButton bnt4; 

    JLabel q4; 

    JButton bnt5; 

    JLabel q5; 

    JLabel link; 

    ArrayList<String> items = new ArrayList<String>(); 

    private JButton bu; 

    JTextField tfu; 

    JTextField tfu2; 

    JButton help; 

    String s; 

    String ss; 

     public te() throws Exception { 
     super("IskcoWare"); 

     this.setUndecorated(true); 
     this.setExtendedState(MAXIMIZED_BOTH); 
     tf=new JTextField(); 



     String s; 

     bnt4=new JButton("Ask"); 

     bnt5=new JButton("Ask"); 


     bnt3=new JButton("Ask"); 

     title=new JLabel("50 Glorious Years Of Iskcon"); 
     title.setFont(new Font("",Font.BOLD+Font.ITALIC,70)); 

     q1.setFont(new Font("",Font.BOLD,25)); 

     bnt1=new JButton("Ask"); 
     bnt1.addActionListener(this); 

     b=new JButton("Search"); 
     b.setBounds(700,170,150,40); 
     b.setFont(new Font("",Font.PLAIN,15)); 
     b.setVisible(true); 
     add(b); 

     link=new JLabel("<html><a href=\"Something.java\">Click to add your  own question</a>"); 

     bnt2=new JButton("Ask"); 

     help=new JButton("Help"); 

     add(help); 

     help.setBounds(100,710,100,40); 

     setLayout(null); 

     tf.setBounds(150,170,450,30); 
     tf.setForeground(Color.GRAY); 

     title.setBounds(190,-150,10000,400); 

     q1.setBounds(100,250, 10000, 100); 

     bnt1.setBounds(1000,288,70,35); 
     bnt1.setFont(new Font("",Font.PLAIN,17)); 

     bnt2.setFont(new Font("",Font.PLAIN,17)); 
     bnt2.setBounds(1000,370, 70,35); 

     q2.setBounds(100,180,400,400); 
     q2.setFont(new Font("",Font.BOLD,25)); 

     q3.setBounds(100,270,400,400); 
     q3.setFont(new Font("",Font.BOLD,25)); 

     bnt3.setBounds(1000,460,70,35); 
     bnt3.setFont(new Font("",Font.PLAIN,17)); 

     bnt4.setBounds(1000,550,70,35); 
     bnt4.setFont(new Font("",Font.PLAIN,17)); 

     q4.setBounds(100,360,400,400); 
     q4.setFont(new Font("",Font.BOLD,25)); 

     q5.setBounds(100,450,500,400); 
     q5.setFont(new Font("",Font.BOLD,25)); 

     bnt5.setBounds(1000,640,70,35); 
     bnt5.setFont(new Font("",Font.PLAIN,17)); 

     link.setBounds(1000,550,400,400); 
     link.setFont(new Font("",Font.BOLD + Font.ITALIC,15));; 

     add(link); 

     add(tf); 

     add(title); 

     add(q1); 

     add(bnt1); 

     add(tf); 

     add(b); 

     add(bnt3); 

     add(q3); 

     add(bnt2); 

     add(bnt4); 

     add(q4); 

     add(bnt5); 

     add(q5); 

     bnt5.addActionListener(this); 
     bnt2.addActionListener(this); 
     bnt3.addActionListener(this); 
     bnt4.addActionListener(this); 


      add(q2); 

      setSize(10000,730); 

      setVisible(true); 

      link.addMouseListener(this); 


      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 

      Locale[] locales = Locale.getAvailableLocales(); 

      items.add("Questions"); 
      tf.addMouseListener(this); 
      tf.setSize(400,37); 
      tf.addKeyListener(this); 
      tf.setFont(new Font("",Font.PLAIN,25)); 
      b.addActionListener(new loginButt(tf)); 
      b.addActionListener(this); 
      setupAutoComplete(tf, items); 
      help.addActionListener(this); 
      tf.setColumns(30); 
      tf.setText("hello"); 
} 
     private static boolean isAdjusting(JComboBox cbInput) { 
    cbInput.setFont(new Font("",Font.PLAIN,20)); 
    new JScrollPane(null,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, 
       JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); 

    if (cbInput.getClientProperty("is_adjusting") instanceof Boolean) { 
     return (Boolean) cbInput.getClientProperty("is_adjusting"); 
    } 
    return false; 
} 

    public JScrollPane createScroller() 
    { 
     return new JScrollPane(null,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED, 
       JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 
    } 

private static void setAdjusting(JComboBox cbInput, boolean adjusting) { 
    cbInput.putClientProperty("is_adjusting", adjusting); 
} 

public static void setupAutoComplete(final JTextField tf, final ArrayList<String> items) { 
    final DefaultComboBoxModel model = new DefaultComboBoxModel(); 
    final JComboBox cbInput = new JComboBox(model) 
    // 


    { 

     public Dimension getPreferredSize() { 
      return new Dimension(super.getPreferredSize().width, 0); 
     } 
    }; 
    setAdjusting(cbInput, false); 
    for (String item : items) { 
     model.addElement(item); 
    } 
    cbInput.setSelectedItem(null); 
    cbInput.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent e) { 
      if (!isAdjusting(cbInput)) { 
       if (cbInput.getSelectedItem() != null) { 
        tf.setText(cbInput.getSelectedItem().toString()); 
       } 
      } 
     } 
    }); 

    tf.addKeyListener(new KeyAdapter() { 

     @Override 
     public void keyPressed(KeyEvent e) { 
      setAdjusting(cbInput, true); 
      //bla 
      if (e.getKeyCode() == KeyEvent.VK_ENTER || e.getKeyCode() == KeyEvent.VK_UP || e.getKeyCode() == KeyEvent.VK_DOWN) { 
       e.setSource(cbInput); 
       cbInput.dispatchEvent(e); 
       if (e.getKeyCode() == KeyEvent.VK_ENTER) { 
        tf.setText(cbInput.getSelectedItem().toString()); 
        cbInput.setPopupVisible(false); 
       } 
      } 
      if (e.getKeyCode() == KeyEvent.VK_ESCAPE) { 
       cbInput.setPopupVisible(false); 
      } 
      setAdjusting(cbInput, false); 
     } 
    }); 
    tf.getDocument().addDocumentListener(new DocumentListener() { 
     public void insertUpdate(DocumentEvent e) { 
      updateList(); 
     } 

     public void removeUpdate(DocumentEvent e) { 
      updateList(); 
     } 

     public void changedUpdate(DocumentEvent e) { 
      updateList(); 
     } 

     private void updateList() { 
      setAdjusting(cbInput, true); 
      model.removeAllElements(); 
      String input = tf.getText(); 
      if (!input.isEmpty()) { 
       for (String item : items) { 
        if (item.toLowerCase().startsWith(input.toLowerCase())) { 
         model.addElement(item); 
        } 
       } 
      } 
      cbInput.setPopupVisible(model.getSize() > 0); 
      setAdjusting(cbInput, false); 
     } 
    }); 
    tf.setLayout(new BorderLayout()); 
    tf.add(cbInput, BorderLayout.SOUTH); 
} 

だこれはあなたの問題を簡素化

public class SubmitButton{ 
JTextField Input; 
String s; 

public SubmitButton(JTextField textfield){ 
    Input = textfield; 
} 


public void actionPerformed(ActionEvent e) { 

    String q1 = "questions"; 


       if(Input.getText().equals(q1)) 
       { 
        JOptionPane.showMessageDialog(null,"The answer",  "Answer",JOptionPane.WARNING_MESSAGE); 
       } 
      } 
     } 
+1

具体的に何が問題になっていますか? –

答えて

1

他のクラスです。

サブ問題を解決するためにすべてのSwingコードが必要なわけではありません。

シンプルなバージョンのコマンドラインインターフェイスで試してから、大きなコードベースに折り畳んでください。

コンピュータサイエンスは分解に関するものです。大きな問題を小さなものに分割して解決します。

質問リストの長さの範囲で乱数を生成するように思えます。インデックスを生成し、質問を引き出す。

関連する問題