Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 面板中的定心线_Java_Swing_Text_Graphics_Java 2d - Fatal编程技术网

Java 面板中的定心线

Java 面板中的定心线,java,swing,text,graphics,java-2d,Java,Swing,Text,Graphics,Java 2d,我正在试着把一根线放在面板的中心 目前我正在这样做: public void paintComponent(Graphics g) { super.paintComponent(g); int stringWidth = 0; int stringAccent = 0; int xCoordinate = 0; int yCoordinate = 0; // g

我正在试着把一根线放在面板的中心

目前我正在这样做:

public void paintComponent(Graphics g) {
        super.paintComponent(g);
            int stringWidth = 0;
            int stringAccent = 0;
            int xCoordinate = 0;
            int yCoordinate = 0;
            // get the FontMetrics for the current font
            FontMetrics fm = g.getFontMetrics();


        /** display new message */
        if (currentMessage.equals(message1)) {
            removeAll();
            /** Centering the text */
            // find the center location to display
            stringWidth = fm.stringWidth(message2);
            stringAccent = fm.getAscent();
            // get the position of the leftmost character in the baseline
            xCoordinate = getWidth() / 2 - stringWidth / 2;
            yCoordinate = getHeight() / 2 + stringAccent / 2;

            // draw String
            g.drawString(message2, xCoordinate, yCoordinate);
            currentMessage = message2;  // alternate message
        }
}
有没有一种方法可以简化这项任务

例如:

public void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (currentMessage.equals(message1)) {
                removeAll();
                // draw String centered (with one line of code)
            }
}
看起来我做了很多工作只是为了让文本居中。

试试

xCoordinate = (getWidth() - stringWidth) / 2;
yCoordinate = ((getHeight() - fm.getHeight) / 2) + stringAccent;
相反

查看以了解更多详细信息

此外,您还可以在带有GridBagLayout的JPanel上使用JLabel实现同样的功能

更新

刚刚注意到removeAll;在油漆组件中。这不是一个好主意,因为它会导致一个新的重绘请求被发布到事件队列中,将代码放入一个infinte循环,这可能会消耗CPU

更新了示例

并排比较拉丝和JLabel…拉丝在左侧,JLabel在右侧

试试像

xCoordinate = (getWidth() - stringWidth) / 2;
yCoordinate = ((getHeight() - fm.getHeight) / 2) + stringAccent;
相反

查看以了解更多详细信息

此外,您还可以在带有GridBagLayout的JPanel上使用JLabel实现同样的功能

更新

刚刚注意到removeAll;在油漆组件中。这不是一个好主意,因为它会导致一个新的重绘请求被发布到事件队列中,将代码放入一个infinte循环,这可能会消耗CPU

更新了示例

并排比较拉丝和JLabel…拉丝在左侧,JLabel在右侧


在java标准库中?不,你总可以在这方面做点什么

private void drawStringMiddleOfPanel(String string, Graphics g) {
            String message2 = string;
            int stringWidth = 0;
            int stringAccent = 0;
            int xCoordinate = 0;
            int yCoordinate = 0;
            // get the FontMetrics for the current font
            FontMetrics fm = g.getFontMetrics();


        /** display new message */
            /** Centering the text */
            // find the center location to display
            stringWidth = fm.stringWidth(message2);
            stringAccent = fm.getAscent();
            // get the position of the leftmost character in the baseline
            xCoordinate = getWidth() / 2 - stringWidth / 2;
            yCoordinate = getHeight() / 2 + stringAccent / 2;

            // draw String
            g.drawString(message2, xCoordinate, yCoordinate);
            currentMessage = message2;  // alternate message
        }


在java标准库中?不,你总可以在这方面做点什么

private void drawStringMiddleOfPanel(String string, Graphics g) {
            String message2 = string;
            int stringWidth = 0;
            int stringAccent = 0;
            int xCoordinate = 0;
            int yCoordinate = 0;
            // get the FontMetrics for the current font
            FontMetrics fm = g.getFontMetrics();


        /** display new message */
            /** Centering the text */
            // find the center location to display
            stringWidth = fm.stringWidth(message2);
            stringAccent = fm.getAscent();
            // get the position of the leftmost character in the baseline
            xCoordinate = getWidth() / 2 - stringWidth / 2;
            yCoordinate = getHeight() / 2 + stringAccent / 2;

            // draw String
            g.drawString(message2, xCoordinate, yCoordinate);
            currentMessage = message2;  // alternate message
        }


将文本放在标签中“更简单”,因为标签返回首选大小。。如图所示。为了更快地获得更好的帮助,请发布一个。将文本放入标签中“更简单”,因为标签返回首选大小。。如图所示。为了更快地获得更好的帮助,请发布一个。
   public void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (currentMessage.equals(message1)) {
            removeAll();
            int x=  (panel.getWidth() / 2) - str.length()/2;
            int y = (panel.getHeight() / 2);
            graphics.drawString(str, x, y);

        }
   }