Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么Jframe的大小有时会自行改变?_Java_Swing_Netbeans_Contentpane - Fatal编程技术网

Java 为什么Jframe的大小有时会自行改变?

Java 为什么Jframe的大小有时会自行改变?,java,swing,netbeans,contentpane,Java,Swing,Netbeans,Contentpane,我正在尝试创建自动单击器,但遇到了一个随机问题。 通过使用.getContentPane().getSize() 但在我的“基本”Jframe上,该值有时会发生变化(我在它打开时添加了一个系统打印。 第一个值是java.awt.Dimension[width=284,height=462] 第二个是java.awt.Dimension[width=294,height=472](这是正确的) 有人能解释一下为什么会发生这种情况,因为我的脑子被炸了。 我正在使用NetBeans。 这不是我的完整代

我正在尝试创建自动单击器,但遇到了一个随机问题。
通过使用.getContentPane().getSize()
但在我的“基本”Jframe上,该值有时会发生变化(我在它打开时添加了一个系统打印。
第一个值是java.awt.Dimension[width=284,height=462]
第二个是java.awt.Dimension[width=294,height=472](这是正确的)
有人能解释一下为什么会发生这种情况,因为我的脑子被炸了。
我正在使用NetBeans。
这不是我的完整代码:
Main

/*
 * 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次,没有出现问题),但是是的……我不知道为什么……帮助!

而不是
Homef.setLocation(…)
使用
JFrame#setLocationRelativeTo
并将其传递
null
-它将在屏幕中居中显示窗口,同时考虑其他UI元素,如task barA通常更好的解决方案是使用适当的布局管理器和可能的
玻璃窗格
,谢谢提示:P…但我的问题不是它们也不是答案
contentPane.setLayout(null);
1)Java GUI必须在不同的操作系统、屏幕大小、屏幕分辨率等上工作,在不同的地区使用不同的PLAF。因此,它们不利于像素完美布局。而是使用布局管理器,或与布局填充和边框一起使用。2) 为了更快地获得更好的帮助,请发布一个or。