简体   繁体   English

在Applet中单击按钮可打开另一个Applet Java

[英]Button click in Applet opens another Applet Java

How can I use ActionListener to open another applet in my eclipse package by pressing a button? 如何通过按下按钮使用ActionListener打开我的eclipse包中的另一个applet?

is it setVisible(true) or something else. 是setVisible(true)还是其他东西。 I already did this; 我已经做到了;

report.addActionListener(this);

and implemented it but what do I put in the method? 并实现了它,但是我要在方法中添加什么呢?

public void actionPerformed(ActionEvent arg0) {
}

Here's the context if you want: 如果需要,这里是上下文:

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

public class Test extends Applet implements ActionListener {

Image img;
Image image;
Button security = new Button("Security Controls");
Button activity = new Button("Activity Logs");
Button report =  new Button("Employee Report");
Button reportc = new Button(" Company Report");
Button loginout = new Button("Log out");
Button bug = new Button("Report a bug");

Button about = new Button("About");
Button filejk = new Button("File");
Button help = new Button("Help");

Button inbox = new Button("Inbox");
Button insta = new Button("Quick View");


Button access = new Button("Employee Record");
Button accessc = new Button("Company Sales");
Button manager = new Button("Alert Manager");
Button alerte = new Button("Alert Employee");
Button reward = new Button("Reward System");
Label answerr = new Label("Breaking News!");
TextArea answers = new TextArea("John has acquired Reward Level 3.\n"
        + "Mary is having dinner with the CEO.\n" + "Company has increased sales by          45% this month.", 4, 30,
        TextArea.SCROLLBARS_NONE);

public void init() {
    setSize(750, 600);
    Frame c = (Frame) this.getParent().getParent();
    c.setTitle("HiroshimaBlock");
    img = getImage(getDocumentBase(), "dancer2.gif");
    image = getImage(getDocumentBase(), "hsblock.png");
    add(security);
    add(activity);
    add(report);
    add(loginout);
    add(bug);
    add(about);
    add(reportc);
    add(filejk);
    add(help);
    add(access);
    add(accessc);
    add(manager);
    add(alerte);
    add(reward);
    add(inbox);
    add(insta);
    add(answerr);
    answers.setEditable(false);
    add(answers);
    report.addActionListener(this);

}

public void update(Graphics g) {
    g.drawImage(img, 250, 80, 260,200, this);
    g.drawImage(image, 120,10, this);


    this.security.setLocation(25, 200);
    this.security.setSize(120, 25);
    this.activity.setLocation(25, 250);
    this.activity.setSize(120,25);
    this.report.setLocation(25,300);
    this.report.setSize(120,25);
    this.loginout.setLocation(25,500);
    this.loginout.setSize(120,25);
    this.reportc.setLocation(25,350);
    this.reportc.setSize(120,25);
    this.bug.setLocation(25, 400);
    this.bug.setSize(120,25);
    this.filejk.setLocation(0,0);
    this.filejk.setSize(40,25);
    this.about.setLocation(40,0);
    this.about.setSize(40,25);
    this.help.setLocation(80,0);
    this.help.setSize(40,25);
    this.access.setLocation(595,200);
    this.access.setSize(120,25);

    this.accessc.setLocation(595,250);
    this.accessc.setSize(120,25);
    this.alerte.setLocation(595,300);
    this.alerte.setSize(120,25);
    this.manager.setLocation(595,350);
    this.manager.setSize(120,25);
    this.reward.setLocation(595,400);
    this.reward.setSize(120,25);
    this.inbox.setLocation(300,300);
    this.inbox.setSize(150,75); 
    this.insta.setLocation(325,390);
    this.insta.setSize(100, 50);
    this.answers.setLocation(265,525);
    this.answerr.setLocation(263, 500);
    g.setColor(Color.red);
    Graphics2D g2 = (Graphics2D)g;
    g2.setStroke(new BasicStroke(2));
    g.drawLine(0, 180, 200, 180);
    g.drawLine(200, 180, 200, 1100);
    g.drawLine(545, 180, 800,180);
    g.drawLine(545, 180, 545, 1100);
}

public void paint(Graphics g) {

    update(g);
}


@Override
public void actionPerformed(ActionEvent arg0) {
}
}

Here's the weird error: 这是奇怪的错误:

Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
at Test.actionPerformed(Test.java:126)
at java.awt.Button.processActionEvent(Unknown Source)
at java.awt.Button.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

You have to add the following 您必须添加以下内容

addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                //enter the code here that will launch your new window or 
                //whatever you like
            }

        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM