如何生成随机字符串并为其赋值

时间:2016-10-13 17:46:04

标签: java arrays

我正在制作一个包含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 个答案:

答案 0 :(得分:1)

简化您的问题。

您不需要所有Swing代码来解决您的子问题。

在问题的简单版本上使用命令行界面尝试,然后将其折叠到更大的代码库中。

计算机科学是关于分解的。通过将大问题分解为较小问题来解决大问题。

听起来您想要在问题列表的长度范围内生成随机数。生成索引并提出问题。