import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyJFram extends JFrame implements ActionListener{
private JLabel lblMsg;
private JButton btnClick;
private JButton btnClick1;
public MyJFram() {
lblMsg = new JLabel("Are you happy today?");
btnClick = new JButton("happy");
btnClick1 = new JButton("sad");
Container cpMe = getContentPane();
cpMe.setLayout(new FlowLayout(FlowLayout.LEFT));
cpMe.add(lblMsg, BorderLayout.CENTER);
cpMe.setBounds(200,200,500,500);
cpMe.add(btnClick);
cpMe.add(btnClick1);
setTitle("520");
btnClick.addActionListener(this);
btnClick1.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 400);
setResizable(true);
setVisible(true);
}
public void actionPerformed(ActionEvent ae) {
Object a1 = ae.getSource();
if (a1 == btnClick) {
lblMsg.setText("节日快乐");
} else if (a1 == btnClick1) {
lblMsg.setText("不准快乐");
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
MyJFram mf=new MyJFram();
new MyJFram();
}
}
2、
public class TestMyJFram {
public static void main(String[] args) {
// TODO Auto-generated method stub
MyJFram mf=new MyJFram();
new MyJFram();
}
}