全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:1939
推到 Plurk!
推到 Facebook!

開啟圖片問題

答題得分者是:neoart
vespa
一般會員


發表:14
回覆:22
積分:7
註冊:2004-02-13

發送簡訊給我
#1 引用回覆 回覆 發表時間:2004-06-19 10:29:15 IP:163.13.xxx.xxx 未訂閱
以下程式碼可指定路徑在Frame視窗下開啟圖片 麻煩大家幫我看看,為何開啟圖片都得按兩次? 謝謝 < class="code"> import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.awt.image.*; public class open extends JFrame implements ActionListener{ private Image m = null; private String dir = null; JPanel contentPane; JScrollPane jScrollPane1 = new JScrollPane(); static FileDialog dialog; //宣告檔案對話方塊變數 JLabel jLabel1 = new JLabel(); MenuBar menubar1; //宣告功能表列變數 Menu mnuFile; //宣告功能表變數 MenuItem itmOpen1, itmExit; //宣告功能項變數 java.awt.Image iconimg1; java.awt.Image iconimg2; //Construct the frame public open() { 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(500, 400)); //size of JFrame this.setTitle("open"); jScrollPane1.setBounds(new Rectangle(45, 58, 170, 140)); contentPane.add(jScrollPane1, null); jScrollPane1.getViewport().add(jLabel1, null); menubar1 = new MenuBar(); //建立功能表列 mnuFile = new Menu("File"); //建立檔案功能表 menubar1.add(mnuFile); //加入檔案功能表 itmOpen1 = new MenuItem("Open1"); //建立開啟舊檔功能項 itmExit = new MenuItem("Exit"); //建立結束功能項 mnuFile.add(itmOpen1); //加入開啟舊檔到檔案功能表 mnuFile.add(itmExit); //加入結束到檔案功能表 dialog = new FileDialog(this, "開啟舊檔"); //建立對話方塊 itmOpen1.addActionListener(this); itmExit.addActionListener(this);//加入結束功能項動作監聽 setMenuBar(menubar1); //設定視窗功能表列 } //----------------------------------------- public void actionPerformed(ActionEvent e) { //執行視窗Action事件方法 if(e.getSource() == itmOpen1){ dialog.setVisible(true);//顯示對話方塊 loadImage1(); } else if(e.getSource() == itmExit) { //若為結束功能項事件 System.exit(0); //結束並關閉視窗 } } public void loadImage1(){ FileDialog dlg = new FileDialog(this, "Choose Image", FileDialog.LOAD); //set current directory if(dir != null){ dlg.setDirectory(dir); } dlg.setVisible(true); //get image name and path String imgFile = dlg.getDirectory() dlg.getFile(); dir = dlg.getDirectory(); //create image using filename Toolkit tk = Toolkit.getDefaultToolkit(); m = tk.getImage(imgFile); ImageIcon tempIcon1=new ImageIcon(m); iconimg1=tempIcon1.getImage(); jLabel1.setIcon(tempIcon1); } //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); } } } class open_M{ public static void main(String[] args) { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); open _frame = new open(); //Validate frames that have preset sizes //Pack frames that have useful preferred size info, e.g. from their layout _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); } catch(Exception e) { e.printStackTrace(); } } } 發表人 - vespa 於 2004/06/19 11:01:11
neoart
版主


發表:22
回覆:582
積分:425
註冊:2003-05-09

發送簡訊給我
#2 引用回覆 回覆 發表時間:2004-06-19 10:35:10 IP:61.56.xxx.xxx 未訂閱
跟你們說過多少次了?n次了吧,要人debug,總要把程式碼排縮好嘛. 用[ code ].... [ /code] ("[" "code" "]" 與 "[" "/code" "]"不用空白) 知道嗎? 現在說下次已經是n 1次了.
vespa
一般會員


發表:14
回覆:22
積分:7
註冊:2004-02-13

發送簡訊給我
#3 引用回覆 回覆 發表時間:2004-06-19 11:02:19 IP:163.13.xxx.xxx 未訂閱
不好意思 我了解了
neoart
版主


發表:22
回覆:582
積分:425
註冊:2003-05-09

發送簡訊給我
#4 引用回覆 回覆 發表時間:2004-06-19 11:55:53 IP:61.56.xxx.xxx 未訂閱
      public void actionPerformed(ActionEvent e) {        //執行視窗Action事件方法
         if(e.getSource() == itmOpen1){ 
                   //dialog.setVisible(true);//顯示對話方塊<---這一行是多此一舉,你的loadImge1已經有show dialog了.
                   loadImage1();
                   
         }
               else if(e.getSource() == itmExit) {        //若為結束功能項事件
                  System.exit(0);        //結束並關閉視窗
         }
       }    
既然你們已經有jbuilder了,為什麼不用F7 F8 等debug功能呢? 對了,FileDialog建議改用JFileChooser,他可以加上圖片預覽區,你的專案應該用的到的.
vespa
一般會員


發表:14
回覆:22
積分:7
註冊:2004-02-13

發送簡訊給我
#5 引用回覆 回覆 發表時間:2004-06-19 13:32:35 IP:163.13.xxx.xxx 未訂閱
再次說聲謝謝你 你的回應文章對我們的專案開發真的幫助很大 不知道你是從何得知我們使用JBuilder 不過我們目前仍是使用文字編輯器 在命令提示字元下編譯執行 很多資料都是網路上找來的 我想可能是因為這個原因讓你有這種猜測 如果jbuilder有除錯功能的話 我們會試著去熟悉jbuilder的操作環境 另外 謝謝你JFileChooser的建議
系統時間:2024-05-09 3:02:53
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!