我想在JFrame中添加背景,应该如何操作?

huangapple 未分类评论47阅读模式
英文:

I want to add background in JFrame how could I?

问题

这是我的代码,我该如何在其中添加背景?我尝试了所有可能的解决方案,但图片不会显示在背景中。

我应该怎么做?有人可以帮我吗?

英文:

Here is my code how can I add a background in it I tried every possible solution but the picture won't show up in the background.

what should I do can anyone help me?

  public class Triangle {
        JLabel l1;
        public static void main(String[] args) {
            new Triangle();
        }
    
        public Triangle() {
            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 BorderLayout());
                    frame.add(new TestPane());
                    frame.setSize(400,400);
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                 
                }
            });
        }
    
        public class TestPane extends JPanel {
           
          
            private Polygon poly;
            
    
           {
          
    
                poly = new Polygon(
                    new int[]{110, 150, 50},
                    new int[]{200, 0, 0},
                        3);
            }
    
            
        
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(400, 400);
                
            }
           
    
         
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                
                Graphics2D g2 = (Graphics2D) g;
                g2.setColor(Color.blue);
                g2.fill(poly);
                 g2.setColor(Color.red);
                 g2.fillRect(0,0,25,75); 
                 g2.setColor(Color.green);    
                 g2.fillOval(200,100,100,50);
                 g2.setColor(Color.green);    
                 g2.fillOval(300,300,250,250);
                
    
             
                 
    
            }
        }
    }

Here is my code how can I add a background in it I tried every possible solution but the picture won't show up in the background.

what should I do can anyone help me?

答案1

得分: 0

设置 JPanel 的背景颜色

试试这个:

TestPane myPane = new TestPane();
myPane.setBackground(Color.green);
frame.add(myPane);
英文:

Set background color to the JPanel

Try this:

TestPane myPane = new TestPane();
myPane.setBackground(Color.green);
frame.add(myPane);

huangapple
  • 本文由 发表于 2020年5月29日 18:41:42
  • 转载请务必保留本文链接:https://java.coder-hub.com/62084077.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定