Java问题,Button改成JButton后应该如何修改呢
以前发过的一个帖子:http://www.ibm.com/developerworks/cn/java/j-java-test/importjava.awt.*;impo...
以前发过的一个帖子: http://www.ibm.com/developerworks/cn/java/j-java-test/
import java.awt.*;
import java.awt.event.*;
public class GenerateEventQueue extends Frame implements ActionListener{
Button button1 = new Button();
TextField textField1 = new TextField();
public GenerateEventQueue() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
GenerateEventQueue generateEventQueue = new GenerateEventQueue();
}
private void jbInit() throws Exception {
button1.setLabel("button1");
button1.addActionListener(this) ;
textField1.setText("textField1");
this.add(button1, BorderLayout.SOUTH);
this.add(textField1, BorderLayout.CENTER);
EventQueue eq=getToolkit().getSystemEventQueue() ;
eq.postEvent(new ActionEvent(button1,ActionEvent.ACTION_PERFORMED,"test" )) ;
addWindowListener(new WinListener());
setBounds(100,100,300,200);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
textField1.setText("event is :"+e.getActionCommand()) ;
}
}
class WinListener extends WindowAdapter{
public void windowClosing(WindowEvent we){
System.exit(0) ;
}
}
把“Button button1 =new Button();"
改为"JButton button1=new JButton()"
eq.postEvent(new ActionEvent(button1,ActionEvent.ACTION_PERFORMED,"test"));
就不起作用了
要如何修改呢? 展开
import java.awt.*;
import java.awt.event.*;
public class GenerateEventQueue extends Frame implements ActionListener{
Button button1 = new Button();
TextField textField1 = new TextField();
public GenerateEventQueue() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
GenerateEventQueue generateEventQueue = new GenerateEventQueue();
}
private void jbInit() throws Exception {
button1.setLabel("button1");
button1.addActionListener(this) ;
textField1.setText("textField1");
this.add(button1, BorderLayout.SOUTH);
this.add(textField1, BorderLayout.CENTER);
EventQueue eq=getToolkit().getSystemEventQueue() ;
eq.postEvent(new ActionEvent(button1,ActionEvent.ACTION_PERFORMED,"test" )) ;
addWindowListener(new WinListener());
setBounds(100,100,300,200);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
textField1.setText("event is :"+e.getActionCommand()) ;
}
}
class WinListener extends WindowAdapter{
public void windowClosing(WindowEvent we){
System.exit(0) ;
}
}
把“Button button1 =new Button();"
改为"JButton button1=new JButton()"
eq.postEvent(new ActionEvent(button1,ActionEvent.ACTION_PERFORMED,"test"));
就不起作用了
要如何修改呢? 展开
8个回答
展开全部
这样写就可以了,你改了没有把jbutton的包引进来import java.awt.*;
import java.awt.event.*;
import javax.swing.JButton;
public class GenerateEventQueue extends Frame implements ActionListener{
// Button button1 = new Button();
JButton button1=new JButton();
TextField textField1 = new TextField();
public GenerateEventQueue() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
GenerateEventQueue generateEventQueue = new GenerateEventQueue();
}
private void jbInit() throws Exception {
button1.setLabel("button1");
button1.addActionListener(this) ;
textField1.setText("textField1");
this.add(button1, BorderLayout.SOUTH);
this.add(textField1, BorderLayout.CENTER);
EventQueue eq=getToolkit().getSystemEventQueue() ;
eq.postEvent(new ActionEvent(button1,ActionEvent.ACTION_PERFORMED,"test" )) ;
addWindowListener(new WinListener());
setBounds(100,100,300,200);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
textField1.setText("event is :"+e.getActionCommand()) ;
}
}
class WinListener extends WindowAdapter{
public void windowClosing(WindowEvent we){
System.exit(0) ;
}
}
import java.awt.event.*;
import javax.swing.JButton;
public class GenerateEventQueue extends Frame implements ActionListener{
// Button button1 = new Button();
JButton button1=new JButton();
TextField textField1 = new TextField();
public GenerateEventQueue() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
GenerateEventQueue generateEventQueue = new GenerateEventQueue();
}
private void jbInit() throws Exception {
button1.setLabel("button1");
button1.addActionListener(this) ;
textField1.setText("textField1");
this.add(button1, BorderLayout.SOUTH);
this.add(textField1, BorderLayout.CENTER);
EventQueue eq=getToolkit().getSystemEventQueue() ;
eq.postEvent(new ActionEvent(button1,ActionEvent.ACTION_PERFORMED,"test" )) ;
addWindowListener(new WinListener());
setBounds(100,100,300,200);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
textField1.setText("event is :"+e.getActionCommand()) ;
}
}
class WinListener extends WindowAdapter{
public void windowClosing(WindowEvent we){
System.exit(0) ;
}
}
更多追问追答
追问
不是指代码出错的问题,例如导入javax.swing.*;啦等(当然,JButton修改后其它地方也要作相应的修改)。
我指的是
eq.postEvent(new ActionEvent(button1,ActionEvent.ACTION_PERFORMED,"test"));
不起作用了,如何修改才能使这句话起作用。或者有类似的“发射事件执行GUI程序”的办法吗?
追答
原来的时候起作用吗?我怎么运行的时候他们的效果都是一样的?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
个人认为,JButton和Button分别属于不同的界面开发包,Swing和AWT,你用的是AWT,自然Swing的JButton控件装不到AWT的面板上,这两包的不同点很多,到这里看一下吧
http://blog.csdn.net/shenlongenjoyjava/article/details/5455288
http://blog.csdn.net/shenlongenjoyjava/article/details/5455288
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你改成JButton了是不是要相应把你继承的Frame改成JFrame?
好久没有java做界面了,你试试。
好久没有java做界面了,你试试。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
对JButton实例调用setPreferredSize(new Dimension(25, 25));方法把25,25换成你想要的宽度和高度。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
工具栏里面有个项目 然后找清理 让系统重构下路径试试!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询