天天看點

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