Drawline 无法与 JPanel 或 JFrame 一起使用

发布于 2025-01-13 02:16:22 字数 2183 浏览 2 评论 0原文

我似乎无法让这条线出现。我有一个背景颜色和一些图片。如果我有 frame.setSize(x, y);frame.setVisible(true); 那么结果就如预期的那样,但那里没有线条。如果我更改代码并从这两行中删除 frame.,留下 setSize(x,y);setVisible(true);。我尝试过使用 extends JPanelextends JFrame 但都不起作用。我尝试添加和删除 @OverridepaintComponentg2d.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

新雨望断虹 2025-01-20 02:16:22

简介

Oracle 有一个有用的教程,使用 Swing 创建 GUI。跳过使用 NetBeans IDE 学习 Swing 部分。

由于您发布的代码不可执行,我继续创建了以下 GUI。

输入图片这里的描述

您可以在 GUI 的右下角看到一条黑线。

说明

所有 Swing 应用程序都必须从调用 SwingUtilities invokeLater 方法开始。此方法确保在 事件调度线程上创建并执行 Swing 组件

在单独的方法中创建 JFrameJPanels。这使得代码更容易阅读和理解。

使用 Swing 布局管理器JFrame 有一个默认的BorderLayout。我对两个 JPanels 使用了 FlowLayout

代码

这是完整的可运行代码。我将 Picture 类设置为一个内部类,这样我就可以将代码作为一个块发布。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.image.BufferedImage;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class CodeBreakerGUI implements Runnable {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new CodeBreakerGUI());
    }
    
    private final Picture picture;
    
    public CodeBreakerGUI() {
        this.picture = new Picture();
    }

    @Override
    public void run() {
        JFrame frame = new JFrame("Code Breaker!");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        frame.add(createTopPanel(), BorderLayout.NORTH);
        frame.add(createBottomPanel(), BorderLayout.SOUTH);
        
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }
    
    public JPanel createTopPanel() {
        JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER, 25, 25));
        panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        
        JLabel label = new JLabel(new ImageIcon(picture.getEmptyImage()));
        panel.add(label);
        
        JLabel label2 = new JLabel(new ImageIcon(picture.getEmptyImage()));
        panel.add(label2);
        
        JLabel label3 = new JLabel(new ImageIcon(picture.getEmptyImage()));
        panel.add(label3);
        
        JLabel label4 = new JLabel(new ImageIcon(picture.getEmptyImage()));
        panel.add(label4);
        
        return panel;
    }
    
    public JPanel createBottomPanel() {
        JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER, 25, 25)) {
            private static final long serialVersionUID = 1L;

            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.setColor(Color.black);
                g.drawLine(120, 50, 360, 50);
            }
        };
        panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        
        JLabel label5 = new JLabel(new ImageIcon(picture.getEmptyImage()));
        panel.add(label5);
        
        JLabel label6 = new JLabel(new ImageIcon(picture.getEmptyImage()));
        panel.add(label6);
        
        JLabel label7 = new JLabel(new ImageIcon(picture.getEmptyImage()));
        panel.add(label7);
        
        JLabel label8 = new JLabel(new ImageIcon(picture.getEmptyImage()));
        panel.add(label8);
        
        return panel;
    }
    
    public class Picture {
        
        private final BufferedImage emptyImage;
        
        public Picture() {
            this.emptyImage = new BufferedImage(64, 64, BufferedImage.TYPE_INT_RGB);
        }

        public BufferedImage getEmptyImage() {
            return emptyImage;
        }
        
    }

}

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.

enter image description here

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 and JPanels in separate methods. This makes the code much easier to read and understand.

Use Swing layout managers. The JFrame has a default BorderLayout. I used a FlowLayout for both JPanels.

Code

Here's the complete runnable code. I made the Picture class an inner class so I could post the code as one block.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.image.BufferedImage;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class CodeBreakerGUI implements Runnable {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new CodeBreakerGUI());
    }
    
    private final Picture picture;
    
    public CodeBreakerGUI() {
        this.picture = new Picture();
    }

    @Override
    public void run() {
        JFrame frame = new JFrame("Code Breaker!");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        frame.add(createTopPanel(), BorderLayout.NORTH);
        frame.add(createBottomPanel(), BorderLayout.SOUTH);
        
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }
    
    public JPanel createTopPanel() {
        JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER, 25, 25));
        panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        
        JLabel label = new JLabel(new ImageIcon(picture.getEmptyImage()));
        panel.add(label);
        
        JLabel label2 = new JLabel(new ImageIcon(picture.getEmptyImage()));
        panel.add(label2);
        
        JLabel label3 = new JLabel(new ImageIcon(picture.getEmptyImage()));
        panel.add(label3);
        
        JLabel label4 = new JLabel(new ImageIcon(picture.getEmptyImage()));
        panel.add(label4);
        
        return panel;
    }
    
    public JPanel createBottomPanel() {
        JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER, 25, 25)) {
            private static final long serialVersionUID = 1L;

            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.setColor(Color.black);
                g.drawLine(120, 50, 360, 50);
            }
        };
        panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        
        JLabel label5 = new JLabel(new ImageIcon(picture.getEmptyImage()));
        panel.add(label5);
        
        JLabel label6 = new JLabel(new ImageIcon(picture.getEmptyImage()));
        panel.add(label6);
        
        JLabel label7 = new JLabel(new ImageIcon(picture.getEmptyImage()));
        panel.add(label7);
        
        JLabel label8 = new JLabel(new ImageIcon(picture.getEmptyImage()));
        panel.add(label8);
        
        return panel;
    }
    
    public class Picture {
        
        private final BufferedImage emptyImage;
        
        public Picture() {
            this.emptyImage = new BufferedImage(64, 64, BufferedImage.TYPE_INT_RGB);
        }

        public BufferedImage getEmptyImage() {
            return emptyImage;
        }
        
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文