第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將JFileChooser與Swing GUI類和偵聽器一起使用

將JFileChooser與Swing GUI類和偵聽器一起使用

慕斯709654 2019-11-19 11:10:08
這是我當前的菜單:public class DrawPolygons{    public static void main (String[] args) throws FileNotFoundException    {        /**         *   Menu - file reader option         */        JMenuBar menuBar;        JMenu menu;        JMenuItem menuItem;        //  Create the menu bar.        menuBar = new JMenuBar();        //  Build the first menu.        menu = new JMenu("File");        menu.setMnemonic(KeyEvent.VK_F);        menu.getAccessibleContext().setAccessibleDescription("I have items");        menuBar.add(menu);        //  a group of JMenuItems        menuItem = new JMenuItem("Load",KeyEvent.VK_T);        menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_1, ActionEvent.ALT_MASK));        menuItem.getAccessibleContext().setAccessibleDescription("Load your old polygons");        menu.add(menuItem);        menuItem = new JMenuItem("Save",KeyEvent.VK_U);        menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_2, ActionEvent.ALT_MASK));        menuItem.getAccessibleContext().setAccessibleDescription("Save the contents of your polygons");        menu.add(menuItem);        // attaching the menu to the frame        JFrame frame = new JFrame("Draw polygons");        frame.setJMenuBar(menuBar);        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        frame.setContentPane(new DrawingPanel());        frame.pack();        frame.setVisible(true);    }}它有兩個選項Load和Save。 在此處輸入圖片說明現(xiàn)在,我如何將附加JFileChooser到該actionPerformed方法,在這里:/** * Main class * @author X2 * */class DrawingPanel extends JPanel implements MouseListener, MouseMotionListener ,KeyListener{    // code    // code    // and more code    static DrawingPanel app ;      private static final Dimension MIN_DIM = new Dimension(300, 300);    private static final Dimension PREF_DIM = new Dimension(500, 500);    public Dimension getMinimumSize() { return MIN_DIM; }    public Dimension getPreferredSize() { return PREF_DIM; }使用我在中創(chuàng)建的menu和的初始代碼?Jpanelmain
查看完整描述

3 回答

?
楊__羊羊

TA貢獻1943條經(jīng)驗 獲得超7個贊

為了詳細說明Kitesurfer所說的內(nèi)容,我將使用“操作”,因為大多數(shù)情況下,我使用工具欄按鈕或其他組件執(zhí)行與菜單項相同的操作。為了避免在類的某個地方(或從當前類可以訪問的任何地方)出現(xiàn)重復或不必要的代碼,我創(chuàng)建了一個Action字段,可以在需要時重用它,或者在決定重構代碼時移動。這是一個例子。


JMenuItem miLoad = new JMenuItem(actionLoad);


Action actionLoad = new AbstractAction("Load") {

    public void actionPerformed(ActionEvent e) {

        //your code to load the file goes here like a normal ActionListener

    }

};

一定要檢查一下API,以查看可以將什么參數(shù)傳遞到AbstractAction類中,我使用了,String因此JMenuItem將顯示字符串,還可以設置Icon,我不記得所有的構造函數(shù),因此值得一看。哦,將in傳遞給類JMenuItems的構造函數(shù)DrawingPanel并不一定是個壞主意,但是如果它繼承自我,JPanel我不相信您可以在菜單欄中添加菜單欄,JPanel因此請確保將菜單欄也添加到您的菜單欄中JFrame。希望能有所幫助。


查看完整回答
反對 回復 2019-11-19
  • 3 回答
  • 0 關注
  • 504 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號