鼠标点击,没有反应。在线急等!!!

理想哥 2004-09-20 02:56:48
想在frame上,任意处点击后,出现一个按钮,但实现不了!然直接在frame上加,没有问题,我现在将jbutton的相关语句剪贴到鼠标触发函数中,再运行后,在frame上点击没有反应?以下是我的完成小程序,大家可以试一下,帮忙解决!!!
package sw2;
import javax.swing.UIManager;
import java.awt.*;

public class Application2 {
boolean packFrame = false;

//Construct the application
public Application2() {
Frame2 frame = new Frame2();
//Validate frames that have preset sizes
//Pack frames that have useful preferred size info, e.g. from their layout
if (packFrame) {
frame.pack();
}
else {
frame.validate();
}
//Center the window
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
frame.setVisible(true);
}

//Main method
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e) {
e.printStackTrace();
}
new Application2();
}
}
------------------------------------------------------------------------------------------
package sw2;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;

public class Frame2 extends JFrame {
JPanel contentPane;
XYLayout xYLayout1 = new XYLayout();

//Construct the frame
public Frame2() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}

//Component initialization
private void jbInit() throws Exception {
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(xYLayout1);
this.setSize(new Dimension(400, 300));
this.setTitle("Frame Title");
contentPane.addMouseListener(new Frame2_contentPane_mouseAdapter(this));
}

//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}

void contentPane_mouseClicked(MouseEvent e) {
JButton jButton1 = new JButton();
contentPane.add(jButton1, new XYConstraints(98, 57, 186, 40));
}
}

class Frame2_contentPane_mouseAdapter extends java.awt.event.MouseAdapter {
Frame2 adaptee;

Frame2_contentPane_mouseAdapter(Frame2 adaptee) {
this.adaptee = adaptee;
}
public void mouseClicked(MouseEvent e) {
adaptee.contentPane_mouseClicked(e);
}
}
...全文
128 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
yxlleslie 2004-09-20
  • 打赏
  • 举报
回复
那就好,^_^
理想哥 2004-09-20
  • 打赏
  • 举报
回复
你说的没错!我试过了,非常感谢!
yxlleslie 2004-09-20
  • 打赏
  • 举报
回复
为什么不看一下我说的是否能好用? 抗议!
chy2swl 2004-09-20
  • 打赏
  • 举报
回复
关键是没有时间
up一下吧
理想哥 2004-09-20
  • 打赏
  • 举报
回复
嘿,各位不能见死不救呀!不会的也帮忙顶呀,顶者有分!
yxlleslie 2004-09-20
  • 打赏
  • 举报
回复
我试验了一下,我在contentPane.add(jButton1, new XYConstraints(98, 57, 186, 40));
之后加了一句contentPane.updateUI();然后就又反应了,你看一下好用否?
不过我记得一般不用这种方法来实现,因为我也有很多问题在等别人回答,所以就不帮你研究了。
理想哥 2004-09-20
  • 打赏
  • 举报
回复
嘿,各位不能见死不救呀!不会的也帮忙顶呀,顶者有分!

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