将jpanel放在另一个类的jpanel之上

时间:2013-08-14 03:47:43

标签: swing jframe jpanel paint

我试图让一个类中的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();
    }
}

__ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ ___ _Shop Class

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);
    }
}

1 个答案:

答案 0 :(得分:0)

覆盖paintComponent()而非paint(),然后您可以正常放置组件并让摆动来绘制它们:

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();
            }
        });
    }
}

...导致:

Button atop oval