问题描述
- 为什么最小化后Panel就超出主界面范围
-
Panelpublic 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) { } }
解决方案
我感觉是设定宽度和图片不一致,导致撑了!!!
解决方案二:
你的GameJPanel类中的构造函数中有一行是设置了panel的长和宽的,就是this.setBounds(40,65,800,480);当主窗口缩小时,它依然是长800宽480的窗口,不会变的。望采纳!
解决方案三:
恢复后的GamePanel大小变成了主面板的大小了。原因不知道。
时间: 2024-09-22 15:16:23