2016-07-26 49 views
1

我可以运行这个小程序,但它不会显示任何JApplet组件,小程序不会显示标签或文本字段,并希望我的if/else状态是正确的。这个JApplet有问题,不确定它为什么不起作用

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 */ 
    } 

} 
+0

一定的[Java控制台(http://www.java.com/en/download/help/javaconsole.xml)配置为显示。如果在默认级别没有输出,请提高级别并再次尝试。 –

回答

2

从类声明删除abstract关键字,因此它可以被实例化

public abstract class ex7_2 extends JApplet implements ActionListener { 
    ^
2

你例如带有若干意见:

  • 如图here,一个abstract类不能被实例化直。

  • actionPerformed()中,应将本地字符串设置为相应的文本字段值。

  • 请注意下面修改后的逻辑actionPerformed()

  • 您的最后一个错误源自缺少分号。

  • 请注意使用组件初始化来建立初始大小。

  • 考虑未来的相关小程序问题,检查here。请参阅Initial Threads

image

//<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) + ".")); 
    } 
} 
相关问题