我一直在尝试将我的Frames变成applet,但我仍然无法让它们在BlueJ中正常运行......我还是新手,我可以在转换工作中进行一些故障排除和帮助吗?提前致谢
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Option1 extends JApplet implements ActionListener {
private int click = 0;
public Option1() {
JFrame base = new JFrame ("Button Click Counter");
base.getContentPane().setLayout(null);
base.setSize(500,500);
base.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button = new JButton("Click Me!");
button.addActionListener(this);
JTextField count = new JTextField(click);
this.add(button);
this.add(count);
}
@Override
public void actionPerformed (ActionEvent e) {
click++; }
}
和另一个
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.Random;
public class Option2 extends JApplet implements MouseListener {
double x;
double y;
public void init() {
addMouseListener(this);
JFrame base = new JFrame("Mouse Coordinates");
base.getContentPane().setLayout(null);
base.setSize(500,500);
base.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextField answer = new JTextField(x + "," + y);
}
@Override
public void mouseClicked( MouseEvent e ) {
x = e.getX();
y = e.getY();
this.setBackground(new Color((int)(Math.random() * 0x1000000)));
}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
}
另外,如果有人可以提供关于如何通过鼠标点击将背景更改为随机颜色的建议(如在Option2中尝试的那样),那就太棒了。谢谢!