Drawline 无法与 JPanel 或 JFrame 一起使用
我似乎无法让这条线出现。我有一个背景颜色和一些图片。如果我有 frame.setSize(x, y);
和 frame.setVisible(true);
那么结果就如预期的那样,但那里没有线条。如果我更改代码并从这两行中删除 frame.
,留下 setSize(x,y);
和 setVisible(true);
。我尝试过使用 extends JPanel
和 extends JFrame
但都不起作用。我尝试添加和删除 @Override
、paintComponent
和 g2d.drawLine
。
无论是其中之一,我如何让两者都工作?
import javax.swing.*;
import java.awt.*;
import javax.imageio.*;
import java.io.*;
public class CodeBreaker extends JPanel
{
JFrame frame = new JFrame("Code Breaker!");
Picture picture = new Picture("Empty.png");
JLabel label = new JLabel(picture);
JLabel label2 = new JLabel(picture);
JLabel label3 = new JLabel(picture);
JLabel label4 = new JLabel(picture);
JLabel label5 = new JLabel(picture);
JLabel label6 = new JLabel(picture);
JLabel label7 = new JLabel(picture);
JLabel label8 = new JLabel(picture);
JPanel panel = new JPanel();
JPanel panel2 = new JPanel();
public CodeBreaker()
{
frame.setSize(600, 900);
frame.setContentPane(panel);
frame.add(panel2);
frame.setResizable(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setBackground(new Color(141, 100, 21));
panel.add(label);
panel.add(label2);
panel.add(label3);
panel.add(label4);
panel2.add(label5);
panel2.add(label6);
panel2.add(label7);
panel2.add(label8);
panel2.setOpaque(false);
frame.setVisible(true);
}
/*
void drawLines(Graphics g)
{
Graphics2D g2d = (Graphics2D) g;
g2d.drawLine(120, 50, 360, 50);
}
*/
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.white);
g.drawLine(120, 50, 360, 50);
}
}
这是我的主要内容:
public class CodeBreakerDriver
{
public static void main(String[] args)
{
CodeBreaker cb = new CodeBreaker();
}
}
I cant seem to get the line to appear. I have a Background color and a few pictures. If I have frame.setSize(x, y);
and frame.setVisible(true);
then the outcome is as expected but without the line there. If I change the code and remove frame.
from these two lines, leaving setSize(x,y);
and setVisible(true);
. I have tried using extends JPanel
and extends JFrame
but neither work. I have tried adding and removing @Override
, paintComponent
and g2d.drawLine
.
It either one or the other, how do I get both to work?
import javax.swing.*;
import java.awt.*;
import javax.imageio.*;
import java.io.*;
public class CodeBreaker extends JPanel
{
JFrame frame = new JFrame("Code Breaker!");
Picture picture = new Picture("Empty.png");
JLabel label = new JLabel(picture);
JLabel label2 = new JLabel(picture);
JLabel label3 = new JLabel(picture);
JLabel label4 = new JLabel(picture);
JLabel label5 = new JLabel(picture);
JLabel label6 = new JLabel(picture);
JLabel label7 = new JLabel(picture);
JLabel label8 = new JLabel(picture);
JPanel panel = new JPanel();
JPanel panel2 = new JPanel();
public CodeBreaker()
{
frame.setSize(600, 900);
frame.setContentPane(panel);
frame.add(panel2);
frame.setResizable(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setBackground(new Color(141, 100, 21));
panel.add(label);
panel.add(label2);
panel.add(label3);
panel.add(label4);
panel2.add(label5);
panel2.add(label6);
panel2.add(label7);
panel2.add(label8);
panel2.setOpaque(false);
frame.setVisible(true);
}
/*
void drawLines(Graphics g)
{
Graphics2D g2d = (Graphics2D) g;
g2d.drawLine(120, 50, 360, 50);
}
*/
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.white);
g.drawLine(120, 50, 360, 50);
}
}
And this is my main:
public class CodeBreakerDriver
{
public static void main(String[] args)
{
CodeBreaker cb = new CodeBreaker();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简介
Oracle 有一个有用的教程,使用 Swing 创建 GUI。跳过使用 NetBeans IDE 学习 Swing 部分。
由于您发布的代码不可执行,我继续创建了以下 GUI。
您可以在 GUI 的右下角看到一条黑线。
说明
所有 Swing 应用程序都必须从调用
SwingUtilities
invokeLater
方法开始。此方法确保在 事件调度线程上创建并执行 Swing 组件。在单独的方法中创建
JFrame
和JPanels
。这使得代码更容易阅读和理解。使用 Swing 布局管理器。
JFrame
有一个默认的BorderLayout
。我对两个JPanels
使用了FlowLayout
。代码
这是完整的可运行代码。我将
Picture
类设置为一个内部类,这样我就可以将代码作为一个块发布。Introduction
Oracle has a helpful tutorial, Creating a GUI With Swing. Skip the Learning Swing with the NetBeans IDE section.
Since the code you posted isn't executable, I went ahead and created the following GUI.
You can see a black line in the lower right of the GUI.
Explanation
All Swing applications must start with a call to the
SwingUtilities
invokeLater
method. This method ensures that the Swing components are created and executed on the Event Dispatch Thread.Create the
JFrame
andJPanels
in separate methods. This makes the code much easier to read and understand.Use Swing layout managers. The
JFrame
has a defaultBorderLayout
. I used aFlowLayout
for bothJPanels
.Code
Here's the complete runnable code. I made the
Picture
class an inner class so I could post the code as one block.