简体   繁体   English

如何在java中将JPanel添加到JFrame

[英]how to add a JPanel to a JFrame in java

In this code I have created a JFrame to display the x and y points of the spot the users have clicked.在这段代码中,我创建了一个 JFrame 来显示用户单击的位置的 x 和 y 点。 As you can see in the example the y point is a really high number because it is taking the title bar height into consideration.正如您在示例中看到的那样,y 点是一个非常高的数字,因为它考虑了标题栏的高度。 How can i get it so the top left corner that is clickable is 0,0?我怎样才能得到它,所以可点击的左上角是 0,0?

Right now (0,0) is out of reach and i'm not to sure how to incorporate the JPanel in order to be able to reach that point.现在 (0,0) 遥不可及,我不确定如何合并 JPanel 才能达到这一点。

I can further explain my code or clarify if needed如果需要,我可以进一步解释我的代码或澄清

例子

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


class Proj07Runner {
    Proj07Runner() { 
        System.out.println(
          "Terminal");
       new GUI();
    }
} 

class MyFrame extends Frame {
  int clickX;
  int clickY;
  public void paint(Graphics g) {
      g.drawString(" " + clickX + ", " + clickY, clickX, clickY);
  } 
} 




class GUI { 
  public GUI(){ // constructor - set up frame
    MyFrame frame = new MyFrame();
    frame.setSize(300,100);
    frame.setTitle("xxx");
    frame.setVisible(true);
    
    // window listent to allow users to close frame 
    frame.addWindowListener(new Close());

    // mouse listener so we can print points for mouse clicks
    frame.addMouseListener(new MouseProc(frame));

    // JPanel
    JPanel panel = new JPanel();
    frame.add(panel);
    } 
} 

// monitors closing window
class Close extends WindowAdapter{
  public void windowClosing(WindowEvent e){
    System.exit(0);
  }
}

// Monitor mouse clicks and gather x and y points
class MouseProc extends MouseAdapter {
    MyFrame reference; 
    MouseProc(MyFrame winIn) { 
      reference = winIn;
    } 
    
    @Override
    public void mousePressed(MouseEvent e) {
      reference.clickX = e.getX();
      reference.clickY = e.getY();
      reference.repaint();
    }
} 

How can i get it so the top left corner that is clickable is 0,0?我怎样才能得到它,所以可点击的左上角是 0,0?

Add the MouseListener to the panel (not the frame).将 MouseListener 添加到面板(而不是框架)。 The mouse points are relative to the component you add the listener to.鼠标点与您添加侦听器的组件相关。

Any custom painting should then be done by:然后应通过以下方式完成任何自定义绘画:

  1. extend your JPanel扩展您的 JPanel
  2. overriding the paintComponent(...) method, not paint(...).覆盖paintComponent(...)方法,而不是paint(...)。

Read the Swing tutorial on Custom Painting for more information and working example, including an example that uses a MouseListener.阅读有关自定义绘画的 Swing 教程以获取更多信息和工作示例,包括使用 MouseListener 的示例。

By nature, a jpanel has no particular size, and will just float around in the jframe without doing anything in particular.从本质上讲,jpanel 没有特定的大小,只会在 jframe 中浮动而不做任何特别的事情。 What you want it to do is expand to take up the whole frame.您想要它做的是扩展以占据整个框架。 By default frames use BorderLayout, so you need to use border layout centre, which means "take up as much space as possible"默认框架使用BorderLayout,所以需要使用border layout center,意思是“尽可能占用空间”

frame.add(panel, BorderLayout.CENTER)

You should become very very familiar with how to use BorderLayout if you want to be a good Swing programmer.如果您想成为一名优秀的 Swing 程序员,您应该非常熟悉如何使用 BorderLayout。

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

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