首页 文章

如何在Java中添加多个扩展JPanel到一个JFrame中?

提问于
浏览
2

我想要两个不同类的两个图像,它们将JPanel扩展为并排 .

我遇到的问题是两个JPanel应该在JFrame中,但是当我执行framename.add(panel)时,它会替换另一个而不是并排添加其中两个 .

我尝试在主类中添加flowlayout和其他布局,但没有出现任何图像 .

So my question is ,如果我有两个扩展Jpanel的类,我如何在Jframe中添加这两个面板,以便它们并排(彼此相邻)而不替换其他面板?

任何建议将不胜感激 .

Edit :如果我将JFrame扩展到一个类,该类是否会自动成为JPanel本身?我知道扩展意味着什么,但我不确定它在Jframe方面是如何工作的 .

Main.java

import javax.swing.*;
import java.awt.*;
import java.net.URL;

public class Main
{       
public static void main(String[] args) 
    {        
        JFrame frame = new JFrame();           

        Panel1 s = new Panel1(); //picture 1
        Panel2 n = new Panel2(); //picture 2

        frame.add(n);
        frame.add(s); //here is the problem, it replaces the previous panel

        f.setSize(200,100);
        f.setLocation(0,400);                    
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

Panel1.java

import javax.swing.*;
import java.awt.*;
import java.net.URL;

public class image2 extends JPanel 
    {
        ImageIcon anotherIcon;

        public image2() //constructor
            {                               
                 URL imageURL = Panel1.class.getResource("images/puppy.png");            
                 anotherIcon = new ImageIcon(imageURL);
            }

        public void paint(Graphics g)
            {
                super.paintComponent(g);                
                anotherIcon.paintIcon(this, g, 0, 0);                           
            }       
    }

Panel2.java

import javax.swing.*;
import java.awt.*;
import java.net.URL;

public class Panel2 extends JPanel 
    {
        ImageIcon anotherIcon2;

        public Panel2() //constructor
            {                               
                 URL imageURL = Panel2.class.getResource("images/puppy2.png");           
                 anotherIcon = new ImageIcon(imageURL);
            }

        public void paint(Graphics g)
            {
                super.paintComponent(g);                
                anotherIcon2.paintIcon(this, g, 0, 0);                          
            }       
    }

7 回答

  • 0

    我希望两个不同类的两个图像能够将JPanel并排放在一起 .

    要么

  • 0

    JFrame的默认布局管理器是BorderLayout . 尝试将其更改为使用类似GridLayout的内容

    看看A visual guide to layout managers了解更多想法

  • 0

    试试 frame.getContentPane().add(...)

    并将布局 of the contentpane 设置为FlowLayout(或其他)

    JFrame f = new JFrame("This is a test");
    f.setSize(400, 150);
    Container content = f.getContentPane();
    content.setLayout(new FlowLayout()); 
    content.add(new JButton("Button 1"));
    content.add(new JButton("Button 2"));
    f.setVisible(true);
    
  • 0

    您要将面板直接添加到JFrame,您必须在contentPane中添加面板 .

    它还推荐创建一个面板作为contentPane frame.setContentPane(myPanel); 并使用一些布局配置它(BorderLayout,GridBagLayout,等等......)

  • 0

    在框架集FlowLayout()

    frame.setLayout(new FlowLayout());

  • 2

    我更正了Panel1.java和Panel2.java的小错误,并添加了一个如何使用 GridBagLayout / GridBagConstraints combo将它们并排放置的示例:

    Main.java

    import javax.swing.*;
    import java.awt.*;
    import java.net.URL;
    
    public class Main {
        public static void main(String[] args) {
                GridBagConstraints gbc = new GridBagConstraints();
    
                JFrame frame = new JFrame();
                frame.setLayout(new GridBagLayout());
                Panel1 s = new Panel1(); //picture 1
    
                Panel2 n = new Panel2(); //picture 2
    
                gbc.gridx = 0;
                gbc.gridy = 0;
                gbc.gridwidth = 1;
                gbc.weightx = 1;
                gbc.weighty = 1;
                gbc.fill = GridBagConstraints.BOTH;
                frame.add(n, gbc);
    
                gbc.gridx = 1;
                gbc.gridy = 0;
                gbc.gridwidth = GridBagConstraints.REMAINDER;
                frame.add(s, gbc); //here is the problem, it replaces the previous panel
    
                frame.setSize(200,100);
                frame.setLocation(0,400);
                frame.setVisible(true);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           }
        }
    

    Panel1.java

    import javax.swing.*;
        import java.awt.*;
        import java.net.URL;
    
        public class Panel1 extends JPanel {
            ImageIcon anotherIcon;
    
            public Panel1 ( ) {
                URL imageURL = Panel1.class.getResource("images/puppy.png");
                anotherIcon = new ImageIcon(imageURL);
            }
    
            public void paint(Graphics g) {
                super.paintComponent(g);
                anotherIcon.paintIcon(this, g, 0, 0);
            }
        }
    

    Panel2.java

    import javax.swing.*;
        import java.awt.*;
        import java.net.URL;
    
        public class Panel2 extends JPanel {
            ImageIcon anotherIcon;
    
            public Panel2 ( ) {
                URL imageURL = Panel2.class.getResource("images/puppy2.png");
                anotherIcon = new ImageIcon(imageURL);
            }
    
            public void paint(Graphics g) {
                super.paintComponent(g);
                anotherIcon.paintIcon(this, g, 0, 0);
            }
        }
    

    您的代码的一般想法没有错,只是将面板放在另一个上面,导致一个隐藏另一个....

  • 1

    你可以试试这个 . 将框架的布局设置为: setLayout(null) 然后可以向面板添加边界即 . setBounds() 然后将这些面板添加到框架中

相关问题