【问题标题】:drawing a line in Java - line is not visible用Java画一条线 - 线不可见
【发布时间】:2012-12-31 10:55:11
【问题描述】:

我正在自学 Java。尝试创建一个带有线条的框架。它看起来很基本,但我看不到这条线。代码编译,我似乎无法理解为什么我看不到这条线。我在框架中看到了其他组件。 我正在使用 2 个 java 文件。一个文件是容器文件,另一个文件有画线代码。它们是包 a1 的一部分。 这是我的代码(请帮助):

容器文件:

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

public class gameContainer {

    public static void addComponentsToPane(Container pane) {
        pane.setLayout(null);

        //add button to the pane
        JButton b3 = new JButton("B1");
        pane.add(b3);

        //add line to the pane
        drawingLine line1 = new drawingLine();
        pane.add(line1);

        //size and position all relatively          
        Insets insets = pane.getInsets();
        Dimension size;
        size = b3.getPreferredSize();        
        b3.setBounds(350+insets.left,15+insets.top,size.width+50,size.height+20);
        size = line1.getPreferredSize();
        line1.setBounds(350+insets.left,75+insets.top,size.width+50,size.height+20);    

    }

    private static void createAndShowGUI() {
        int l = 200, w = 80;

        //Create and set up the window.
        JFrame frame = new JFrame("Frame");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Set up content pane
        addComponentsToPane(frame.getContentPane());

        //add menu bar
        JMenuBar menuBar = new JMenuBar();
        JMenu menu = new JMenu("Menu");
        menu.add(new JMenuItem("Do nothing"));
        menuBar.add(menu);
        frame.setJMenuBar(menuBar);

        // size and display root pane/window
        Insets insets = frame.getInsets();
        frame.setSize(500+insets.left+insets.right,300+insets.top+insets.bottom);
        frame.setLocation(w,l);
        frame.setVisible(true);
    }


    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI();
        }
        });
    }

}

画线文件:

package a1;
import javax.swing.*;
import java.awt.geom.Line2D;
import java.awt.Graphics2D;
import java.awt.Graphics;

public class drawingLine extends JPanel{

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        Line2D line = new Line2D.Double(200, 300, 1000, 1000);
        //g2d.setColor(Color.black);
        g2d.draw(line);
        //g.drawLine(200, 300, 1000, 1000);
        //g.setColor(color.BLACK);

    }

}

为什么我看不到这条线?

【问题讨论】:

  • 我尝试在使用 g2d.draw(line) 之前使用 g2d.drawImage 在 DrawingLine 的线条下添加图像。它不工作。我只看到线。我想在线条的背景中看到图像。

标签: java swing jpanel paintcomponent graphics2d


【解决方案1】:

您的主要问题是您使用null/Absolute 布局。

你应该在这里阅读:

1) 您应该使用适当的LayoutManager(或将多个LayoutManagers 嵌套在一起)和/或覆盖JComponent 中的getPreferredSize(),以返回适合图纸的正确尺寸。

2) 不要在JFrame 上调用setSize,而是在设置JFrame 可见之前调用pack()(记住以上内容)

3) 无需通过 getContentPane().add(..) 添加到 contentPane,因为 add(..) setLayout(..) 和 remove(..) 已转发到 contentPane。

4) 注意类名,遵守 java 约定,类名以大写字母开头,之后的每个新单词的第一个字母应大写,即gameContainer 应为GameContainer,drawingLine 应为@987654340 @

这是您实现了上述修复的代码(不是最好的布局,但它只是一个示例):

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;


public class GameContainer {

    public static void addComponentsToPane(JFrame frame) {

        JPanel buttonPanel=new JPanel();//create panel to hold button
        //add button to the pane
        JButton b3 = new JButton("B1");
        buttonPanel.add(b3);
        frame.add(buttonPanel, BorderLayout.EAST);//use contentPane default BorderLayout

        //add line to the pane
        DrawingLine line1 = new DrawingLine();
        frame.add(line1);

    }

    private static void createAndShowGUI() {

        //Create and set up the window.
        JFrame frame = new JFrame("Frame");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Set up content pane
        addComponentsToPane(frame);

        //add menu bar
        JMenuBar menuBar = new JMenuBar();
        JMenu menu = new JMenu("Menu");
        menu.add(new JMenuItem("Do nothing"));
        menuBar.add(menu);
        frame.setJMenuBar(menuBar);

        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}
class DrawingLine extends JPanel {

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        Line2D line = new Line2D.Double(10, 10, 100, 100);
        g2d.draw(line);

    }

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

【讨论】:

  • 是的,就是这样。我以为我正在适当地调整框架中的所有组件。我想我现在应该只使用 LayoutManager。稍后会回到这个问题。谢谢大卫·克鲁坎普。
  • @jdek 很高兴您知道问题所在。查看我的更新答案
  • 哇。谢谢你。 +1。在您第一次发表评论后,我确实使用了 pack()。我有一些后续问题,但这个更新的答案已经解决了这一切。谢谢。
【解决方案2】:

David Kroukamp 展示了正确的(更好地说,更简单,更不容易出错)的方法。至于你原来的问题,修改原代码如下:

line1.setBounds(350+insets.left,75+insets.top,size.width+50,size.height+20);    
line1.setBorder(BorderFactory.createEtchedBorder()); // add this line

您的线从点 200,300 开始,但您绘制它的面板的宽度为 50+10,高度为 20+10 - 至少在我的计算机上 - 这意味着该线在 drawingLine 面板之外这就是为什么它没有被绘制。要验证修改原始drawingLine.paintComponent 中的行,如下所示:

Line2D line = new Line2D.Double(0, 0, 1000, 1000);

你会看到如下结果:

原始代码中的这一行:

pane.setLayout(null);

很清楚-您没有使用布局管理器,或者换句话说,您选择使用absolute positioning。这意味着组件的坐标、宽度和高度必须由您预先计算和设置。但是,如果您在某处犯了错误(正如您的示例很好地展示的那样),则很难检测到。更不用说,例如,如果您想处理窗口大小调整。这就是 LayoutManagers 存在的原因。

我也推荐阅读:

【讨论】:

  • 谢谢林斯基。我了解绝对定位所涉及的问题。计算组件坐标的麻烦是不值得的。现在坚持使用 BorderLayout。
猜你喜欢
  • 2015-03-14
  • 1970-01-01
  • 1970-01-01
  • 2016-07-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-08
  • 2014-12-27
相关资源
最近更新 更多