简体   繁体   English

尽管边界在大小范围内,但JButton部分地被绘制在窗口之外

[英]JButton is drawn partially outside of window despite bounds being within size

I am a student trying to figure out the basics of Java's graphics. 我是一名学生,试图弄清楚Java图形的基础知识。 My class creates a window where a single red button will move every time it is pressed. 我的班级创建了一个窗口,每次按下一个红色按钮就会移动。 I set the bounds of the window to 720x720 and the button is 50 pixels wide. 我将窗口的边界设置为720x720,按钮宽度为50像素。 Everytime it is pressed, the button goes to a new x and y coordinate that is between 0 and 670. My understanding is that if the setBounds() method is called with the parameters (670,670,50,50) then my red button will occupy the bottom right corner of the window. 每次按下它时,按钮都会转到0到670之间的新x和y坐标。我的理解是,如果使用参数(670,670,50,50)调用setBounds()方法,那么我的红色按钮将占用窗口的右下角。

Unfortunately, the button seems to be going outside of the window even when the bounds are set to something like (660,660,50,50). 不幸的是,即使边界被设置为类似(660,660,50,50),按钮似乎也会出现在窗口之外。

Problem 问题

I have tried tracking the bounds by printing every change in coordinates but this still does not add up. 我已经尝试通过打印坐标中的每个更改来跟踪边界,但这仍然没有加起来。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Custom extends JPanel
{
   //fields
   private int kovakx = 0, kovaky = 0; //stores x and y coordinates of the button
   private double accuracy_percent = 100; //not used yet

   private JButton kovak = new JButton();

   //Only ActionListener this program needs since theres only one button that moves around the screen.
   private class Elim implements ActionListener
   {
      public void actionPerformed(ActionEvent e)
      {
         kovakx = (int)(Math.random()*670);
         kovaky = (int)(Math.random()*670);
         kovak.setBounds(kovakx,kovaky,50,50);
         System.out.println(kovakx+","+kovaky);//prints out the new coordinates of the button
      }
   }

   //Constructor sets icon and initally puts the button in the top left corner
   public Custom()
   {
      kovak.setIcon(new ImageIcon("Target.png"));
      setLayout(null);
      kovak.setBounds(0,0,50,50);
      kovak.addActionListener(new Elim());
      add(kovak);
   }

   //Creates frame based on teacher's tutorials.
   public static void main(String args[])
   {
      Custom a = new Custom();
      JFrame f = new JFrame("Reaction and Accuracy Test");
      f.setSize(720,720);
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      f.add(a);
      f.setVisible(true);
   } 
}

You set the frame size, but are never setting the size on Custom. 您可以设置帧大小,但永远不会在自定义上设置大小。 If you call getSize() on Custom, it returns (0,0). 如果在Custom上调用getSize(),则返回(0,0)。 So what is happening I think is that the frame really is (720,720) in size, but that includes the area of the decorations (such as title bar) and frame borders. 所以我认为框架的大小确实是(720,720),但包括装饰区域(如标题栏)和框架边框。 The effective area inside the frame is smaller, which is why you cannot see the entire button. 框架内的有效区域较小,这就是您无法看到整个按钮的原因。

The solution is either to determine the actual area inside the frame, or to adjust the frame so that the area inside is (720,720). 解决方案是确定框架内的实际区域,或者调整框架以使其内部区域为(720,720)。

To adjust the frame so the inner size is (720,720), the usual way is to set the component size and then pack the frame: 要调整框架以使内部尺寸为(720,720),通常的方法是设置组件尺寸然后打包框架:

In main, 主要的,

f.add(a);
a.setPreferredSize(720,720);   // set desired size of JPanel
f.pack();                      // resize frame to fit around its contents
f.setVisible(true);

It is not necessary to call frame.setSize() if you are calling pack(), because pack will set the size automatically. 如果调用pack(),则无需调用frame.setSize(),因为pack会自动设置大小。 Note that it you call pack without calling setPreferredSize(), the frame will be a tiny zero-size frame that may be hard to see. 请注意,你调用pack而不调用setPreferredSize(),框架将是一个很小的零尺寸框架,可能很难看到。 If you run the program and don't see anything that is the likely problem. 如果您运行该程序,并没有看到任何可能的问题。

Getting the frame size without the decorations is a little more complicated. 获得没有装饰的框架尺寸有点复杂。 See the part about getInsets() in JFrame: get size without borders? 请参阅JFrame中关于getInsets()的部分:获取无边框的大小?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM