0

I have created an applet with two choice controls. One of the choice control is the OS name and the other is the browser name. I have added the item listeners for both these choice controls, so that we can detect the option selected for each of the choice control. The code adds another OS entry ("Windows 10")in the OS choice control if the browser selected in Firefox.

Somehow when I select the browser as firefox , multiple entries of "Windows 10' are added in the OS choice control.

Can someone please help me?

SingleClick

package p1;

import java.applet.Applet;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

public class SingleClick implements ItemListener {

    Applet currentApplet;

    @Override
    public void itemStateChanged(ItemEvent arg0) {
        // TODO Auto-generated method stub

        this.currentApplet.repaint();

    }

    public void appletLink(Applet test) {
        this.currentApplet = test;
    }

}

OSBrowserSelector

package p1;

import java.applet.Applet;
import java.awt.Button;
import java.awt.Checkbox;
import java.awt.CheckboxGroup;
import java.awt.Choice;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.GraphicsEnvironment;
import java.awt.Label;
import java.awt.List;
import java.awt.event.ItemListener;

public class OSBrowserSelector extends Applet {

    Choice os;
    Choice browser;

    public void init() {

        os = new Choice();
        browser = new Choice();
        // Set size of the applet window
        this.setSize(500, 500);

        // Add the operating systems
        os.add("XP");
        os.add("Windows 7");
        os.add("Macos");

        //Add the browsers
        browser.add("Chrome");
        browser.add("firefox");
        browser.add("ie");

        //Make the controls visible
        this.add(os);
        this.add(browser);

        // Set the listener for the selection
        //SingleClick class implements the ItemListener interface
        SingleClick l = new SingleClick();
        l.appletLink(this);

        // Add the listener
        os.addItemListener(l);
        browser.addItemListener(l);
    }

    public void start() {

    }

    public void paint(Graphics g) {

        //If the firefox is selected as the browser , add another entry in the os for "Windows 10"
        if (browser.getSelectedItem().equalsIgnoreCase("firefox")) {
            this.os.add("Windows 10");
        }

    }

}
4

1 回答 1

1
public void paint(Graphics g) {

    //If the firefox is selected as the browser , add another entry in the os for "Windows 10"
    if (browser.getSelectedItem().equalsIgnoreCase("firefox")) {
        this.os.add("Windows 10");
    }

}

只要 JRE 认为有必要,就会调用 Paint。它可能由(我们的代码)更改小程序中的组件(即添加或删除组件)或更改字段中显示的值来触发。它可能通过最小化和恢复浏览器来触发。它可能是通过在浏览器前面显示另一个窗口然后将其关闭来触发的。

做这些事情最好的地方是在init()方法中,它只被调用一次。


顺便说一句 - 您知道以下方法(这使得操作系统“选择器”变得不必要)?

System.getProperty("os.name");

您实际上想从这些用户选择中获得什么?

于 2014-12-24T03:21:50.870 回答