为什么Jframe size SOME TIME会自行更改?

时间:2017-07-05 00:21:28

标签: java swing netbeans contentpane

我正在尝试创建一个自动跟踪器,但我遇到了一个随机问题 我做了退出"退出"使用 .getContentPane()生成右下角的按钮.getSize()
但在我的基本" Jframe的值有些变化(我在打开时添加了系统打印 第一个值是 java.awt.Dimension [width = 284,height = 462]
 第二个是 java.awt.Dimension [width = 294,height = 472] (这是正确的)
有人可以解释为什么会发生这种情况,因为我的思绪被吹了 我正在使用NetBeans 这不是我的完整代码:
主要

/*
 * To change this license header, choose License Headers in Project 
Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
 package test;

 import java.awt.AWTException;
 import java.awt.Color;
 import java.awt.Dimension;
 import java.awt.Toolkit;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.io.IOException;
 import javax.swing.JButton;
 import javax.swing.JComponent;
 import javax.swing.JFrame;
 import javax.swing.JPanel;

 /**
  *
  * @author Xaralabos
  */
 public class Test {

/**
 * @param args the command line arguments
 */



static void placeAtThe (JComponent a, String where, Dimension framesize){

    if((where == "bottom")||(where == "Bottom")||(where == "bot")||(where == "Bot")){
        a.setLocation(a.getX(),(framesize.height - a.getHeight()));
    }
    else if ((where == "right")||(where == "Right")){
        a.setLocation(framesize.width - a.getWidth(), a.getY());
    }
}

    // Center Align - Width (needs width of frame and of component)
static int CW (int framew, int compw){
    int c = (((framew) / 2) - (compw / 2));
    return c;
}


// CREATE NEW FRAME FOR Basic Clicker
 static class BasicClickerFrame implements ActionListener{
private JFrame Homef;
private boolean StopCopies;

public BasicClickerFrame(JFrame Homef){
    this.Homef = Homef;
    StopCopies = false;
}

public void actionPerformed(ActionEvent e) {
    Homef.dispose();
    if (StopCopies == false){
        StopCopies = true;
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        double width = screenSize.getWidth();
        double height = screenSize.getHeight();
        int basicw = 300;
        int basich = 500;

        // FRAME , SIZE - SPAWN LOCATION
        JFrame Basic = new JFrame("Basic Auto Clicker");
        Basic.setResizable(false);
        Basic.setSize(basicw, basich);
        Basic.setLocation(((int) (width / 4)) - (Basic.getWidth() / 2), ((int) height / 10));
        Basic.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


            // COLORING THE BACKGROUND
        JPanel contentPane = new JPanel();
        contentPane.setBackground(Color.WHITE);
        contentPane.setLayout(null);
            // add more code here
        Basic.setContentPane(contentPane);


            // EXIT BUTTON (CLOSES WINDOW, NOT PROGRAM)
        JButton Exit = new JButton("Exit");
        Exit.setSize(Exit.getPreferredSize());
            // add more code here
        contentPane.add(Exit);


            // button for getting keybind for starting
        JButton Getme = new JButton("Click Me");
        Getme.setSize(Getme.getPreferredSize());
            // add more code here
        contentPane.add(Getme);


        // SPAWN EVERYTHING
        Basic.setVisible(true);

        // ADD LOCATIONS HERE
        placeAtThe(Exit, "right", Basic.getContentPane().getSize());
        placeAtThe(Exit, "bot", Basic.getContentPane().getSize());
        Getme.setLocation(CW(Basic.getContentPane().getSize().width, Getme.getWidth()), 50);
        System.out.println(Basic.getContentPane().getSize());
    }
   }
  }


public static void main(String[] args) throws IOException, AWTException {
    int frame1w = 600;
    int frame1h = 400;
        // MAIN WINDOW - SIZE - SPAWN LOCATION
    JFrame Homef = new JFrame("Auto Clicker");
    Homef.setResizable(false); // stops resizing
    Homef.setSize(frame1w, frame1h);
    Homef.setLocationRelativeTo(null);
    Homef.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    // COLORING BACKGROUND
    JPanel contentPane = new JPanel();
    contentPane.setBackground(Color.WHITE);
    contentPane.setLayout(null);
    // Add more code here
    Homef.setContentPane(contentPane);


    // EXIT BUTTON (CLOSES PROGRAM)
    JButton Exit = new JButton("Exit");
    Exit.setSize(Exit.getPreferredSize());
        // add code here
    contentPane.add(Exit);

        // BUTTON FOR CREATING A NEW FRAME FOR Basic AUTO CLICKER
    JButton BasicClicker = new JButton("Basic Clicker");
    BasicClicker.setSize(BasicClicker.getPreferredSize());
    BasicClicker.addActionListener(new BasicClickerFrame(Homef));
    // ADD MORE STUFF HERE
    contentPane.add(BasicClicker);

    Homef.setVisible(true);

    // ADD LOCATIONS HERE
    placeAtThe(Exit, "right", Homef.getContentPane().getSize());
    placeAtThe(Exit, "bot", Homef.getContentPane().getSize());
    BasicClicker.setLocation(CW(Homef.getContentPane().getSize().width, BasicClicker.getWidth()), BasicClicker.getY());   
  }
 }  

注意我尝试删除第一帧,然后立即打开基本帧 当我这样做的时候,问题就解决了(我想,我跑了30次没有出现问题),但是......我不知道为什么......帮助!

0 个答案:

没有答案