【问题标题】:Default colored spacing between JPanel and FrameJPanel 和 Frame 之间的默认彩色间距
【发布时间】:2014-03-04 06:11:40
【问题描述】:

我写了一个简单的程序,在打印“生日快乐!”时随机改变背景颜色。到屏幕上,但我看到一个不需要的间距。我该如何解决这个问题?

生日.java

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class Birthday {

    DrawString panel = new DrawString();

    public Birthday() {
        JFrame f = new JFrame();

        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(panel);
        f.setTitle("Happy Birthday!");
        f.setLocation(10, 10);
        f.setVisible(true);
        f.setResizable(false);
        f.pack();

        Timer timer = new Timer(500, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                panel.repaint();
            }
        });
        timer.start();
    }

    public static void main(String args[]) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new Birthday();
            }
        });
    }
}

DrawString.java

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import javax.swing.JPanel;

public class DrawString extends JPanel {

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        int width = getWidth();
        int height = getHeight();

        int x = 350;
        int y = 250;
        int red   = random(0,255);
        int green = random(0,255);
        int blue  = random(0,255);
        Color black = new Color(0,0,0);

        Color newColor = new Color(red,green,blue);
        g.setColor(newColor);
        g.fillRect(0,0,1000,500);
        g.setColor(black);
        g.setFont(new Font(null,Font.BOLD,40));
        g.drawString("Happy Birthday!",x,y);
    }

    public static int random(int min, int max)
    {
        int range = max - min + 1;
        int number = (int) (range * Math.random() + min);
        return number;
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(1000, 500);
    }
}

【问题讨论】:

    标签: java string swing jpanel paintcomponent


    【解决方案1】:

    这与可调整大小和不可调整大小的框架边框之间的差异有关。

    因为您在调用setResizable 之前调用了setVisible,所以窗口对框架装饰的更改没有反应...

    尝试最后拨打setVisible,例如...

        f.setResizable(false);
        f.pack();
        f.setVisible(true);
    

    这是一般 UI 创建的好建议,在您建立 UI 内容和属性后最后致电setVisible ;)

    【讨论】:

    • 在构造函数末尾调用 f.pack() 也有效。
    • @HugoSousa 是的,但他们已经给pack 打过一次电话……不妨利用这个事实;)
    • 另外,f.add(panel); 确实应该是f.setContentPane(panel);,但这并不能解决额外空间问题。只是一个建议。
    • 我的意思是从它所在的位置删除并放在构造函数的末尾。它有效,但我不知道为什么(如果你知道,请解释我)。在此之前只有计时器定义(相对于它原来的位置)所以我不明白为什么它会这样工作。
    • @HugoSousa 不知道,也许到那时框架已经被实现到足以识别框架边框的差异......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-04
    • 1970-01-01
    相关资源
    最近更新 更多