Drawline 不适用于 JPanel 或 JFrame

Drawline not working with JPanel or JFrame

我似乎无法显示该行。我有背景颜色和一些图片。如果我有 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();
    }
}

简介

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

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

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

说明

所有 Swing 应用程序必须以调用 SwingUtilities invokeLater 方法开始。此方法确保 Swing 组件在 Event Dispatch Thread.

上创建和执行

在不同的方法中创建 JFrameJPanels。这使代码更易于阅读和理解。

使用Swing layout managersJFrame 有一个默认值 BorderLayout。我对 JPanels.

都使用了 FlowLayout

代码

这是完整的可运行代码。我将 Picture class 设为内部 class 这样我就可以 post 将代码作为一个块。

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

}