本文介绍了在小程序点击按钮打开另一个小程序的Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用的ActionListener 由$ P $在我的日食包打开另一个小程序pssing一个按钮?

是调用setVisible(true),还是别的东西。我已经做了这一点;

  report.addActionListener(本);

和实施,但什么才是我放在方法?

 公共无效的actionPerformed(ActionEvent的为arg0){
}

这里的背景下,如果你想:

 进口java.applet中的*。
进口java.awt中的*。
java.awt.event中导入*。
进口的java.net.URL;公共类测试扩展的Applet实现的ActionListener {图片IMG;
形象画像;
按钮安全性=新按钮(安全控件);
按钮活动=新按钮(活动日志);
巴顿报告=新按钮(员工报告);
按钮reportc =新按钮(「本公司报告);
按钮loginout =新按钮(退出);
按钮错误=新的Button(报告错误);按钮关于=新按钮(关于);
按钮filejk =新按钮(文件);
按钮帮助=新按钮(帮助);按钮的收件箱=新按钮(收件箱);
按钮斯塔=新按钮(快速查看);
按钮访问=新按钮(员工记录);
按钮accessc =新按钮(「本公司销售部);
按钮经理=新按钮(报警管理器);
按钮alerte =新按钮(警报雇员);
按钮奖励=新按钮(奖励制度);
标签answerr =新标签(突发新闻!);
文本区答案=新的TextArea(约翰已经获得奖励3级。\\ n
        +玛丽吃晚饭的CEO。\\ n+公司已按45%的本月销量大增。,4,30,
        TextArea.SCROLLBARS_NONE);公共无效的init(){
    的setSize(750,600);
    架C =(帧)this.getParent()的getParent()。
    c.setTitle(HiroshimaBlock);
    IMG =的getImage(getDocumentBase(),dancer2.gif);
    图像=的getImage(getDocumentBase(),hsblock.png);
    加(安全性);
    加(活动);
    增加(报告);
    加(loginout);
    加(BUG);
    加(约);
    加(reportc);
    加(filejk);
    加(帮助);
    加入(访问);
    加(accessc);
    加(经理);
    加(alerte);
    加(奖励);
    加(收件箱);
    加(章安);
    加(answerr);
    answers.setEditable(假);
    加(答案);
    report.addActionListener(本);}公共无效更新(图形G){
    g.drawImage(IMG,250,80,260200,本);
    g.drawImage(图像,120,10,这一点);
    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(595200);
    this.access.setSize(120,25);    this.accessc.setLocation(595250);
    this.accessc.setSize(120,25);
    this.alerte.setLocation(595300);
    this.alerte.setSize(120,25);
    this.manager.setLocation(595350);
    this.manager.setSize(120,25);
    this.reward.setLocation(595400);
    this.reward.setSize(120,25);
    this.inbox.setLocation(300,300);
    this.inbox.setSize(150,75);
    this.insta.setLocation(325390);
    this.insta.setSize(100,50);
    this.answers.setLocation(265525);
    this.answerr.setLocation(263,500);
    g.setColor(Color.red);
    Graphics2D的G2 =(Graphics2D的)克;
    g2.setStroke(新的BasicStroke(2));
    g.drawLine(0,180,200,180);
    g.drawLine(200,180,200,1100);
    g.drawLine(545,180,800180);
    g.drawLine(545,180,545,1100);
}公共无效漆(图形G){    更新(G);
}
@覆盖
公共无效的actionPerformed(ActionEvent的为arg0){
}
}

下面是奇怪的错误:

 异常螺纹AWT-EventQueue的-1显示java.lang.NullPointerException
在Test.actionPerformed(Test.java:126)
在java.awt.Button.processActionEvent(来源不明)
在java.awt.Button.processEvent(来源不明)
在java.awt.Component.dispatchEventImpl(来源不明)
在java.awt.Component.dispatchEvent(来源不明)
在java.awt.EventQueue.dispatchEventImpl(来源不明)
在java.awt.EventQueue.access $ 200(来源不明)
在java.awt.EventQueue中的$ 3.run(来源不明)
在java.awt.EventQueue中的$ 3.run(来源不明)
在java.security.AccessController.doPrivileged(本机方法)
在java.security.ProtectionDomain $ 1.doIntersectionPrivilege(​​来源不明)
在java.security.ProtectionDomain $ 1.doIntersectionPrivilege(​​来源不明)
在java.awt.EventQueue中的$ 4.run(来源不明)
在java.awt.EventQueue中的$ 4.run(来源不明)
在java.security.AccessController.doPrivileged(本机方法)
在java.security.ProtectionDomain $ 1.doIntersectionPrivilege(​​来源不明)
在java.awt.EventQueue.dispatchEvent(来源不明)
在java.awt.EventDispatchThread.pumpOneEventForFilters(来源不明)
在java.awt.EventDispatchThread.pumpEventsForFilter(来源不明)
在java.awt.EventDispatchThread.pumpEventsForHierarchy(来源不明)
在java.awt.EventDispatchThread.pumpEvents(来源不明)
在java.awt.EventDispatchThread.pumpEvents(来源不明)
在java.awt.EventDispatchThread.run(来源不明)


解决方案

您必须添加以下

  addActionListener方法(新的ActionListener(){            公共无效的actionPerformed(ActionEvent的为arg0){
                // TODO自动生成方法存根
                //输入code在这里,将启动新窗口或
                //随你喜欢
            }        });

How can I use ActionListener to open another applet in my eclipse package by pressing a button?

is it setVisible(true) or something else. 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
            }

        });

这篇关于在小程序点击按钮打开另一个小程序的Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 23:13