iT邦幫忙

0

Design View Netbeans Swings 按鈕設計原理是?

Tzu 2021-06-11 19:00:31732 瀏覽
  • 分享至 

  • xImage

各位大大晚安:

我在找https://ithelp.ithome.com.tw/upload/images/20210611/20119035QtvA9S6dTh.png

https://www.youtube.com/watch?v=goaaZ-t1qtI
這個影片居然一打開就有...QQ到底哪裡按的啦~

拉方塊的地方...
老師要我們改程式碼...一定要用他的拉...QQ
/images/emoticon/emoticon02.gif

老師原來的程式碼:


package gui;



import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Sample4 extends JFrame
{
   private JPanel pn;
   private JLabel lb;
   private JTextArea ta;
   private JScrollPane sp;
   private JButton bt1, bt2;

   public static void main(String[] args)
   {
      Sample4 sm = new Sample4();
   }
  public Sample4()
   {
      // 設定標題
      super("範例-Read/Write File");

      // 建立元件
      lb = new JLabel("請選擇檔案。");
      ta = new JTextArea();
      sp = new JScrollPane(ta);

      pn = new JPanel();
      bt1 = new JButton("讀取");
      bt2 = new JButton("儲存");

      // 新增到容器中
      pn.add(bt1);
      pn.add(bt2);

      add(lb, BorderLayout.NORTH);
      add(sp, BorderLayout.CENTER);
      add(pn, BorderLayout.SOUTH);

      // 登錄傾聽者
      bt1.addActionListener(new SampleActionListener());
      bt2.addActionListener(new SampleActionListener());
      addWindowListener(new SampleWindowListener());

      // 設定框架
      setSize(300, 300);
      setVisible(true);
  }

   // 傾聽者類別
   class SampleActionListener implements ActionListener
   {
      public void actionPerformed(ActionEvent e)
      {
         Container cnt = getContentPane();
         JFileChooser fc = new JFileChooser();
         try{
            if(e.getSource() == bt1){
               int res = fc.showOpenDialog(cnt);
               if(res == JFileChooser.APPROVE_OPTION){       
                  File fl = fc.getSelectedFile();
                  BufferedReader br = new BufferedReader(new FileReader(fl));
                  ta.read(br, null);
                  br.close();
               }
            }
            else if(e.getSource() == bt2){
               int res = fc.showSaveDialog(cnt);
               if(res == JFileChooser.APPROVE_OPTION){       
                  File fl = fc.getSelectedFile();
                  BufferedWriter bw = new BufferedWriter(new FileWriter(fl));
                  ta.write(bw);
                  bw.close();
              }
            }
         }
         catch(Exception ex){
            ex.printStackTrace();
         }
      }
   }
   class SampleWindowListener extends WindowAdapter
   {
      public void windowClosing(WindowEvent e)
      {
         System.exit(0);
      }
   }
}

要修改程式碼:

res = fc.showSaveDialog(cnt);
               if(res == JFileChooser.APPROVE_OPTION){       
                  File fl = fc.getSelectedFile();
                  BufferedWriter bw = new BufferedWriter(new FileWriter(fl));
                  ta.write(bw);
                  bw.close();

可是怎麼樣都找不到阿
https://ithelp.ithome.com.tw/upload/images/20210611/20119035ueShmQIO14.png
https://ithelp.ithome.com.tw/upload/images/20210611/20119035CbiAvkktTH.png

老師到底要怎樣....
後來我找到按鈕的地方了

https://ithelp.ithome.com.tw/upload/images/20210612/20119035IudNJUoEGw.png

https://ithelp.ithome.com.tw/upload/images/20210612/20119035IqcFtoJ0Gn.png

https://ithelp.ithome.com.tw/upload/images/20210612/20119035NiSW4ID31I.pnghttps://ithelp.ithome.com.tw/upload/images/20210612/2011903532CrUtyfMC.png

現在是找不到Design View Netbeans Swings 按鈕設計原理是?
老師說不能用程式寫....

要有讀取 儲存 exit 跟瀏覽 功能

你要不要換個IDE編輯器試試?
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友回答

立即登入回答