如何点击一个按钮,画出一个圆?

冬雷软件 2004-09-21 09:39:23
如何点击一个按钮,画出一个圆?请帮忙贴出代码。
...全文
282 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
dlglmf 2004-09-27
  • 打赏
  • 举报
回复
别客气,多讨论
冬雷软件 2004-09-24
  • 打赏
  • 举报
回复
呵,感谢雨魂,我明白了。
dlglmf 2004-09-24
  • 打赏
  • 举报
回复
两个文件放一起,运行哪个总不用说了吧
dlglmf 2004-09-24
  • 打赏
  • 举报
回复
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class TestFrame extends JFrame {
JPanel contentPane;
JButton jbt = new JButton();
JScrollPane jScrollPane1 = new JScrollPane();
JTextArea jta = new JTextArea();

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

//Component initialization
private void jbInit() throws Exception {
contentPane = (JPanel) this.getContentPane();
jbt.setBounds(new Rectangle(99, 22, 83, 28));
jbt.setText("jButton1");
jbt.addActionListener(new TestFrame_jbt_actionAdapter(this));
contentPane.setLayout(null);
this.setSize(new Dimension(400, 300));
this.setTitle("Frame Title");
jScrollPane1.setBounds(new Rectangle(25, 61, 341, 198));
jta.setText("jTextArea1");
contentPane.add(jbt, null);
contentPane.add(jScrollPane1, null);
jScrollPane1.getViewport().add(jta, null);
}

//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 jbt_actionPerformed(ActionEvent e) {
// jta.setText("");
Graphics g=jta.getGraphics();
g.drawString("dlg",50,50);
g.drawOval(30,30,100,100);
}
}

class TestFrame_jbt_actionAdapter implements java.awt.event.ActionListener {
TestFrame adaptee;

TestFrame_jbt_actionAdapter(TestFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jbt_actionPerformed(e);
}
}
dlglmf 2004-09-24
  • 打赏
  • 举报
回复
public class TestApp {
boolean packFrame = false;

//Construct the application
public TestApp() {
TestFrame frame = new TestFrame();
//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 TestApp();
}
}
冬雷软件 2004-09-22
  • 打赏
  • 举报
回复
我的目的不在于这,请帮忙贴段代码好吗?画直线也可。
giant216 2004-09-22
  • 打赏
  • 举报
回复
你要掌握按钮的事件机制,然后就是java2D的作图,其实就是点击按钮调用画圆的函数而已,在Swing和Applet中还有jsp页面中都可以实现。
冬雷软件 2004-09-22
  • 打赏
  • 举报
回复
为什么没人帮我贴代码呢?
dlglmf 2004-09-22
  • 打赏
  • 举报
回复
要自己动手才能学好的,
首先你要学会事件机制,
然后在jpanel或者其他的组件上,像jbutton,jtextarea啊,都可以,画任意图形,不过首先你要得到哪个组件的graphics才行

50,528

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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