0

我试图让一个班级的 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);
    }
}
4

1 回答 1

0

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

... 结果是:

椭圆形顶部的按钮

于 2013-08-14T07:44:57.837 回答