在JCombo Box Java中向字符串添加文本

时间:2018-06-17 22:46:42

标签: java string swing combobox

我正在使用Java进行作业,但我仍然坚持如何执行此操作:完整代码位于

之下

JTextField descripField = new JTextField(20);    //将您想要的电视节目添加到字符串

String [] progs = {};    //将您想要的电视节目描述添加到字符串

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

public class JTVDownload extends JFrame implements ItemListener
{
   JComboBox<String> programBox = new JComboBox<String>();
   JLabel programList = new JLabel("***                  Program List               ***");
   JLabel stars = new JLabel("*****************************");
   JTextField descripField = new JTextField(20);
   // add your desired TV programs to the string


   String[] progs = {               };
   // add your desired TV programs descrips to the string
   String[] descrips = {              };
   public JTVDownload()
   {
      super("JTVDownload");
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setLayout(new FlowLayout());
      programBox.addItemListener(this);
      add(stars);
      add(programList);
      programBox.addItem("none");
      for(int x = 0; x < progs.length; ++x)
         programBox.addItem(progs[x]);
      add(programBox);
      add(descripField);
   }
   public static void main(String[] arguments)
   {
      JTVDownload frame = new JTVDownload();
      frame.setSize(300,150);
      frame.setVisible(true);
   }
   @Override
   public void itemStateChanged(ItemEvent list)
   {
      Object source = list.getSource();
      if(source == programBox)
      {
         int num = programBox.getSelectedIndex();
         descripField.setText(descrips[num]);
      }
   }
}

1 个答案:

答案 0 :(得分:1)

首先progsdescrips需要添加一些文字。问题是,你添加了none,这使得两个列表不同步,可能最好将它包含在你的数组中

import java.awt.FlowLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class JTVDownload extends JFrame implements ItemListener {

    JComboBox<String> programBox = new JComboBox<String>();
    JLabel programList = new JLabel("***                  Program List               ***");
    JLabel stars = new JLabel("*****************************");
    JTextField descripField = new JTextField(20);
    // add your desired TV programs to the string

    String[] progs = {"None", "Happy days", "Here's Lucy", "That 70's Show"};
    // add your desired TV programs descrips to the string
    String[] descrips = {"", "Happy days", "Here's Lucy", "That 70's Show"};

    public JTVDownload() {
        super("JTVDownload");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new FlowLayout());
        programBox.addItemListener(this);
        add(stars);
        add(programList);
        for (int x = 0; x < progs.length; ++x) {
            programBox.addItem(progs[x]);
        }
        add(programBox);
        add(descripField);
    }

    public static void main(String[] arguments) {
        JTVDownload frame = new JTVDownload();
        frame.setSize(300, 150);
        frame.setVisible(true);
    }

    @Override
    public void itemStateChanged(ItemEvent list) {
        Object source = list.getSource();
        if (source == programBox) {
            int num = programBox.getSelectedIndex();
            descripField.setText(descrips[num]);
        }
    }
}

总的来说,更好的解决方案是使用POJO将“名称”和“描述”包装到单个对象中,这样您就可以在一个单独的工作单元中携带信息。这个原因需要您使用ListCellRenderer来自定义对象的呈现方式。在这种情况下,请仔细查看How to Use Combo Boxes以获取更多详细信息