请编写一个jframe,其中包含一个jlabel和五个jbutton,五个jbutton分别实现功能
展开全部
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Demo{
private JFrame frame;
private JButton bt1, bt2, bt3, bt4, bt5;
private JLabel jl;
Demo() {
frame = new JFrame();
frame.setBounds(200, 200, 400, 300);
frame.setVisible(true);
bt1 = new JButton("1");
bt2 = new JButton("2");
bt3 = new JButton("3");
bt4 = new JButton("4");
bt5 = new JButton("5");
jl = new JLabel();
frame.setLayout(new FlowLayout());
frame.add(jl);
frame.add(bt1);
frame.add(bt2);
frame.add(bt3);
frame.add(bt4);
frame.add(bt5);
bt1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
jl.setText("1");
}
});
bt2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
jl.setText("2");
}
});
bt3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
jl.setText("3");
}
});
bt4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
jl.setText("4");
}
});
bt5.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
jl.setText("");
}
});
}
public static void main(String[] args) {
new Demo();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询