很抱歉打扰你,但我已经因为这个问题而被困3天了。
单击开始按钮后,我需要将一个类加载到初始帧中。问题是我永远无法完成它。
我猜问题是在actionListener方法....但我可以解决它。
这是针对学校项目的,教师不允许我们使用任何Swing
或任何Jcomponent
。
谁能给我一点帮助?
第一帧的代码是
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Frame extends Applet {
private Button v;
private Button f;
private Image lo;
Pergun per = new Pergun();
Pergun p;
public void init() {
setSize(400, 550);
Font myFont = new Font("Arial", Font.BOLD, 25);
v = new Button("INICIAR");
v.setFont(myFont);
v.setBounds((400 / 2) - 35, (550 / 2) - 10, 100, 30);
setLayout(null);
add(v);
v.addActionListener(new Exit());
f = new Button("FECHAR");
f.setVisible(true);
f.setBounds(10, 500, 60, 30);
f.addActionListener(new Ex());
add(f);
}
public void start() {
p = new Pergun();
Thread funcao = new Thread();
funcao.start();
}
public void paint(Graphics g) {
g.setColor(Color.BLUE);
g.fillRect(0, 0, 400, 550);
}
class Exit implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (v instanceof Button) {
per.start();
per.setVisible(true);
}
}
}
class Ex implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (f instanceof Button) {
System.exit(0);
}
}
}
}
我要加载到第一帧的第二个类是这个
import java.applet.*;
import java.awt.*;
public class Pergun extends Applet {
public void update(){
}
public void init () {
setBackground(Color.RED);
Label i, ii, iii;
i = new Label("Indique o número de lâmpadas.");
ii = new Label("Têm todas da mesma potência?");
iii = new Label("Indique a Potência:");
i.setBounds(10, 10, 200, 20);
ii.setBounds(10, 50, 200, 20);
iii.setBounds(10, 100, 200, 20);
setLayout(null);
add(i);
add(ii);
add(iii);
Checkbox w15, w25, w40, w60, w75, w100, w150, wout;
w15 = new Checkbox("15W");
w25 = new Checkbox("25W");
w40 = new Checkbox("40W");
w60 = new Checkbox("60W");
w75 = new Checkbox("75W");
w100 = new Checkbox("100W");
w150 = new Checkbox("150W");
wout = new Checkbox("Outra?");
w15.setBounds(20, 120, 50, 50);
w25.setBounds(70, 120, 50, 50);
w40.setBounds(120, 120, 50, 50);
w60.setBounds(170, 120, 50, 50);
w75.setBounds(220, 120, 50, 50);
w100.setBounds(270, 120, 50, 50);
w150.setBounds(320, 120, 50, 50);
wout.setBounds(20, 155, 55, 50);
add(w15);
add(w25);
add(w40);
add(w60);
add(w75);
add(w100);
add(w150);
add(wout);
TextField num;
num = new TextField();
num.setBounds(250, 10, 100, 20);
add(num);
Choice sn;
sn = new Choice();
sn.addItem("");
sn.addItem("Sim");
sn.addItem("Não");
sn.setBounds(250, 50, 100, 20);
add(sn);
sn.getBackground();
}
public void paint(Graphics g) {
setSize(400, 550);
g.setColor(Color.RED);
g.fillRect(0, 0, 400, 550);
}
}
谢谢你的时间。