【问题标题】:placing a jpanel on top of a jpanel from another class将 jpanel 放在另一个类的 jpanel 之上
【发布时间】:2013-08-14 03:47:43
【问题描述】:

我试图让一个班级的 Jpanel 出现在另一个班级的 Jpanel 之上。我可以以迂回的方式做到这一点,但我知道从长远来看我会后悔的。我的框架类包含带有按钮的主 Jpanel,我想暂时在其顶部放置另一个带有按钮的 Jpanel。我试图简化我的代码。注意第一块代码下面的 ShopButton 方法和 Shop Class。这就是我想要关注的。我正在尝试将 Shop JPanel 放在现有的“框架”(这就是我的班级)JPanel 上。

package sonomaroller;
//Bunch of imports i didnt include
import javax.swing.text.StyleConstants;

public class frame extends JPanel implements Runnable {
    //buttons
    private JButton Button4;
    //images
    private Image Sonoma;
    private Image pic;
    //booleans
    private boolean loaded;
    public boolean inTown = true;
    public boolean isShopping = false;
    //variables
    //strings 
    //arrays
    public Integer[] attributes = {0,1,2}; //attk - 0, def - 1 , magic - 2
    //new jpanel
    JTextPane field = new JTextPane();
    JScrollPane sp = new JScrollPane(field);
    //calling another class making it public
    public Shop shopping = new Shop();

    public void run(){
    }

    public frame(){
        loadpics();
        attackButton();
        magicButton();
        travelButton();
        shopButton();
        textField(); 
    }

    public void paint(Graphics g){
        super.paint(g);
        g.drawString(firstName +givenName+ secondName,225,40);
        g.drawRect(100, 50, 350, 275);
        if(loaded){
            g.drawImage(Sonoma,101,51,350,275,null);
        }
        if(isShopping==true){
           shopping.paint(g);
        }
    }

    public void shopButton(){
        //for shop button
        Button4= new JButton("Shop");
        Button4.addActionListener( new ActionListener() {
            public void actionPerformed(ActionEvent Event) {
                loaded=false; 
                isShopping=true;
                repaint();
            }
        });
        Button4.setBounds(130, 485, 90, 30);
        add(Button4);
    }

    public void loadpics(){
        Sonoma = new ImageIcon("C:\\Users\\Camtronius\\Documents\\NetBeansProjects\\SonomaRoller\\src\\sonomaroller\\Sonoma.png").getImage();
        System.out.println("i was called?");  
        loaded = true;
        repaint();
    }
}

_____________________________________店铺类

public class Shop extends JPanel {
    public JButton CloseButton;
    public Shop(){
        CloseButton();
        setVisible(true);
        setLayout(null);
    }
    public void paint(Graphics g){
        System.out.println("shop is working");
        g.setColor(Color.WHITE);
        g.fillRect(100, 50, 350, 275);
        g.drawRect(100, 50, 350, 275);
    } 
    public void CloseButton(){
        //for 
        CloseButton= new JButton("Exit Shop");
        CloseButton.addActionListener( new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent Event) {
                System.out.println("am i alive?");
            }
        });
        CloseButton.setBounds(100, 100, 90, 30);
        add(CloseButton);
    }
}

【问题讨论】:

    标签: swing jframe jpanel paint


    【解决方案1】:

    覆盖paintComponent()而不是paint(),然后你可以正常放置组件,让swing负责绘制它们:

    import java.awt.Color;
    import java.awt.FlowLayout;
    import java.awt.Graphics;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    public class Sample {
        private static class ImagePanel extends JPanel {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                // Some background drawing
                g.setColor(Color.BLUE);
                g.fillOval(0, 0, getWidth(), getHeight());
            }
        }
    
        Sample() {
            JFrame frame = new JFrame("Button atop drawing");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLocationByPlatform(true);
            JPanel panel = new ImagePanel();
            panel.setLayout(new FlowLayout(FlowLayout.CENTER, 50, 50));
    
            JButton button = new JButton("Sample button");
            panel.add(button);
    
            frame.add(panel);
            frame.pack();
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new Sample();
                }
            });
        }
    }
    

    ...结果:

    【讨论】:

    • 哇,我不敢相信这有效...非常感谢!我需要去复习一下paint和paint componenet之间的区别
    • 实际上现在它只添加了按钮,并没有绘制我想要的填充矩形。我会试着找出原因。谢谢
    • @CameronRoberson paint() 是顶级的绘制方法,负责调用paintComponent()paintChildren()paintBorder()。所以通常你只想覆盖paintComponent()。如果没有在您的子组件下方绘制某些内容,我的猜测是它们要么不遵守不透明合同,要么在背景上绘制。对于按钮,您可以通过设置button.setContentAreaFilled(false); button.setOpaque(false); 快速检查它是否正在绘制,并查看背景是否显示出来。
    猜你喜欢
    • 2017-04-18
    • 1970-01-01
    • 2012-12-13
    • 2012-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多