1

这是我的代码。重绘在这里没有任何作用。有任何想法吗?因为初始化后下拉列表中没有项目,所以下拉列表很窄。添加文本项后,宽度保持不变。

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

import java.io.*;

//user is prompted to enter a semi-colon delimitted text string

public class ChoiceWidth extends Applet implements ActionListener, ItemListener {

    private Choice m_Choice = null;
    private Button m_Button = null;
    private TextField m_Textbox = null;     

    public void init() {
        m_Button = new Button("Add Items");
        m_Button.addActionListener(this);
        add(m_Button);

        m_Choice = new Choice();
        m_Choice.addItemListener(this);
        add(m_Choice);

        m_Textbox = new TextField(40);
        add(m_Textbox);
    }

   public void itemStateChanged(ItemEvent e)
   {
       String clr=m_Choice.getSelectedItem();
       System.out.println("Selected: " + clr);
   }


    public void actionPerformed(ActionEvent e){
        AddItems();
    }

    private void CreateList(String items) {
    String devlist[] = items.split(";");
        for(int i = 0; i < devlist.length; ++i) {
        m_Choice.add(devlist[i]);
        }
        m_Choice.repaint();  //I thought this would resize width - but doesn't
    }

    private boolean AddItems() {
        String devs = m_Textbox.getText();
        if(!devs.isEmpty()) {
        CreateList(devs);
        }
        return true;
    }
}

和 html

<html>
<body>
  Enter a semi-colon delimited text string
  <applet code = "ChoiceWidth.class" width="300" height = "60">
</body>
</html>
4

0 回答 0