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布局.

你应该在这里阅读:

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值