gui编程eclipse项目停止工作

时间:2017-04-30 02:07:42

标签: java eclipse swing user-interface

我对我的Java GUI程序有疑问。当我正在处理它时,它运行正常,但是当它应该显示几个按钮时,它突然停止在窗口中显示任何内容。该窗口看起来像具有正确标题的默认JFrame。我以为它可能是日食的问题,但我不确定。请帮忙,我需要尽快转过来......

以下是GUI类的代码:

package gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import image.PixelImage;



/**
 * 
 * @author ???
 * @version ???
 */
public class SnapShopGUI extends JFrame {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    /**
     * Tracks whether there is currently an image loaded into the application.
     */
    private boolean myIsLoaded;

    /**
     * The image File.
     */
    private static PixelImage myImage;

    /**
     * The JFrame holding all window data.
     */
    private final JFrame mySSWin;

    /**
     * List to hold all the filter buttons.
     */
    private final List<JButton> myFilterButtons;

    /**
     * List to hold all the image buttons.
     */
    private final List<JButton> myImageButtons;

    /**
     * JFileChooser for open and save dialogs.
     */
    private final JFileChooser myFC;

    private final JLabel myImageLabel;

    /**
     * JLabel to actually make it possible to remove the picture from the JFrame
     * because apparently I can't just reference the JLabel in the JFrame like any 
     * other collection ever, I need the exact damn object.
     */
    private final JPanel myImagePanel;

    /**
     * Constructor that creates the JFrame.
     */
    public SnapShopGUI() {
        super();
        myImagePanel = new JPanel();
        myImageLabel = new JLabel();
        mySSWin = new JFrame("TCSS 305 SnapShop");
        myIsLoaded = false;
        myFilterButtons = new ArrayList<JButton>();
        myImageButtons = new ArrayList<JButton>();
        myFC = new JFileChooser();
    }

    /**
     * Method that runs to initialize the GUI window.
     */
    public void start() {
        // Instantiate component variables
        final JPanel p1 = new JPanel(new BorderLayout());
        final JPanel p2 = new JPanel(new GridLayout(7, 1, 0, 3));
        final JPanel p3 = new JPanel(new GridLayout(3, 1, 0, 3));
       // myFilterButtons.add(makeButton("EDGE_DETECT"));
        myFilterButtons.add(new JButton("Flip Horizontal"));
        myFilterButtons.add(new JButton("Flip Vertical"));
        myFilterButtons.add(new JButton("Grayscale"));
        myFilterButtons.add(new JButton("Sharpen"));
        myFilterButtons.add(new JButton("Soften"));
        myImageButtons.add(new JButton("Open..."));
        myImageButtons.add(new JButton("Save as..."));
        myImageButtons.add(new JButton("Close Image"));

        // Set up buttons
        toggleButtons();
        myImageButtons.get(0).addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(final ActionEvent theE) {
                getNewImage();
            }
        });
        myImageButtons.get(1).addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(final ActionEvent theE) {
                saveImage();
            }
        });
        myImageButtons.get(2).addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(final ActionEvent theE) {
                closeImage();
            }
        });

        // Tweak JFrame and JFileChooser
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(true);
        setLayout(new BorderLayout());
        final File workingDir = new File(System.getProperty("user.dir"));
        myFC.setCurrentDirectory(workingDir);

        // Add components to JFrame
        for (final JButton b : myFilterButtons) {
            p2.add(b);
        }
        for (final JButton b : myImageButtons) {
            p3.add(b);
        }
        p1.add(p2, BorderLayout.NORTH);
        p1.add(p3, BorderLayout.SOUTH);
        add(p1, BorderLayout.WEST);

        resizeWindow();
    }

    /**
     * Returns the current image.
     * 
     * @return The image as a PixelImage
     */
    public static PixelImage getImage() {
        return myImage;
    }

    /**
     * Used by buttons to get a new image from the user and update the currently
     * displayed image.
     * 
     * @throws IOException If user passes a file that is not an image.
     */
    private void getNewImage() {
        final int returnVal = myFC.showOpenDialog(mySSWin);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            if (!myIsLoaded) {
                toggleButtons();
            }
            if (myIsLoaded) {
                mySSWin.remove(myImagePanel);
                mySSWin.revalidate();
                mySSWin.repaint();
            }
            try {
                myImage = PixelImage.load(myFC.getSelectedFile());
                myImageLabel.setIcon(new ImageIcon(myImage));
            } catch (final IOException e) {
                JOptionPane.showMessageDialog(mySSWin, 
                                              "Please choose an image file.", 
                                              "Cannot load file", JOptionPane.ERROR_MESSAGE);
            }
            myIsLoaded = true;
            myImagePanel.add(myImageLabel, BorderLayout.NORTH);
            mySSWin.add(myImagePanel, BorderLayout.CENTER);
            resizeWindow();
        }
    }

    /**
     * Creates a filter button for the start method.
     * 
     * @param theButton A String defining what button to make.
     * @return The JButton.
     */
    private JButton makeButton(final String theButton) throws IllegalArgumentException {
        final JButton b = new JButton();
        switch (theButton) {
            case "EDGE_DETECT": b.addActionListener(new EdgeDetectListener());
                break;

            case "EDGE_HIGHLIGHT": b.addActionListener(new EdgeHighlightListener());
                break;

//            case "FLIP_HORIZONTAL": b.addActionListener(new FlipHorizonalListener());
//                break;
//                
//            case "FLIP_VERTICAL": b.addActionListener(new FlipVerticalListener());
//                break;
//                
//            case "GRAYSCALE": b.addActionListener(new GrayscaleListener());
//                break;
//                
//            case "SHARPEN": b.addActionListener(new SharpenListener());
//                break;
//            
//            case "SOFTEN": b.addActionListener(new SoftenListener());
//                break;
//            
//            default: throw new IllegalArgumentException("makeButton() must be called "
//                            + "with a proper filter!");
        }
        return b;
    }

    /**
     * Saves the currently displayed picture to the user's computer.
     */
    private void saveImage() {
        final int rVal = myFC.showSaveDialog(mySSWin);
        if (rVal == JFileChooser.APPROVE_OPTION) {
            try {
                myImage.save(myFC.getSelectedFile());
            } catch (final IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * Enables/disables all filter buttons at once.
     */
    private void toggleButtons() {
        for (final JButton b : myFilterButtons) {
            b.setEnabled(!b.isEnabled());
        }
        for (int i = 1; i <= 2; i++) {
            myImageButtons.get(i).setEnabled(!myImageButtons.get(i).isEnabled());
        }
    }

    /**
     * Packs the window the the size of the components and ensures components
     * cannot be covered. Also resets the window position to the center.
     */
    private void resizeWindow() {
        mySSWin.setVisible(false);
        mySSWin.setMinimumSize(new Dimension(0, 0));
        mySSWin.pack();
        mySSWin.setMinimumSize(new Dimension(mySSWin.getSize()));
        mySSWin.setLocationRelativeTo(null);
        mySSWin.setVisible(true);
    }

    /**
     * Closes the image.
     */
    private void closeImage() {
        mySSWin.remove(myImagePanel);
        mySSWin.revalidate();
        mySSWin.repaint();
        if (myIsLoaded) { 
            toggleButtons();
        }
        myIsLoaded = false;
        resizeWindow();
    }
}

有些代码已被注释掉,但应该在此状态下在窗口内显示内容。

0 个答案:

没有答案