gpt4 book ai didi

java - 在 Java 中画一条线 - 线不可见

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:57:56 25 4
gpt4 key购买 nike

我正在自学 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(或将多个 LayoutManager 嵌套在一起)和/或覆盖 getPreferredSize() >JComponent 返回适合图纸的正确尺寸。

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

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

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

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

enter image description here

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

关于java - 在 Java 中画一条线 - 线不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14369981/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com