Java JFrame 边界 [英] Java JFrame Boundaries

查看:27
本文介绍了Java JFrame 边界的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写执行以下操作的代码:如果我单击起始位置为 (100,100) 的字符串 C(JLabel),该字符串将在 JFrame 的边界内移动. 代码本身并不难实现,但我遇到了问题为 JLabel 设置 (x,y) 以便字符串C"的任何部分都不会被截断.

I'm trying to write a code that does the following: If I click on the String C(JLabel) whose starting position is (100,100), the String moves WITHIN the boundaries of JFrame. The code itself wasn't hard to implement but I'm having issues with setting the (x,y) for JLabel so that any Part of the String "C" doesn't get cut off.

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class adfadf extends JFrame{
    JLabel text = new JLabel("C");
    Container container = getContentPane();
    public adfadf(){
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        container.setLayout(null);
        MyMouseListener mml = new MyMouseListener();
        text.addMouseListener(mml);
        text.setLocation(100,100);
        text.setSize(30,30);
        add(text);
        setSize(400,400);
        setVisible(true);
    }


    public static void main(String[] args) {
        new adfadf();

    }

}
class MyMouseListener extends MouseAdapter{
    @Override
    public void mouseClicked(MouseEvent e){
        JLabel text = (JLabel)e.getSource();
        int x = (int)(Math.random()*(400-30));
        int y = (int)(Math.random()*(400-30));
        text.setLocation(x,y);
    }

}

我该怎么改

int x = (int)(Math.random()*(400-30));
int y = (int)(Math.random()*(400-30));

为了实现我想要的?

推荐答案

首先,理解 JFrame 比看起来复杂得多

First, understanding that a JFrame is much more complex then it seems

首先,JFrame 有一个 JRootPane,它包含 contentPaneJMenuBar>玻璃窗格

To start with, a JFrame has a JRootPane, that contains the contentPane and JMenuBar and glassPane

由于窗口的装饰实际上是在框架的可见边界内绘制的,这使情况变得更加复杂,这意味着您的内容可用的可见区域实际上小于框架的大小.

This is further complicated by the fact the window's decorations are actually painted WITHIN the visible bounds of the frame, meaning that the visible area available to your content is actually smaller than the frame's size.

你可以看看如何设置中间?标题栏中的图形渲染如何获得屏幕的准确中间,即使重新调整大小以获取更多详细信息和示例.

You can have a look at How can I set in the midst?, Graphics rendering in title bar and How to get the EXACT middle of a screen, even when re-sized for more details and examples of this.

但这对您有什么帮助?好吧,现在您知道您有一个小于 400x400 的空间来显示您的标签,但有多少?

But how does this help you? Well, now you know that you have a space of less than 400x400 to display your label in, but how much?

简单的解决方案是停止使用魔法"数字,并查看框架使用的内容,contentPane.contentPaneJFrame(通过 JRootPane)管理,因此它位于框架装饰中,因此您可以做更多类似的事情...

The simple solution is to stop using "magic" numbers, and take a look at something which is been used by the frame, the contentPane. The contentPane is managed by the the JFrame (via the JRootPane) so that it sits within the frame decorations, so you could do something more like...

JLabel text = (JLabel)e.getSource();
int width = getContentPane().getSize().width;
int height = getContentPane().getSize().height;
int x = (int)(Math.random()*(width-30));
int y = (int)(Math.random()*(height-30));
text.setLocation(x,y);

在这个例子中查看 contentPane 的原因很简单,因为它是实际添加标签的容器.

The reason for looking at the contentPane in this instance is simply because, that's the container that the label is actually added to.

这就是我们建议您不要使用幻数"的原因之一,而是在需要时查看实际已知值.

This is one of the reasons why we suggest you don't use "magic" numbers, but look at the actual known values at the time you need them.

这篇关于Java JFrame 边界的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