【问题标题】:How to draw both graphics and buttons properly in Jframe?如何在 Jframe 中正确绘制图形和按钮?
【发布时间】:2021-09-05 03:30:36
【问题描述】:

我很难弄清楚如何同时设置图形和按钮而不会使按钮闪烁。

我正在尝试为我的机器人制作导航程序。在过去的一两周里,我尝试在我的程序中添加按钮,但没有任何效果,在此先感谢!

这是我的代码:

public static void main (String [] args) throws IOException{
image = ImageIO.read(new File("images/2020.jpg"));
double ratio = (double) image.getHeight() / (double) image.getWidth();
double Yscaled = (int) (Xscaled*ratio);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize((int) Xscaled+15 + 2*ButtonSpace + ButtonWidth,(int) Yscaled+34);

JButton clear = new JButton("Clear");
clear.setBounds((int)Xscaled+ButtonSpace, (int)Yscaled/4 - ButtonHeight/2, ButtonWidth, ButtonHeight);
window.add(clear);

cordList.add((int) Xs);
cordList.add((int) Ys);

JPanel painting = new JPanel() {
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;

        y1 = Math.sin(Math.toRadians(a))*c;
        x1 = Math.cos(Math.toRadians(a))*c;
        y2 = -Math.cos(Math.toRadians(a))*h;
        x2 = -Math.sin(Math.toRadians(a))*h;

        g.drawImage(image, 0, 0, (int) Xscaled, (int) Yscaled, null);

        if (cordList.size() > 0){
            if (cordList.get(cordList.size()-2) > Xscaled || cordList.get(cordList.size()-1) > Yscaled){
                cordList.remove(cordList.size()-2);
                cordList.remove(cordList.size()-1);
            }
        }
        
        for (int i = 0; i < cordList.size(); i+=2){
            int size = 5;
            g.setColor(new Color(220, 242, 19));
            g.fillOval(cordList.get(i)-size/2, cordList.get(i+1)-size/2, size, size);
            g.setColor(new Color(0, 0, 0));
            g.drawOval(cordList.get(i)-size/2, cordList.get(i+1)-size/2, size, size);
            if(i < cordList.size()-2){
                g.drawLine(cordList.get(i), cordList.get(i+1), cordList.get(i+2), cordList.get(i+3));
            }
        }
                        
        Triangle_Shape triangleShape = new Triangle_Shape(new Point2D.Double(Xs - x1, Ys - y1),
        new Point2D.Double(Xs + x1, Ys + y1), new Point2D.Double(Xs - x2, Ys + y2));
        Graphics2D g2d = (Graphics2D) g.create();
        g2d.draw(triangleShape);
        g2.setColor(new Color(19, 191, 15));
        g2.fill(triangleShape);
        repaint();
    }
};
painting.setBounds(0, 0 ,(int) Xscaled ,(int) Yscaled);
window.add(painting);

window.getContentPane().addMouseListener(new Field());
window.setVisible(true);

}

【问题讨论】:

  • 您为 JButtons 创建一个控件 JPanel,为图形创建一个绘图 JPanel。在 JFrame 上放置两个 JPanel。
  • 阅读 Custom Painting 上的 Swing 教程中的部分,以获得更好的结构类来进行绘画。 1) 不要使用 setBounds()。 Swing 被设计为与布局管理器一起使用。本教程还有一个关于Layout Managers 的链接。 2) 不要在绘画方法中调用 repaint()。 3)您需要在自定义面板中覆盖getPreferredSize()

标签: java swing jframe jpanel


【解决方案1】:

几年前我也遇到过同样的问题。 JPanels 可能非常有用,但在您的 JFrame 上绘图添加按钮而不闪烁仅适用于 JLabels原因。只需尝试用 JLabel 替换 JPanel,如下所示:

public static void main(String[] args) {
    // init Frame
    JFrame window = new JFrame();
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setSize(800, 600);

    // !! add Buttons before you add the JLabel to the Frame !!!!
    JButton clear = new JButton("Clear");
    window.add(clear);
    clear.setBounds(100, 100, 100, 100);

    // !! change to JLabel!!
    JLabel painting = new JLabel() {
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g;

            // adds Antialising for rounded edges when adding text
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

            // test draw
            g.setColor(Color.green);
            g.fillRect(0, 0, 500, 500);
            g.setColor(Color.black);
            g.setFont(new Font("Consolas", 0, 100));
            g.drawString("Test123'*#", 100, 300);

            repaint();
        }
    };
    window.add(painting);
    window.setVisible(true);
}

Working Window without flickering

旁注:您需要添加按钮和其他组件在将 JLabel添加到 JFrame 之前!

还添加了抗锯齿作为一个小技巧,您可以使用它来获得更好的视觉效果。

希望对你有帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-24
    • 2018-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多