简体   繁体   English

Java GUI:使坐标正确对齐

[英]Java GUI: Making coordinates align properly

I am making a simple Java Swing GUI chessboard where the player can drag and drop pieces. 我正在制作一个简单的Java Swing GUI棋盘,玩家可以在其中拖放棋子。 The problem is that, because of the border around the frame (with the title on top, maximize/minimize/close buttons, etc), the coordinates are skewed off - (0, 0) is the upper-left-hand corner of the frame, that is, a little above the X button, but the GUI starts building itself right below the title bar, so the GUI doesn't align with the coordinates, and things do not end up working the way they should. 问题在于,由于框架周围的边框(标题位于顶部,最大化/最小化/关闭按钮等),因此坐标偏斜了-(0,0)是坐标的左上角框架,即X按钮上方一点点,但是GUI在标题栏的正下方开始建立自身,因此GUI与坐标不对齐,并且事情最终没有按应有的方式进行。 Additionally, when I set the size of the frame to, for instance, 100 x 100, the lower part and some of the right-hand part of my GUI is cut off because the frame doesn't compensate for its border. 另外,当我将框架的大小设置为例如100 x 100时,GUI的下部和右侧部分会被切除,因为框架无法补偿其边界。 When I run it as an applet, I don't have this problem, but I don't want to do that. 当我将其作为applet运行时,我没有这个问题,但是我不想这样做。 How can I either get rid of that border around my frame window so I can just have the plain GUI, or have the coordinates set themselves up properly? 如何摆脱框架窗口周围的边框,以便只使用普通的GUI或正确设置坐标?

sscce: SSCCE:

import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;

public class class1 extends JFrame{
    public class1(){
        addMouseListener(new MouseAdapter(){
            public void mousePressed(MouseEvent evt){
                System.out.print(evt.getPoint());
            }
        });
    }

    public static void main(String[] args){
        class1 c = new class1();
        c.setTitle("Test");
        c.setSize(320, 320);
        c.setLocationRelativeTo(null);
        c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        c.setVisible(true);
    }
}

It's hard to know what is wrong with your code without the code, but I do know that if you go the easy way by using various layout managers, and let these managers do the laying out of components for you and the sizing of things as well, including calling pack() on the JFrame, usually things fall easily and well into place. 没有代码很难知道您的代码出了什么问题,但是我确实知道,如果您通过使用各种布局管理器来轻松进行操作,并让这些管理器为您进行组件布局和事物的大小调整, ,包括在JFrame上调用pack() ,通常情况很容易就可以解决。 So again, don't set the size of anything, but rather let the components' preferred sizes and the layout managers do this for you. 同样,不要设置任何大小,而是让组件的首选大小和布局管理器为您完成。

If this advice doesn't help, please give us more information and code, preferably an sscce , a small compilable and runnable program that doesn't do anything other than demonstrate your problem. 如果此建议无济于事,请给我们提供更多信息和代码,最好是sscce ,一个可编译且可运行的小型程序,除了演示您的问题外别无其他。

Edit: I am assuming that this is a Swing GUI. 编辑:我假设这是一个Swing GUI。 Please verify if this is so. 请验证是否是这样。

Edit 2: One problem you're having is that you're setting the size of a JFrame not taking into account its "decorations" including the menu bar, the resize/maximize/close icon. 编辑2:您遇到的一个问题是您在设置JFrame的大小时没有考虑其“装饰”,包括菜单栏,调整大小/最大化/关闭图标。 Again, you shouldn't be setting sizes directly, but if you must better override the getPreferredSize() method of the JPanel that holds your grid. 同样,您不应该直接设置大小,但是如果您必须更好地重写保存网格的JPanel的getPreferredSize()方法。

Edit 3: For example: 编辑3:例如:

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

public class Grid extends JPanel {
   public static final Color DARK_COLOR = Color.red.darker().darker().darker();
   public static final Color LIGHT_COLOR = Color.lightGray.brighter();
   public static final int SQUARE_SIDE = 60;
   private static final int ROW_COUNT = 8;

   @Override
   public Dimension getPreferredSize() {
      return new Dimension(ROW_COUNT * SQUARE_SIDE, ROW_COUNT * SQUARE_SIDE);
   }

   @Override
   protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      for (int i = 0; i < ROW_COUNT; i++) {
         for (int j = 0; j < ROW_COUNT; j++) {
            Color c = (i % 2 == j % 2) ? LIGHT_COLOR : DARK_COLOR;
            g.setColor(c);
            int x = i * SQUARE_SIDE;
            int y = j * SQUARE_SIDE;
            g.fillRect(x, y, SQUARE_SIDE, SQUARE_SIDE);
         }
      }
   }

   public Grid() {
      // TODO Auto-generated constructor stub
   }

   private static void createAndShowGui() {
      Grid mainPanel = new Grid();

      JFrame frame = new JFrame("Grid");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }

}

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

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