Java Swing学习小结

11 篇文章 0 订阅

1.使窗体位于屏幕中心

Dimension welcomeScreen=Toolkit.getDefaultToolkit().getScreenSize();
        int x=(welcomeScreen.width-this.getSize().width)/2;
        int y=(welcomeScreen.height-this.getSize().height)/2;
        this.setBounds(x, y, 500, 400);

2.设置文件过滤器

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        jFileChooser1.setFileFilter(new FileFilter() {

            @Override
            public boolean accept(File f) {
                return f.getName().toLowerCase().endsWith(".txt")||f.isDirectory(); 
            }

            @Override
            public String getDescription() {
                return ".txt 文件"; //To change body of generated methods, choose Tools | Templates.
            }
        });
        jFileChooser1.showOpenDialog(this);
    }                              

3.进阶文件过滤器

1.该方法获得文件选择器已有的过滤器并将其删除

2.添加自己定义的两个过滤器

public void initFileChooser(){
        FileFilter[] fileFilters=this.FileChooser.getChoosableFileFilters();
        int fSize=fileFilters.length;
        for(int i=0;i<fSize;++i)
            this.FileChooser.removeChoosableFileFilter(fileFilters[i]);
        this.FileChooser.addChoosableFileFilter(new TxtFilter());
        this.FileChooser.addChoosableFileFilter(new JavaFilter());
    }

4.打开文件设置

private void jButtonOpenActionPerformed(java.awt.event.ActionEvent evt) {                                            
        // TODO add your handling code here:
        FileChooser.showOpenDialog(this);
        File selectFile=FileChooser.getSelectedFile();
        if(selectFile==null){
            JOptionPane.showMessageDialog(this, "你没有选择打开的文件","提示:", JOptionPane.INFORMATION_MESSAGE);
            return;
        }else{
            int choose=JOptionPane.showConfirmDialog(this, "你确定打开文件吗","请确认:", JOptionPane.YES_NO_OPTION,JOptionPane.INFORMATION_MESSAGE);
            if(choose==JOptionPane.NO_OPTION){
                JOptionPane.showMessageDialog(this, "你取消了打开文件", "提示:", JOptionPane.INFORMATION_MESSAGE);
                return;
            }else{
                String tempString="";
                BufferedReader bufferedReader=null;
                String pathNameString=selectFile.getPath();
                this.jTextFieldFilePath.setText(pathNameString);
                try {
                    int size=(int)selectFile.length();
                    byte[] tempArray=new byte[size];
                    FileInputStream fin=new FileInputStream(selectFile);
                    fin.read(tempArray);
                    tempString=new String(tempArray);
                    this.jTextAreaContent.setText(tempString);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            
            }
        }
    }              

5.文件打开和文件保存设置

 private void jMenuItemOpenFileActionPerformed(java.awt.event.ActionEvent evt) {                                                  
        // TODO add your handling code here:JFileChooser fileChooser=new JFileChooser();
        JFileChooser fileChooser=new JFileChooser();
        fileChooser.showOpenDialog(this);
        File selectfFile=fileChooser.getSelectedFile();
        FileInputStream finFileInputStream;
        try {
            finFileInputStream=new FileInputStream(selectfFile);
            byte[] filecontent=new byte[(int)selectfFile.length()];
            finFileInputStream.read(filecontent);  
            String fileString=new String(filecontent);
            this.jTextArea1.setText(fileString);
           }catch(Exception e){
            e.printStackTrace();
        }
      
    }                                                 

    private void jMenuItemSaveFileActionPerformed(java.awt.event.ActionEvent evt) {                                                  
        // TODO add your handling code here:
        JFileChooser fileChooser=new JFileChooser();
        fileChooser.showSaveDialog(this);
        File saveFile=fileChooser.getSelectedFile();
        if(saveFile==null){
            JOptionPane.showMessageDialog(this, "你没有保存文件","提示:", JOptionPane.INFORMATION_MESSAGE);
            return;
        }
        System.out.println(saveFile.getName());
        try {
            FileOutputStream outputStream=new FileOutputStream(saveFile);
            String fileString=this.jTextArea1.getText();
            outputStream.write( fileString.getBytes());
        } catch (Exception e) {
        }
       
    }                                                 

    private void jMenuItemExitActionPerformed(java.awt.event.ActionEvent evt) {                                              
        // TODO add your handling code here:
        System.exit(0);
    }                                

6.java.lang.NullPointerException异常

如果出现这种错误,一般原因是 1)使用了null对象的方法。 2)使用了只是对象引用的声明但并没有创建对象。

7.JFrame设置背景图片

 private JPanel imagePanel;
    private ImageIcon background;
    public BoxLayout() {
      
        background = new ImageIcon("e://2.jpg");// 背景图片
        JLabel label = new JLabel(background);// 把背景图片显示在一个标签里面
        // 把背景图片添加到分层窗格的最底层作为背景
        this.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE));
        // 把标签的大小位置设置为图片刚好填充整个面板
        label.setBounds(0, 0, background.getIconWidth(),background.getIconHeight());
        
        // 把内容窗格转化为JPanel,否则不能用方法setOpaque()来使内容窗格透明
        imagePanel = (JPanel)this.getContentPane();
        imagePanel.setOpaque(false);
        // 内容窗格默认的布局管理器为BorderLayout
        
        
        initComponents();
    }
其它JPanel面板的Opaque属性设置为false,即透明状态.

8.鼠标事件中的弹出式菜单触发器

 private void jTreeCountryMouseReleased(java.awt.event.MouseEvent evt) {                                           
        // TODO add your handling code here:
        if(evt.isPopupTrigger())
             this.jPopupMenuNode.show(evt.getComponent(), evt.getX(), evt.getY());
    }                                          

    private void jTreeCountryMousePressed(java.awt.event.MouseEvent evt) {                                          
        // TODO add your handling code here:
        if(evt.isPopupTrigger())
            this.jPopupMenuNode.show(evt.getComponent(), evt.getX(), evt.getY());
    }                     
单击鼠标左键,会产生鼠标按下和鼠标释放事件,但这不属于弹出式菜单触发器。鼠标右键的按下和释放属于弹出式触发器。所以,以上代码只有鼠标右键的动作才会出现弹出式菜单。

9.设置表格网线

this.jTable2.setShowGrid(true);
默认情况下,JTable的表格网线是不显示的,需要设置为true

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值