How can I paint in an specific JPanel when more than one in same frame- Java

后端 未结 1 1091

Updated - this is related with the following link I posted yesterday.

https://stackoverflow.com/questions/15916360/swing-gui-application-in-java-with-multiple-frames

1条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-22 09:46

    You could...

    Use the glass pane...

    enter image description here

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.GridLayout;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.imageio.ImageIO;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    import javax.swing.border.CompoundBorder;
    import javax.swing.border.EmptyBorder;
    import javax.swing.border.LineBorder;
    
    public class TestGlassPane04 {
    
        public static void main(String[] args) {
            new TestGlassPane04();
        }
    
        public TestGlassPane04() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    }
                    
                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new GridLayout(2, 2, 10, 10));
                    frame.add(createBox(1));
                    frame.add(createBox(2));
                    frame.add(createBox(3));
                    frame.add(createBox(4));
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setGlassPane(new GlassPane());
                    frame.getGlassPane().setVisible(true);
                    frame.setVisible(true);
                }
            });
        }
        
        protected JLabel createBox(int box) {
            JLabel label = new JLabel("Box " + box);
            label.setBorder(new CompoundBorder(new LineBorder(Color.GRAY), new EmptyBorder(10, 10, 10, 10)));
            return label;
        }
        
        public class GlassPane extends JPanel {
            
            private BufferedImage background;
            
            public GlassPane() {
                setOpaque(false);
                try {
                    background = ImageIO.read(getClass().getResource("/sillydash.png"));
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
            
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(200, 200);
            }
            
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g.create();
                if (background != null) {
                    int x = (getWidth() - background.getWidth()) / 2;
                    int y = (getHeight() - background.getHeight()) / 2;
                    g2d.drawImage(background, x, y, this);
                }
                g2d.dispose();
            }
        }
        
    }
    

    Take a look at How to use Root Panes for more details...

    Or you could...

    Use a JXLayer/JLayer to accomplish a more localized version of a glass pane...

    0 讨论(0)
提交回复
热议问题