我的JFrame停止了工作

时间:2018-01-05 12:09:35

标签: java swing file jframe jpanel

直到昨天我的JFrame工作得很好,今天我做了一点编辑,但它不再工作了。我试图删除所有未与我的JFrame严格绑定的代码,但它仍然拒绝显示任何内容,我会在此处粘贴我的代码

这是我的主要文件

public class ImageEditor extends JFrame{

    JFrame mainFrame = new JFrame("ImageEditor");
    JMenuBar menu = new JMenuBar();
    JMenu fileMenu = new JMenu("File");
    JMenu editMenu = new JMenu("Edit");
    JMenuItem openButton = new JMenuItem("Open...");
    JMenuItem saveButton = new JMenuItem("Save");
    JMenuItem saveAsButton = new JMenuItem("Save as...");
    JMenuItem exitButton = new JMenuItem("Exit");
    JMenuItem rotatePositive = new JMenuItem("Rotate 45°");
    JMenuItem rotateNegative = new JMenuItem("Rotate -45°");
    JMenuItem resizeButton = new JMenuItem("Resize");
    JMenuItem adjustBrightnessButton = new JMenuItem("Adjust brightness");
    JMenuItem insertLabel = new JMenuItem("Add text...");
    JFileChooser fileChooser;
    JFileChooser fileSaver;
    JSlider brightnessSlider = new JSlider(0, 200, 100);
    ImagePanel photoViewer = new ImagePanel();

