简体   繁体   English

在JFrame内的JPanel中设置JLabel位置

[英]Set JLabel location inside JPanel thats inside JFrame

I have a JFrame that is filled with is JPanel (code below). 我有一个用JPanel填充的JFrame(下面的代码)。
I'm using the JPanel to draw stuff in it , for example i can draw lines anywhere however i like , but when adding JLabel to it i can't move it anywhere it is stuck in top center like this thread . 我正在使用JPanel在其中绘制东西,例如我可以在我喜欢的任何地方画线,但是当将JLabel添加到它时,我不能将它移动到像这样的线程那样停留在顶部中心的任何地方。
but the problem is the solution they suggested there doesn't work for me . 但是问题在于他们建议的解决方案对我不起作用。
i tried adding : 我尝试添加:

 getContentPane().setLayout(null);

sorry if i wasn't clear , i tried adding the above in the function initUI() before and after creating Surface. 抱歉,如果我不清楚,我尝试在创建Surface之前和之后在函数initUI()中添加以上内容。
but after that the frame shows with almost (1,1) size and its empty (if i manually resize it using mouse drag) . 但是在那之后,框架显示几乎(1,1)的大小,并且它是空的(如果我使用鼠标拖动手动调整大小)。

this is my code : 这是我的代码:
WaveDrawerMain class WaveDrawerMain类

public class WaveDrawerMain extends JFrame {

private static WaveDrawerMain wd;
private Surface s;
public WaveDrawerMain() {
    initUI();
    s.drawLinexxx();
}
private void initUI() {

    setupMenu();
    s = new Surface(1200, 300); 
    add(s);
    setTitle("Waves");
    setSize(1200, 300);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    pack();
}
private static void constructDrawer() {
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            wd = new WaveDrawerMain();
            wd.setVisible(true);
        }
    });
}
public static WaveDrawerMain createDrawer(){
    constructDrawer();
    idPool = new IntPool(100);
    lines = new LinesManager();
    return wd;
}
}

Surface class : 表面类别

class Surface extends JPanel {

private ArrayList<Integer> points = new ArrayList<Integer>();
private int pWidth, pHeight;
@Override
public void paintComponent(Graphics g) {

    super.paintComponent(g);
    doDrawing(g);

}
@Override
public Dimension getPreferredSize() {
    return new Dimension(pWidth, pHeight);
}

public Surface(int width, int height) {
    pWidth = width;
    pHeight = height;


}

private void doDrawing(Graphics g) {

    Graphics2D g2d = (Graphics2D) g;

    g2d.drawLine(30, 30, 200, 30);
    for(int i = 0;i<points.size();i++){
        g2d.drawLine(points.get(i), 100, points.get(i)+50, 100+50);
    }
}

public void drawLinexxx(){
    points.add(600);
    JLabel lab1 = new JLabel("Test x "+Float.toString(getSize().width/3)+" y "+Float.toString(getSize().height/3));
    lab1.setLocation(getSize().width/3, getSize().height/3);

    add(lab1);
    repaint();
}
}

but when adding JLabel to it i can't move it anywhere 但是在添加JLabel时我无法将其移动到任何地方

The default layout manager for a JPanel is a FlowLayout . JPanel的默认布局管理器是FlowLayout When you add components to the panel they are positioned based on the rules of the layout manager. 将组件添加到面板时,它们将根据布局管理器的规则进行定位。

getContentPane().setLayout(null);

You don't want to set the content pane layout null, you want to set the Surface panel layout to null. 您不想将内容窗格的布局设置为null,而是要将“ Surface面板的布局设置为null。

However, when you do this you are now responsible for managing the functions of the layout manager which means once you add the label to the Surface panel you are responsible for setting the size and location of the label. 但是,当您执行此操作时,您现在将负责管理布局管理器的功能,这意味着一旦将标签添加到“表面”面板后,您将负责设置标签的大小和位置。 The location and size will both default to (0, 0). 位置和大小都将默认为(0,0)。

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

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