看板java
标 题[问题]执行绘图後,按钮不见了
发信站铭传大学资工神话 (Tue Apr 4 19:01:19 2006)
转信站ptt!ctu-reader!ctu-gate!news.nctu!news.ncu!news.csie.mcu!MYTH
原先我有放置一个按钮,但执行时,按钮却在画面上不见了
需要乱点才会出现,请问要如何才能够克服这个问题?
===程式码===
package package_mm01_load_image_test;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.lang.*;
import java.util.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2006</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class Frame1_mm01_image_test extends JFrame {
JPanel contentPane;
JFileChooser jFileChooser1 = new JFileChooser();
JButton jButton1 = new JButton();
Graphics g;//
//Construct the frame
public Frame1_mm01_image_test() {
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(null);
this.setSize(new Dimension(400, 300));
this.setTitle("FrameTitle_mm01_load_image_test");
jFileChooser1.setBounds(new Rectangle(358, 267, 14, 20));
jButton1.setBackground(Color.orange);
jButton1.setBounds(new Rectangle(12, 201, 370, 57));
jButton1.setDebugGraphicsOptions(0);
jButton1.setDoubleBuffered(true);
jButton1.setActionCommand("jButton1");
jButton1.setFocusPainted(true);
jButton1.setText("jButton1");
jButton1.addActionListener(new Frame1_mm01_image_test_jButton1_actionAdapter(this));
contentPane.add(jFileChooser1, null);
contentPane.add(jButton1, 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);
}
}
//绘图的程式码
public void paint(Graphics g)
{
if(btn1_click==true){
Toolkit tok = Toolkit.getDefaultToolkit();
Image img = tok.getImage(file_path);
g.drawImage(img, 20 , 40, 100, 100,this);
}//end of if
}///end of public void paint(Graphics g)
public String file_path;
boolean btn1_click=false;//开档的按钮是否被按了
void jButton1_actionPerformed(ActionEvent e) { //按下按钮後,会汇入图档
if (JFileChooser.APPROVE_OPTION == jFileChooser1.showOpenDialog(this)) {
btn1_click=true;
file_path=jFileChooser1.getSelectedFile().getPath();
}//end Of if
Graphics g=getGraphics();
paint(g);
}
}
class Frame1_mm01_image_test_jButton1_actionAdapter implements java.awt.event.ActionListener {
Frame1_mm01_image_test adaptee;
Frame1_mm01_image_test_jButton1_actionAdapter(Frame1_mm01_image_test adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}
--
╔═══╗╭────
bbs.csie.mcu.edu.tw ───────────╮
推荐给您~
║ 神话 ║│
第一个全面 IPv6 Ready 的 BBS,请大家莅临捧场! │
●
╚═══╝│
From: 61.231.236.14 │
▔*>
﹊ ╰──────────────────────────╯
||