首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >GUI程序不能作为小程序运行

GUI程序不能作为小程序运行
EN

Stack Overflow用户
提问于 2014-12-23 10:38:18
回答 1查看 72关注 0票数 0

在我的在线Java类中,我有这样的代码:

代码语言:javascript
复制
/* This is option 1 of my Java final project. This is a program which displays a frame with a button that, when you click it, increases the number below it. */

import java.awt.*; // imports everything I need for this project
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

public class option1 { // This is where I define most of my stuff. Technical restrictions prevent me from doing this in the class where the program is itself executed (see comment towards the end).

    public static void main(String[] args) {

        final Frame mainFrame = new OptionOne(); // First I define my frame, button, and labels ...
        Button button = new Button("Button");
        final Label label1a = new Label();
        final Label label1b = new Label();
        final Label label1c = new Label();
        final Label label2 = new Label();

        label1a.setBounds(15, 30, 270, 10); // ... and set their bounds and the labels' text. Labels 1a, 1b, 1c give instructions, while label2 displays the number.
        label1a.setText("This is a program where you click a button");
        label1b.setBounds(15, 45, 270, 10);
        label1b.setText("and the number below it increases each time");
        label1c.setBounds(15, 60, 270, 10);
        label1c.setText("you click. To start, just click the button.");
        label2.setBounds(150, 220, 50, 30);
        label2.setText("0");
        button.setBounds(100, 100, 50, 50);

/* After this point, things get a little more complicated. Technical restrictions on usage of variables accross classes mean that I had to make some unusual code decisions, as you will see. */

        label2.addPropertyChangeListener(label2.getText(), new PropertyChangeListener() { // This part is used to detect a change in the appearance of label2, and to react appropriately to it.
            @Override // All the "@Override"s are here because it gives a compiling error if I don't put them.
            public void propertyChange(PropertyChangeEvent evt) {

            }
        });

        button.addMouseListener(new MouseListener() { // This part tells the program what to do when you click the button.
            @Override // the only @Override that isn't here for technical reasons (see earlier comment at first @Override) 
            public void mouseClicked(MouseEvent e) {
                int value = Integer.parseInt(label2.getText());
                label2.setText(String.valueOf(value + 1));  
            }

            @Override
            public void mousePressed(MouseEvent e) {

            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {

            }

            @Override
            public void mouseExited(MouseEvent e) {

            }
        });

        mainFrame.add(label1a); // Now that the labels and button are fully defined, we are now ready to add them.
        mainFrame.add(label1b);
        mainFrame.add(label1c);
        mainFrame.add(label2);
        mainFrame.add(button);
    }
}

class OptionOne extends Frame { // technical restrictions prevent me from putting most of my code in this class. in this code I define the actual frame itself, for its final use.

    OptionOne() {
        setTitle("Final Project Option 1");
        setSize(300, 300);
    show();
    }
}

我需要把它作为applet来运行。直接将其放入HTML代码,如下所示

代码语言:javascript
复制
<head></head>
<body>
<applet code="OptionOne.class" width="300" height="300">
</applet>
</body>

给我一个"java.lang.reflect.invocationtargetexception“错误。为什么我会得到这个错误?我知道错误一定出在HTML代码中,因为程序在放入编译器时运行良好,那么我到底做错了什么呢?

编辑:现在我将代码更改为:

代码语言:javascript
复制
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

public class option1main extends Panel {

    public static void main(String[] args) {

        Button button = new Button("Button");
        final Label label1a = new Label();
        final Label label1b = new Label();
        final Label label1c = new Label();
        final Label label2 = new Label();

        label1a.setBounds(15, 30, 270, 10);
        label1a.setText("This is a program where you click a button");
        label1b.setBounds(15, 45, 270, 10);
        label1b.setText("and the number below it increases each time");
        label1c.setBounds(15, 65, 270, 10);
        label1c.setText("you click. To start, just click the button.");
        label2.setBounds(150, 220, 50, 30);
        label2.setText("0");
        button.setBounds(100, 100, 50, 50);

        label2.addPropertyChangeListener(label2.getText(), new PropertyChangeListener() {
            @Override // All the "@Override"s are here because it gives a compiling error if I don't put them.
            public void propertyChange(PropertyChangeEvent evt) {

            }
        });

        button.addMouseListener(new MouseListener() {

            @Override
            public void mouseClicked(MouseEvent e) {
                int value = Integer.parseInt(label2.getText());
                label2.setText(String.valueOf(value + 1));  
            }

            @Override
            public void mousePressed(MouseEvent e) {

            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {

            }

            @Override
            public void mouseExited(MouseEvent e) {

            }
        });
    }
}

public class option1 {

    option1main() {
        final Frame mainFrame = new OptionOne();
        mainFrame.add(label1a);
        mainFrame.add(label1b);
        mainFrame.add(label1c);
        mainFrame.add(label2);
        mainFrame.add(button);
    }

}

public class panel1 extends Applet {

    /* init() { - not sure what to add here...? */

}

class OptionOne extends Frame {

    OptionOne() {
        setTitle("Final Project Option 1");
        setSize(300, 300);
    show();
    }
}

在编译它时,在"option1main() {“行上给出了错误”无效的方法声明:需要返回类型“。为什么会这样呢?

EN

回答 1

Stack Overflow用户

发布于 2014-12-23 10:48:14

将你的基本UI移到一个独立的类中,这个类是从你的当前(

  1. option1)类扩展而来的,使用这个类并将它添加到Frame实例中,然后创建一个从Applet扩展而来的新类。在此classes init方法中,创建Panel类的实例(从步骤1开始),并将其添加到applet
  2. Change your <applet code="OptionOne.class" width="300" height="300">以指向您在步骤3中创建的applet类。

附注:

  • 考虑使用Swing或JavaFX,无意冒犯,AWT已经过时15+多年了,大多数人没有(或没有)使用它的经验
  • 考虑使用适当的布局管理器,以便简单地解决跨平台渲染差异问题
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27613331

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档