import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Example extends Applet implements ActionListener { // #1
Button button;
Label label;
public void actionPerformed(ActionEvent e)
{ //#4
Object source = e.getSource();
if (source == button)
{
label.setText(" It is a example !");
}
}
public void init()
{ // #3
button = new Button(" OK ");
button.setBounds(280, 200, 100, 20);
button.addActionListener(this);
//Label=new Label();
label = new Label();
label.setBounds(260, 100, 200, 20);
add(button);
add(label);
}
public static void main(String[] args)
{
Frame win = new Frame("Example");
Example example = new Example();
win.add("enter", example);
example.init(); // #2
win.setSize(600, 360);
win.setVisible(true);
}
}
import java.awt.*;
import java.awt.event.*;
public class Example extends Applet implements ActionListener { // #1
Button button;
Label label;
public void actionPerformed(ActionEvent e)
{ //#4
Object source = e.getSource();
if (source == button)
{
label.setText(" It is a example !");
}
}
public void init()
{ // #3
button = new Button(" OK ");
button.setBounds(280, 200, 100, 20);
button.addActionListener(this);
//Label=new Label();
label = new Label();
label.setBounds(260, 100, 200, 20);
add(button);
add(label);
}
public static void main(String[] args)
{
Frame win = new Frame("Example");
Example example = new Example();
win.add("enter", example);
example.init(); // #2
win.setSize(600, 360);
win.setVisible(true);
}
}
import java.awt.*;
import java.awt.event.*;
public class Example extends Applet implements ActionListener{ // #1
Button button;
Label label; public static void main(String[] args){
Frame win=new Frame("Example");
Example example=new Example();
win.add("enter", example);
example.init(); // #2
win.setSize(600,360);
win.setVisible(true);
} public void init(){ // #3
button=new Button(" OK ");
button.setBounds(280,200,100,20);
button.addActionListener(this);
//Label=new Label();
label=new Label();
label.setBounds(260,100,200,20);
add(button);
add(label);
} public void actionPerformed(ActionEvent e){ //#4
Object source=e.getSource();
if(source == button){
label.setText(" It is a example !");
}
}
}
javac Example.java通过了,但是用java Example时报了一个cannot add to layout :unknow constraint :enter.
2、main里这写
Frame win = new Frame("Example");
win.setLayout(new BorderLayout());
Example example = new Example();
win.add(example, "Center");
example.init(); // #2
win.setSize(600, 360);
win.setVisible(true);
如果你一定要运行main的话
顺利通过了,现在就是点击OK按钮时,Label上没有显示It is a example!.
并且点能最小化和最大化,就是不能点差退出!
在次感谢各位高手!
public void actionPerformed(ActionEvent e){ //#4
Object source=e.getSource();
if(source == button){
label.setText(" It is a example !"); //这里
this.validate();
}
}
win.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
win.setLayout(new BorderLayout());这个也要偶写出来 :(
It is a example !
请在指教!
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Example extends Applet implements ActionListener { // #1
Button button;
Label label;
public void actionPerformed(ActionEvent e)
{ //#4
Object source = e.getSource();
if (source == button)
{
label.setText(" It is a example !");
this.validate();
}
}
public void init()
{ // #3
button = new Button(" OK ");
button.setBounds(280, 200, 100, 20);
button.addActionListener(this);
//Label=new Label();
label = new Label();
label.setBounds(260, 100, 200, 20);
add(button);
add(label);
}
public static void main(String[] args)
{
Frame win = new Frame("Example");
win.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
win.setLayout(new BorderLayout()); Example example = new Example();
win.add(example, "Center");
example.init(); // #2
win.setSize(600, 360);
win.setVisible(true);
}
}
我的程序和你up的一样,但就是Label中的It is a example !显示不出来,
帮我想想,有什么原因,谢谢!