0

I am creating a program where the user selects an item from a list, then the user enters a text to a TextField and then they press a button which then alerts the user of which item was selected alongside the text from the user.how do I go about alerting the user of which item was selected alongside the text from the user when the button is clicked.

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.event.*;


public class fruitList extends JFrame implements ListSelectionListener
{
    private JTextField textField;
    private JList<String> fruitList;
    private JButton buttonwhich;
    private static fruitList  frame;

 public static void main(String[] args)
    {

        fruitList  frame = new fruitList();
        frame.setTitle("Fruit List");
        frame.setSize(350,150);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
   }
   public fruitList()
   {

      final String[] FRUIT_NAME = {"Banana", "Apple", "Orange"};

      setLayout(new FlowLayout());
      buttonwhich = new JButton("Which");
      fruitList = new JList<String>(FRUIT_NAME);
      fruitList.setVisibleRowCount(2); 
      add(new JScrollPane(fruitList));
      fruitList.addListSelectionListener(this);

      textField = new JTextField(10);

      add(textField);
      add(buttonwhich);
      ButtonHandler handler = new ButtonHandler();
      buttonwhich.addActionListener(handler);

   }  
   class ButtonHandler implements ActionListener {
        public void actionPerformed(ActionEvent e) {

            JOptionPane.showMessageDialog(frame,
                    "You Pressed \t" + e.getActionCommand());

        }
   }
}
4

2 回答 2

0

鉴于您的 ButtonHandler 是一个内部类,因此您可以访问私有变量,然后 getter 可以在其中检索所选值,如下所示:

JOptionPane.showMessageDialog(frame, "You Pressed \t" + fruitList.getSelectedValue() + ";" + textField.getText());

于 2013-03-09T21:34:58.220 回答
0

我已经修复了导入 java.awt 的问题。; 导入 java.awt.event。;

import javax.swing.*;
import javax.swing.event.*;


public class fruitList extends JFrame
{
    private JTextField textField;
    private JList<String> fruitList;
    private JButton buttonwhich;
    private static fruitList  frame;

 public static void main(String[] args)
    {

        fruitList  frame = new fruitList();
        frame.setTitle("Fruit List");
        frame.setSize(350,150);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
   }
   public fruitList()
   {

      final String[] FRUIT_NAME = {"Banana", "Apple", "Orange"};

      setLayout(new FlowLayout());
      buttonwhich = new JButton("Which");
      fruitList = new JList<String>(FRUIT_NAME);
      fruitList.setVisibleRowCount(2); 
      add(new JScrollPane(fruitList));
     //fruitList.addListSelectionListener(this);

      textField = new JTextField(10);

      add(textField);
      add(buttonwhich);
      ButtonHandler handler = new ButtonHandler();
      buttonwhich.addActionListener(handler);

   }  
   class ButtonHandler implements ActionListener {
       private Object sel;
        public void actionPerformed(ActionEvent e) {

             int[] selectedIx = fruitList.getSelectedIndices();
              for (int i = 0; i < selectedIx.length; i++) {
                  sel = fruitList.getModel().getElementAt(selectedIx[i]);
                }
            JOptionPane.showMessageDialog(frame,
                    "You Pressed \t" + e.getActionCommand()+sel+textField.getText());

        }
   }
}
于 2013-03-09T21:43:08.203 回答