单击按钮时,将整个窗口内容替换为图像

时间:2018-02-17 17:06:42

标签: java swing jframe jbutton

解决

所以我有一个问题,当我点击按钮时,我想要一个图像来替换窗口的整个内容。但它只是取代了我认为是小组的一部分。我不应该在这种情况下使用面板吗?我发现一些在线的代码并没有使用可以使用的面板,但也许有一种情况我可以删除面板并在点击按钮时用我的图像覆盖整个框架?

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class SatNav extends JFrame {

private JFrame frame;
private JPanel panel;

private JLabel satelliteLabel;
private JLabel aboutLabel;

private JButton satellite;
private JButton about;

public SatNav() {

    frame = new JFrame("Work Package 5");
    frame.setVisible(true);
    frame.setSize(300, 380);

    about = new JButton("About");
    add(about);

    event abo = new event();
    about.addActionListener(abo);

    panel = new JPanel();
    frame.add(panel);
    panel.add(about);

    setLocationRelativeTo(null); //This is for centering the frame to your screen.
    setDefaultCloseOperation(EXIT_ON_CLOSE); //This for closing your application after you closing the window.
}

public class event implements ActionListener {
    public void actionPerformed(ActionEvent abo) {
        ImagePanel imagePanel = new ImagePanel();

        //JFrames methods
        panel.add(imagePanel, BorderLayout.CENTER);
        revalidate();
        repaint();

        about.setVisible(false);
        //satellite.setVisible(false);
    }
}

public class ImagePanel extends JPanel {

    private BufferedImage image;

    public ImagePanel() {
        try {
            image = ImageIO.read(new File("about.png"));
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        setBorder(BorderFactory.createLineBorder(Color.black, 2));
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
    }
}

public static void main(String[] args) {
    new SatNav();
}
}

The output is attached

1 个答案:

答案 0 :(得分:0)

你的问题是你正在将面板JPanel视为没有BorderLayout。相反,它具有JPanel的默认FlowLayout,它将组件的大小调整为其首选大小(此处为[0,0]),而不是包含组件填充容器。简单的解决方案:为您的面板提供BorderLayout:

panel = new JPanel(new BorderLayout());

现在添加组件时,容器的布局将遵循BorderLayout常量。

另一种可能更好,更持久的解决方案是使用CardLayout来帮助您交换组件。

例如:

import java.awt.CardLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;

public class SatNav2 extends JPanel {
    private static final long serialVersionUID = 1L;
    // a publicly available example image for demonstration purposes:
    public static final String SAT_PATH = "https://upload.wikimedia.org"
            + "/wikipedia/commons/1/18/AEHF_1.jpg";
    private static final String INTRO_PANEL = "intro panel";
    private static final String IMAGE_LABEL = "image label";
    // our layout
    private CardLayout cardLayout = new CardLayout();
    // JLabel to display the image
    private JLabel imgLabel = new JLabel();

    public SatNav2(Image img) {
        // put image into JLabel
        imgLabel.setIcon(new ImageIcon(img));

        // JPanel to hold JButton
        JPanel introPanel = new JPanel();
        // add button that does the swapping
        introPanel.add(new JButton(new ShowImageAction("Show Image")));

        // set the CardLayout and add the components. Order of adding 
        // is important since the first one is displayed
        setLayout(cardLayout);
        // add components w/ String constants
        add(introPanel, INTRO_PANEL);
        add(imgLabel, IMAGE_LABEL);
    }

    private class ShowImageAction extends AbstractAction {
        public ShowImageAction(String text) {
            super(text);
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            // tell card layout to show next component
            cardLayout.next(SatNav2.this);
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            try {
                createAndShowGui();
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
    }

    private static void createAndShowGui() throws IOException {
        // get the image
        URL imgUrl = new URL(SAT_PATH);
        Image img = ImageIO.read(imgUrl);
        // pass image into our new JPanel
        SatNav2 mainPanel = new SatNav2(img);
        JFrame frame = new JFrame("Satellite");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.add(mainPanel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}