简体   繁体   English

Java - Swing Layout + AWT - 如何在使用布局定位的 JPanel 中绘制一条线?

[英]Java - Swing Layout + AWT - How to draw a line in a JPanel that was positioned with layouts?

For the purpose of another program I need to draw a few lines on the screen which has a image built with incremental painting and layouts.为了另一个程序的目的,我需要在屏幕上画几条线,其中有一个用增量绘画和布局构建的图像。 Since the lines were very straight forward and just a few, I thought using the JPanel's painting space to do this.由于线条非常直,只有几条,我想使用JPanel的绘画空间来做到这一点。 Since this was not working, I made an testing program to try to make it work, but I still can't seem to figure out why this isn't working.由于这不起作用,我制作了一个测试程序来尝试使其工作,但我似乎仍然无法弄清楚为什么这不起作用。

Here is my testing program:这是我的测试程序:

import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JFrame;

import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.Dimension;


class TestMain{
public static void main(String[]args){
    TestingComponent tester = new TestingComponent();
}
}
class Block extends JPanel{
Block(){
        setOpaque(true);
}
public void paintComponent(Graphics gr){
    super.paintComponent(gr);
    if(getBackground() == Color.white){
        System.out.println("Reached");
        gr.drawLine(getX(), getY(), getX() + getWidth(), getY() + getHeight());
    }
}
}
class TestingComponent{
TestingComponent(){
    JFrame frmMain = new JFrame("testing");
    frmMain.setSize(Toolkit.getDefaultToolkit().getScreenSize());
    JPanel pnlMain = new JPanel();
    Block block = new Block();
    Dimension dmsDim = new Dimension(16, 16);
    pnlMain.setPreferredSize(dmsDim);
    frmMain.add(pnlMain);
    pnlMain.setLayout(new GridLayout(2, 2));
    for(int index = 0; index < 4; index++){
        block = new Block();
        switch(index){
            case 0:
                block.setBackground(Color.black);
                break;
            case 1:
                block.setBackground(Color.blue);
                break;
            case 2:
                block.setBackground(Color.green);
                break;
            case 3:
                block.setBackground(Color.white);
                break;
        }
        pnlMain.add(block);
    }
    frmMain.setVisible(true);
}
}

Versions/Programs I'm using (in the testing program) -我正在使用的版本/程序(在测试程序中)-

Java 8爪哇 8

Notepad记事本

Command Prompt命令提示符

Seeing how simple the drawLine method is, I'm almost certain it is due to the layouts, but other than that one point, I haven't a clue why the line isn't drawing.看到 drawLine 方法是多么简单,我几乎可以肯定这是由于布局造成的,但除了这一点之外,我不知道为什么没有绘制线条。 The System.out.println("Reached"); System.out.println("到达"); is being out-putted, so the program definitely reachs the gr.drawLine().正在输出,因此程序肯定会到达 gr.drawLine()。

To clearly state the question... Why isn't the line being drawn?为了清楚地陈述问题......为什么不画线? How do I fix this?我该如何解决?

you are using the wrong points to draw your line您使用错误的点来画线

getX() and getY() return the location of your Component ( Block ) on the parent component. getX()getY()返回您的组件 ( Block ) 在父组件上的位置。

and because you divide your drawings into two pieces, getX() returns the very right coordinate of your panel, getY() returns the very upper coordinate.因为您将绘图分成两部分,所以getX()返回面板的最右侧坐标, getY()返回最上方的坐标。

use gr.drawLine(0, 0, getWidth(), getHeight());使用gr.drawLine(0, 0, getWidth(), getHeight()); to draw the lines画线

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

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