    public ImageEditor() {

        editMenu.setEnabled(false);
        mainFrame.setSize(1000,750);
        mainFrame.setResizable(true);
        photoViewer.setSize(1000,750);

        openButton.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK));
        openButton.addActionListener((java.awt.event.ActionEvent evt) -> {
            fileChooser = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
            fileChooser.setVisible(true);
            int returnValue = fileChooser.showOpenDialog(null);

            if (returnValue == JFileChooser.APPROVE_OPTION) {
                editMenu.setEnabled(true);
                fileChooser.setVisible(false);
                File[] selectedImages = fileChooser.getSelectedFiles();
                photoViewer.updateGraphics(selectedImages);
                photoViewer.repaint();
            }
        });

        exitButton.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, ActionEvent.CTRL_MASK));
        exitButton.addActionListener((java.awt.event.ActionEvent evt) -> {
            System.exit(0);
        });

        saveButton.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));
        saveButton.addActionListener((java.awt.event.ActionEvent evt) -> {
            fileSaver = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
            fileSaver.setVisible(true);
            fileSaver.setMultiSelectionEnabled(false);
             int returnValue = fileSaver.showOpenDialog(null);

            if (returnValue == JFileChooser.APPROVE_OPTION){
                String path = fileSaver.getSelectedFile().getAbsolutePath();
                System.err.println(path);
            }
        });

        rotatePositive.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, ActionEvent.CTRL_MASK));
        rotatePositive.addActionListener((java.awt.event.ActionEvent evt) -> {
            photoViewer.positiveRotation();
        });

        rotateNegative.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, ActionEvent.CTRL_MASK));
        rotateNegative.addActionListener((java.awt.event.ActionEvent evt) -> {
            photoViewer.negativeRotation();
        });

        insertLabel.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, ActionEvent.CTRL_MASK));
        insertLabel.addActionListener((java.awt.event.ActionEvent evt) -> {
            JFrame adder = new JFrame("Add text");
            adder.setVisible(true);
            adder.setAlwaysOnTop(true);
            adder.setBounds(500, 250, 250, 125);
            adder.setLayout(new GridLayout(2,2));
            JLabel labelText = new JLabel("Text: ");
            JTextField label = new JTextField();
            JButton cancelButton = new JButton("Cancel");
            JButton confirmButton = new JButton("OK");
            adder.add(labelText);
            adder.add(label);
            adder.add(cancelButton);
            adder.add(confirmButton);
            confirmButton.addActionListener((java.awt.event.ActionEvent evt2) -> {
                adder.setVisible(false);
                mainFrame.addMouseListener(new MouseListener() {
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        photoViewer.label=label.getText();
                        photoViewer.lx=e.getX();
                        photoViewer.ly=e.getY();
                        photoViewer.repaint();
                    }

                    @Override
                    public void mousePressed(MouseEvent e) {
                        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
                    }

                    @Override
                    public void mouseReleased(MouseEvent e) {
                        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
                    }

                    @Override
                    public void mouseEntered(MouseEvent e) {
                        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
                    }

                    @Override
                    public void mouseExited(MouseEvent e) {
                        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
                    }
                });

            });

        });

        resizeButton.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, ActionEvent.CTRL_MASK));
        resizeButton.addActionListener((java.awt.event.ActionEvent evt) -> {
            JFrame resizer = new JFrame("Resize");
            resizer.setVisible(true);
            resizer.setAlwaysOnTop(true);
            resizer.setBounds(500, 250, 250, 125);
            resizer.setLayout(new GridLayout(3,2));
            JLabel widthLabel = new JLabel("Width: ");
            JLabel heightLabel = new JLabel("Height: ");
            JFormattedTextField widthField = new JFormattedTextField();
            JFormattedTextField heightField = new JFormattedTextField();
            JButton cancelButton = new JButton("Cancel");
            JButton confirmButton = new JButton("OK");
            widthField.setFormatterFactory(new DefaultFormatterFactory(new NumberFormatter(java.text.NumberFormat.getIntegerInstance())));
            heightField.setFormatterFactory(new DefaultFormatterFactory(new NumberFormatter(java.text.NumberFormat.getIntegerInstance())));
            resizer.add(widthLabel);
            resizer.add(widthField);
            resizer.add(heightLabel);
            resizer.add(heightField);
            resizer.add(cancelButton);
            resizer.add(confirmButton);

            cancelButton.addActionListener((java.awt.event.ActionEvent evt2) -> {resizer.setVisible(false);});
            confirmButton.addActionListener((java.awt.event.ActionEvent evt2) -> {photoViewer.selectedImg = photoViewer.resize(photoViewer.selectedImg, Integer.parseInt(widthField.getText().replace(".", "")), Integer.parseInt(heightField.getText().replace(".", "")));});  //WORKING

        });

        adjustBrightnessButton.setEnabled(false);

        //adjustBrightnessButton.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, ActionEvent.CTRL_MASK));
        /*adjustBrightnessButton.addActionListener((java.awt.event.ActionEvent evt) -> {
            JFrame slider = new JFrame("Brightness");
            slider.setSize(300,100);
            slider.setAlwaysOnTop(true);
            slider.add(brightnessSlider);
            slider.setVisible(true);
            brightnessSlider.addMouseListener(new MouseListener(){

                @Override
                public void mouseClicked(MouseEvent e){}

                @Override
                public void mousePressed(MouseEvent e){}

                @Override
                public void mouseReleased(MouseEvent e) {
                    photoViewer.adjustBrightness(Float.valueOf((brightnessSlider.getValue()))/100);     //Less memory usage
                }

                @Override
                public void mouseEntered(MouseEvent e){}

                @Override
                public void mouseExited(MouseEvent e) {}
            });
            brightnessSlider.addChangeListener((ChangeEvent e) -> {
                photoViewer.adjustBrightness(Float.valueOf((brightnessSlider.getValue()))/100);         //Real time update, but VERY HEAVY
            });
        });*/

        menu.add(fileMenu);
        menu.add(editMenu);

        fileMenu.add(openButton);
        fileMenu.add(saveButton);
        fileMenu.add(saveAsButton);
        fileMenu.add(exitButton);

        editMenu.add(resizeButton);
        editMenu.add(rotatePositive);
        editMenu.add(rotateNegative);
        editMenu.add(adjustBrightnessButton);
        editMenu.add(insertLabel);

        menu.setVisible(true);
        mainFrame.setJMenuBar(menu);

        mainFrame.setVisible(true);
        mainFrame.add(photoViewer);
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }
    public static void main(String[] args) {
        ImageEditor img = new ImageEditor();
    }
}

这是我的第二个文件,即photoViewer

public class ImagePanel extends JPanel{

    public ArrayList<File> selectedFiles = new ArrayList<>();
    BufferedImage selectedImg;
    public int rotation = 0;
    boolean flag=true;
    String label="";
    int lx,ly;

