2个回答
展开全部
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Test extends JFrame{
private JFrame jframe;
private JButton b1;
private JLabel jlabel ;
private int count=0;
public Test(){
jframe=new JFrame();
jframe.setLayout(null);
jframe.setTitle("测试");
jframe.setBounds(300, 200, 300, 200);
jlabel = new JLabel("测试标签");
jlabel.setBounds(120, 50, 80, 30);
b1=new JButton("点击");
b1.setBounds(100, 100, 80, 30);
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
//这是根据随机生成的数字0、1设置标签jlabel的文字
// int choice=(int)(Math.random()*2);
// if(choice==0)
// jlabel.setText("你好");
// else if(choice==1)
// jlabel.setText("李明");
//点击一次改变一次标签文字
count++;
if(count%2==0)
jlabel.setText("你好");
else if(count%2==1)
jlabel.setText("李明");
}
});
jframe.add(jlabel);
jframe.add(b1);
jframe.setVisible(true);
jframe.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Test();
}
}
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Test extends JFrame{
private JFrame jframe;
private JButton b1;
private JLabel jlabel ;
private int count=0;
public Test(){
jframe=new JFrame();
jframe.setLayout(null);
jframe.setTitle("测试");
jframe.setBounds(300, 200, 300, 200);
jlabel = new JLabel("测试标签");
jlabel.setBounds(120, 50, 80, 30);
b1=new JButton("点击");
b1.setBounds(100, 100, 80, 30);
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
//这是根据随机生成的数字0、1设置标签jlabel的文字
// int choice=(int)(Math.random()*2);
// if(choice==0)
// jlabel.setText("你好");
// else if(choice==1)
// jlabel.setText("李明");
//点击一次改变一次标签文字
count++;
if(count%2==0)
jlabel.setText("你好");
else if(count%2==1)
jlabel.setText("李明");
}
});
jframe.add(jlabel);
jframe.add(b1);
jframe.setVisible(true);
jframe.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Test();
}
}
追问
辛苦你了
展开全部
比较简单:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test28 extends JFrame implements ActionListener {
JButton btn;
JLabel lbl;
public Test28(){
super("测试窗体");
btn = new JButton("按钮");
lbl = new JLabel("李明");
this.setLayout(new FlowLayout());
this.add(lbl);
this.add(btn);
btn.addActionListener(this);
this.setSize(200, 200);
this.setVisible(true);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
public static void main(String[] args) {
new Test28();
}
@Override
public void actionPerformed(ActionEvent e) {
JButton btn = (JButton)e.getSource();
if(btn.getText().equals("按钮")){
if(lbl.getText().equals("李明")){
lbl.setText("你好");
}
else{
lbl.setText("李明");
}
}
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test28 extends JFrame implements ActionListener {
JButton btn;
JLabel lbl;
public Test28(){
super("测试窗体");
btn = new JButton("按钮");
lbl = new JLabel("李明");
this.setLayout(new FlowLayout());
this.add(lbl);
this.add(btn);
btn.addActionListener(this);
this.setSize(200, 200);
this.setVisible(true);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
public static void main(String[] args) {
new Test28();
}
@Override
public void actionPerformed(ActionEvent e) {
JButton btn = (JButton)e.getSource();
if(btn.getText().equals("按钮")){
if(lbl.getText().equals("李明")){
lbl.setText("你好");
}
else{
lbl.setText("李明");
}
}
}
}
追问
谢谢您的回答,辛苦你了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询