我可以运行此applet,但它不会显示任何JApplet
组件,applet不会显示标签或文本字段,希望我的if / else状态是正确的。
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 :(得分:2)
从类声明中删除abstract
关键字,以便可以实例化
public abstract class ex7_2 extends JApplet implements ActionListener {
^
答案 1 :(得分:2)
你的例子有几个观察结果:
如图here所示,abstract
类无法直接实例化。
在actionPerformed()
中,应将本地字符串设置为相应的文本字段值。
请注意下面actionPerformed()
中的修订逻辑。
您的上一个错误源于缺少分号。
请注意使用组件初始化来建立初始大小。
考虑今后审核的相关小程序问题here。
另请参阅Initial Threads。
//<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) + "."));
}
}