首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >这个JApplet有问题,不确定为什么它不能工作

这个JApplet有问题,不确定为什么它不能工作
EN

Stack Overflow用户
提问于 2016-07-27 06:22:56
回答 2查看 52关注 0票数 1

我可以运行这个小程序,但它不会显示任何JApplet组件,小程序不会显示标签或文本字段,希望我的if/else状态是正确的。

代码语言:javascript
复制
package JavaPractice;

/* Dominic Spucches
Exercise 7-2
This program will compare 2 applets
*/

import java.awt.*;
import java.awt.event.AWTEventListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public abstract class ex7_2 extends JApplet implements ActionListener {

    private static final long serialVersionUID = 1L;
        JLabel st1 = new JLabel("Enter a string: ");
        JTextField str1 = new JTextField();
        JLabel st2 = new JLabel("Enter a string: ");
        JTextField str2 = new JTextField();
        JLabel same1 = new JLabel();
        JLabel same2 = new JLabel();
        JLabel results = new JLabel();
        FlowLayout flow = new FlowLayout();
        Container c;


    public void init()
    {

        c = getContentPane();
        c.setLayout(flow);
        c.setBackground(Color.gray);
        st1.setForeground(Color.blue);
        c.add(st1);
        str1.setForeground(Color.blue);
        c.add(str1);
        st2.setForeground(Color.blue);
        c.add(st2);
        str2.setForeground(Color.blue);
        c.add(str2);
        str2.addActionListener(this);       
        same1.setForeground(Color.blue);
        c.add(same1);       
        same2.setForeground(Color.blue);
        c.add(same2);
        results.setForeground(Color.blue);
        c.add(results);




    }

    public void actionPerformed(ActionEvent e)
    {
        String str1, str2;

        if (str1.equals(str2))  // s1 == s2
            same1.setText("Same string");
        else if (str1.equalsIgnoreCase(str2))
            same2.setText("Same string - different case");
        else if (str1.compareTo(str2) > 0)  // s1 > s2
            results.setText(str1 + " is alphabetically greater than "
                    + str2);
        else        // s1 < s2
            results.setText(str1 + " is alphabetically less than "
                    + str2);
        results.setText("Difference is " + (str1.compareTo(str2)) /*i keep getting an error here as well in eclipse, no clue */
    }

}
EN

回答 2

Stack Overflow用户

发布于 2016-07-27 06:33:34

从类声明中删除abstract关键字,以便可以将其实例化

代码语言:javascript
复制
public abstract class ex7_2 extends JApplet implements ActionListener {
       ^ 
票数 2
EN

Stack Overflow用户

发布于 2016-07-27 07:23:18

您的示例包含以下几点观察结果:

  • 如图所示,abstract类不能直接实例化。
  • actionPerformed()中,本地字符串应设置为相应的文本字段值。
  • 请注意下面actionPerformed()中修订的逻辑。使用组件初始化来建立初始semicolon.
  • Note检查了下一步检查的相关小程序问题,上一个错误源于缺少.

。另请参阅

代码语言:javascript
复制
//<applet code="ex7_2.class" width=500 height=100></applet>
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class ex7_2 extends JApplet implements ActionListener {

    JLabel st1 = new JLabel("Enter a string: ");
    JTextField str1 = new JTextField(8);
    JLabel st2 = new JLabel("Enter a string: ");
    JTextField str2 = new JTextField(8);
    JLabel equals =  new JLabel("        ");
    JLabel compare = new JLabel("        ");
    JLabel results = new JLabel("        ");
    FlowLayout flow = new FlowLayout(FlowLayout.LEFT, 8, 8);

    @Override
    public void init() {
        Container c = getContentPane();
        c.setLayout(flow);
        c.setBackground(Color.lightGray);
        st1.setForeground(Color.blue);
        c.add(st1);
        str1.setForeground(Color.blue);
        c.add(str1);
        st2.setForeground(Color.blue);
        c.add(st2);
        str2.setForeground(Color.blue);
        c.add(str2);
        str2.addActionListener(this);
        equals.setForeground(Color.blue);
        c.add(equals);
        compare.setForeground(Color.blue);
        c.add(compare);
        results.setForeground(Color.blue);
        c.add(results);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String s1 = str1.getText();
        String s2 = str2.getText();
        if (s1.equals(s2)) {
            equals.setText("Same strings.");
        } else if (s1.equalsIgnoreCase(s2)) {
            equals.setText("Same strings, different case.");
        } else {
            equals.setText("Different strings.");
        }
        if (s1.compareTo(s2) > 0) {
            compare.setText(s1 + " is alphabetically greater than " + s2 + ".");
        } else {
            compare.setText(s1 + " is alphabetically less than " + s2 + ".");
        }
        results.setText("Difference is " + (s1.compareTo(s2) + "."));
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38600819

复制
相关文章

相似问题

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