编程语言
首页 > 编程语言> > 在Java中绘制一条线 – 线是不可见的

在Java中绘制一条线 – 线是不可见的

作者:互联网

我自己学习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);

    }

}

为什么我看不到这条线?

解决方法:

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

你应该在这里阅读:

> Laying Out Components Within a Container

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

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

3)不需要通过getContentPane()添加到contentPane.添加(..)作为add(..)setLayout(..)和remove(..)已被转发到contentPane.

4)观看类名,坚持java约定一个类名以大写字母开头,之后每个新单词的第一个字母应该是capitilized,即gameContainer应该是GameContainer,drawingLine应该是DrawingLine

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

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);
    }
}

标签:paintcomponent,java,swing,css-position,jpanel
来源: https://codeday.me/bug/20190723/1510727.html