java public void actionPerformed(ActionEvent evt)怎么把这个类用实例化来时间
importjava.awt.event.*;importjavax.swing.*;importjava.awt.*;publicclassaaextendsJFram...
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
public class aa extends JFrame implements ActionListener {//实现ActionListener接口
JButton b1 = new JButton("混蛋");
JButton b2 = new JButton("你好");
public aa() {
super("Title Bar");
b1.addActionListener(this);//设置事件监听
b2.addActionListener(this);//设置事件监听
JPanel pane = new JPanel();
pane.add(b1);
pane.add(b2);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setContentPane(pane);
}
public static void main(String[] arguments) {
JFrame frame = new aa();
frame.pack();
frame.setVisible(true);
}
public void actionPerformed(ActionEvent evt) {//接口ActionListener唯一的方法
Object source = evt.getSource();//返回选取的对象
if (source == b1)
setTitle("你好");//修改标签
else if (source == b2)
setTitle("混蛋");//修改标签
}
}
求大神把这个用实例化来实现 展开
import javax.swing.*;
import java.awt.*;
public class aa extends JFrame implements ActionListener {//实现ActionListener接口
JButton b1 = new JButton("混蛋");
JButton b2 = new JButton("你好");
public aa() {
super("Title Bar");
b1.addActionListener(this);//设置事件监听
b2.addActionListener(this);//设置事件监听
JPanel pane = new JPanel();
pane.add(b1);
pane.add(b2);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setContentPane(pane);
}
public static void main(String[] arguments) {
JFrame frame = new aa();
frame.pack();
frame.setVisible(true);
}
public void actionPerformed(ActionEvent evt) {//接口ActionListener唯一的方法
Object source = evt.getSource();//返回选取的对象
if (source == b1)
setTitle("你好");//修改标签
else if (source == b2)
setTitle("混蛋");//修改标签
}
}
求大神把这个用实例化来实现 展开
1个回答
展开全部
什么玩意乱七八糟的,等着,我给你重新排一下。
先给你一个改按钮文字的。
package lll;
//AA.java
import java.awt.BorderLayout;
import java.awt.event.*;
import javax.swing.*;
@SuppressWarnings("serial")
public class aa extends JFrame{//实现ActionListener接口
JButton b1 = new JButton("混蛋");
JButton b2 = new JButton("你好");
String s=new String("jiangshan");
JFrame x=new JFrame(s);
JPanel pane=new JPanel();
public aa() {
b1.addActionListener(new M1());//设置事件监听
b2.addActionListener(new M2());//设置事件监听
pane.add(b1,BorderLayout.WEST);
pane.add(b2,BorderLayout.CENTER);
x.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
x.setContentPane(pane);
x.pack();
x.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable(){
public void run(){
new aa();
}
});
}}
class M1 implements ActionListener{//M1.java
public void actionPerformed(ActionEvent evt) {
//接口ActionListener唯一的方法
JButton button=(JButton)evt.getSource();//返回选取的对象
String buttonName=evt.getActionCommand();
if (buttonName=="混蛋")
button.setText("你好");//修改标签
else if (buttonName=="你好")
button.setText("混蛋");//修改标签
}
}
class M2 implements ActionListener{//M1.java
public void actionPerformed(ActionEvent evt) {
//接口ActionListener唯一的方法
JButton button=(JButton)evt.getSource();//返回选取的对象
String buttonName=evt.getActionCommand();
if (buttonName=="混蛋")
button.setText("你好");//修改标签
else if (buttonName=="你好")
button.setText("混蛋");//修改标签
}
}
根据这个例子自己改去吧。
import java.awt.BorderLayout;
import java.awt.event.*;
import javax.swing.*;
public class SettitleDemo {
JButton one=new JButton();
JFrame title=new JFrame();
JTextField txtfield=new JTextField();
public SettitleDemo(){
title.getContentPane().add(txtfield,BorderLayout.CENTER);
title.getContentPane().add(one,BorderLayout.SOUTH);
one.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
String str=txtfield.getText();
title.setTitle(str);
}
});
title.pack();
title.setVisible(true);
}
public static void main(String[] args){
new SettitleDemo();
}
}
先给你一个改按钮文字的。
package lll;
//AA.java
import java.awt.BorderLayout;
import java.awt.event.*;
import javax.swing.*;
@SuppressWarnings("serial")
public class aa extends JFrame{//实现ActionListener接口
JButton b1 = new JButton("混蛋");
JButton b2 = new JButton("你好");
String s=new String("jiangshan");
JFrame x=new JFrame(s);
JPanel pane=new JPanel();
public aa() {
b1.addActionListener(new M1());//设置事件监听
b2.addActionListener(new M2());//设置事件监听
pane.add(b1,BorderLayout.WEST);
pane.add(b2,BorderLayout.CENTER);
x.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
x.setContentPane(pane);
x.pack();
x.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable(){
public void run(){
new aa();
}
});
}}
class M1 implements ActionListener{//M1.java
public void actionPerformed(ActionEvent evt) {
//接口ActionListener唯一的方法
JButton button=(JButton)evt.getSource();//返回选取的对象
String buttonName=evt.getActionCommand();
if (buttonName=="混蛋")
button.setText("你好");//修改标签
else if (buttonName=="你好")
button.setText("混蛋");//修改标签
}
}
class M2 implements ActionListener{//M1.java
public void actionPerformed(ActionEvent evt) {
//接口ActionListener唯一的方法
JButton button=(JButton)evt.getSource();//返回选取的对象
String buttonName=evt.getActionCommand();
if (buttonName=="混蛋")
button.setText("你好");//修改标签
else if (buttonName=="你好")
button.setText("混蛋");//修改标签
}
}
根据这个例子自己改去吧。
import java.awt.BorderLayout;
import java.awt.event.*;
import javax.swing.*;
public class SettitleDemo {
JButton one=new JButton();
JFrame title=new JFrame();
JTextField txtfield=new JTextField();
public SettitleDemo(){
title.getContentPane().add(txtfield,BorderLayout.CENTER);
title.getContentPane().add(one,BorderLayout.SOUTH);
one.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
String str=txtfield.getText();
title.setTitle(str);
}
});
title.pack();
title.setVisible(true);
}
public static void main(String[] args){
new SettitleDemo();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询