2 kandankk KANDANKK 于 2015.07.19 21:09 提问

为什么最小化后Panel就超出主界面范围 4C

Panel

 public class GameJPanel extends JPanel {
    private static final long serialVersionUID = 1L;
    private Image image;
    Random rand = new Random();
    public ArrayList<Fish> fishes=new ArrayList<Fish>();


    public GameJPanel() {
        this.setBounds(40, 65, 800, 480);
        image = Toolkit.getDefaultToolkit().getImage("image/mainbg.png"); 

        this.setVisible(true);


    }

    public void paintComponent(Graphics g)
    {
        g.drawImage(image,0,0, this.getWidth(), this.getHeight(),this);

    }



    }

MainFrame

public class MainJFrame extends JFrame implements ActionListener ,Observer{
    private static final long serialVersionUID = 1L;
    private StartJPanel panel; //开始界面
    private GameJPanel mainpanel; //游戏界面
    private HelpJPanel helppanel;
    JLabel mainLabel;
    JButton min, close;
    int x;
    int y;
    protected boolean  isStart;
    protected boolean  isExit;
    protected boolean  isHelp;

    public MainJFrame(Observable ob)
    {
        ob.addObserver(this);
        Observable obs = ob;

        this.panel = new StartJPanel(obs);
        this.mainpanel = new GameJPanel();
        this.helppanel=new HelpJPanel();
        // 设置窗体可以跟随鼠标移动和透明
        new GameUtil(this);
        this.isStart = panel.getIsStart();
        this.isExit  = panel.isExit();
        this.isHelp=panel.getIsHelp();
    }

    public void showMyFrame(){
        this.add(this.panel);
        x=Toolkit.getDefaultToolkit().getScreenSize().width;
        y=Toolkit.getDefaultToolkit().getScreenSize().height;
        this.setBounds((x-880)/2,(y-580)/2,880,580);
        this.setUndecorated(true);
        this.setIconImage(new ImageIcon("image/icon.png").getImage());
        mainLabel=new JLabel(new ImageIcon("image/bg.png"));
        min = new JButton(new ImageIcon("image/min.png"));
        close = new JButton(new ImageIcon("image/close.png"));
        close.setBounds(810, 28, 32, 32);
        close.setBorderPainted(false);
        close.setContentAreaFilled(false);
        mainLabel.add(close);
        min.setBounds(772, 28, 32, 32);
        min.setBorderPainted(false);
        min.setContentAreaFilled(false);
        mainLabel.add(min);
        min.addActionListener(this);
        close.addActionListener(this);
        this.add(mainLabel);
        this.setVisible(true);

    }


    public void update(Observable arg0, Object arg1) 
    {
        StartJPanel panel = (StartJPanel) arg1;
        //System.out.println("update" + panel.getIsStart());
        if(panel.getIsStart())
        {

            //移除mainPanel对象
            this.remove(this.panel);
            //添加Panel对象
            this.add(this.mainpanel);
            this.repaint();
        }
        if(panel.getIsHelp())
        {

            //移除mainPanel对象
            this.remove(this.panel);
            //添加helpPanel对象
            this.add(this.helppanel,BorderLayout.CENTER);
            this.repaint();
        }
    }

    public void changedPanel()
    {

    }


    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == min) {
            this.setState(JFrame.ICONIFIED);
        } else if (e.getSource() == close) {
            System.exit(0);
        }
    }
    public void setDefaultCloseOperation(boolean b) {

    }
} 

3个回答

jh416658
jh416658   2015.07.19 21:32

我感觉是设定宽度和图片不一致,导致撑了!!!

KANDANKK
KANDANKK 我看一下吧
2 年多之前 回复
sinian12345678
sinian12345678   2015.07.19 22:11

你的GameJPanel类中的构造函数中有一行是设置了panel的长和宽的,就是this.setBounds(40,65,800,480);当主窗口缩小时,它依然是长800宽480的窗口,不会变的。望采纳!

KANDANKK
KANDANKK 不对啊,它确实最小化后这个Panel的背景放在了主界面的背景后面,它本来应该嵌入在主界面的背景后面
2 年多之前 回复
tongyi55555
tongyi55555   2015.07.19 22:39

恢复后的GamePanel大小变成了主面板的大小了。原因不知道。

Csdn user default icon
上传中...
上传图片
插入图片