    @Override
    public void paintComponent(Graphics g){

        super.paintComponent(g);

        AffineTransform at = new AffineTransform();

        at.translate(getWidth() / 2, getHeight() / 2);

        at.rotate(Math.toRadians(45*rotation));

        if(flag && !selectedFiles.isEmpty()){
            selectedImg = loadImage(selectedFiles.get(0));      //WORKING
            flag = false;
        }

        if(!selectedFiles.isEmpty())
            at.translate(-selectedImg.getWidth() / 2, -selectedImg.getHeight() / 2);

        Graphics2D g2d = (Graphics2D) g;

        if(!label.isEmpty()){
            System.err.println("PHOTODONE");
            g2d.drawString("vdsavdfvdfsvfdsvdfs", 0, 0);        //PROVARE A UTILIZZARE JFRAME.CREATEIMAGE PER COPIARE L'IMMAGINE E SCRIVERE SU QUELLA
            label="";
            repaint();
        }

        g2d.drawImage(selectedImg, at, null);

    }    
    public BufferedImage loadImage(File fileName){

        BufferedImage bfImg = null;

        try{
            bfImg = ImageIO.read(fileName);
            if(bfImg.getWidth() > 800)
                bfImg = resize(bfImg, 800, bfImg.getHeight());
            if(bfImg.getHeight() > 750)
                bfImg = resize(bfImg, bfImg.getWidth(), 750);
        }
        catch(IOException e){
            System.err.println("Error loading image");
        }
        return bfImg;
    }
    public void updateGraphics(File[] files){
        selectedFiles = new ArrayList<>(Arrays.asList(files));
    }
    public void positiveRotation(){
        if(rotation < 8){
            rotation++;
            repaint();
        }                               //BOTH WORKING
        else{
            rotation=1;
            repaint();
        }
    }
    public void negativeRotation(){
        if(rotation > -8){
            rotation--;
            repaint();
        }
        else{
            rotation=-1;
            repaint();
        }
    }
    /*public void adjustBrightness(Float sliderValue){        
        RescaleOp rop = new RescaleOp(sliderValue, 0, null);
        rop.filter(selectedImg, selectedImg);                   //WORKING
        repaint();
    }*/
    public BufferedImage resize(BufferedImage img, int width, int height) { 
        Image tmp = img.getScaledInstance(width, height, Image.SCALE_SMOOTH);
        BufferedImage dimg = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

        Graphics2D g2d = dimg.createGraphics();
        g2d.drawImage(tmp, 0, 0, null);
        g2d.dispose();

        return dimg;
    }
}

我今天所做的编辑改变了主文件中的一些内容(以及来自photoViewer,因为它们已被绑定)

我改变了这个

selectedImages=fileChooser.getSelectedFile().getAbsolutePath()
photoViewer.updateGraphics(selectedImages);

到此

File[] selectedImages = fileChooser.getSelectedFiles();
photoViewer.updateGraphics(selectedImages);

然后我修改了两个文件,以确保函数将接受一个FIle数组,但如果我删除所有不相关的代码,注释photoViewer调用,注释所有的photoViewer文件,它仍然无法工作。发生了什么事?

ADDON

我想出了如何让NetBeans显示堆栈跟踪,这里是

    java.lang.NullPointerException
    at javax.swing.plaf.basic.BasicScrollPaneUI$Handler.mouseWheelMoved(BasicScrollPaneUI.java:866)
    at org.netbeans.modules.editor.lib2.view.DocumentViewOp.mouseWheelMoved(DocumentViewOp.java:1523)
    at org.netbeans.modules.editor.lib2.view.DocumentViewOp$MouseWheelDelegator.mouseWheelMoved(DocumentViewOp.java:1657)
    at java.awt.AWTEventMulticaster.mouseWheelMoved(AWTEventMulticaster.java:532)
    at java.awt.Component.processMouseWheelEvent(Component.java:6623)
    at java.awt.Component.processEvent(Component.java:6307)
    at java.awt.Container.processEvent(Container.java:2236)
    at java.awt.Component.dispatchEventImpl(Component.java:4891)
    at java.awt.Container.dispatchEventToSelf(Container.java:2318)
    at java.awt.Component.dispatchMouseWheelToAncestor(Component.java:5044)
    at java.awt.Component.dispatchEventImpl(Component.java:4778)
    at java.awt.Container.dispatchEventImpl(Container.java:2294)
    at java.awt.Component.dispatchEvent(Component.java:4713)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4554)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
    at java.awt.Container.dispatchEventImpl(Container.java:2280)
    at java.awt.Window.dispatchEventImpl(Window.java:2750)
    at java.awt.Component.dispatchEvent(Component.java:4713)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.awt.EventQueue$4.run(EventQueue.java:729)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    at org.netbeans.core.TimableEventQueue.dispatchEvent(TimableEventQueue.java:159)
[catch] at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

0 个答案:

没有答案