java - 在嵌套 JPanel 上绘制组件 (Java)

标签 java swing jpanel

有人知道如何在 JPanel 内的 JPanel 上画一个圆圈吗?

基本上,我在另一个 JPanel 中有一个,并且我创建了一个扩展 JComponent 的新 Circle 类。然后我将其添加到其中一个面板并尝试重新绘制,但没有显示任何内容。有任何想法吗?这是我的代码:

      class Circle extends JComponent 
{       
    @Override     public void paintComponent(Graphics g)
{
        super.paintComponent(g);
        g.drawOval(10,10, 11, 11);
        g.setColor(Color.RED);
        g.fillOval(10,10, 11, 11);                  
  } 
}


public void drawTest()
{
    Circle circle = new Circle();
    circle.setOpaque(false);
    circle.setSize(22, 22);
    circle.setVisible(true);
    circle.setBounds(11,11,11,5);
    jpanel.add(circle); 
    jpanel.repaint();

}

当我将圆圈添加到主面板[ add(circle) ] 时,代码有效,但拒绝任何子面板。

最佳答案

您需要覆盖 getPreferredSize(...)方法就像使用 paintComponent(...) 方法一样,让它类似于:

public Dimension getPreferredSize()
{
    return (new Dimension(300, 300));
}

这里是一个示例程序,可以为您提供进一步的帮助:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class TwoButtons
{
    private int x;
    private int y;
    private int count = 0;

    private Timer timer;

    private ActionListener actionTimer; 

    public static void main(String args[])
    {
        Runnable runnable = new Runnable()
        {
            public void run()
            {
                TwoButtons gui = new TwoButtons();
                gui.go();
            }
        };      
        SwingUtilities.invokeLater(runnable);
    }

    public void go()
    {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel contentPane = new JPanel();

        /*
         * Class Name : 
         * Java Naming Convention says that class names 
         * should be in Pascal Case, i.e. the first
         * letter of the class name should be capitalized
         * and every new word must start with a capitalized 
         * Alphabet.
         * For Example : 
         * public class ClassName{...}
         * ----------------------------------------------------------
         * Variable Name : 
         * Java Naming Convention says that the variable name
         * should be in Camel Case, i.e. the first letter of 
         * the variable name should be small case or _ (underscore)
         * and every new word must start with a capitalized
         * Alphabet.
         * ---------------------------------------------------------
         */
        final MyDraw drawPanel = new MyDraw(70, 70);
        x = drawPanel.getXValue();
        y = drawPanel.getYValue();
        contentPane.add(drawPanel);

        actionTimer = new ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {               
                x++;
                y++;
                if (count < 100)
                    drawPanel.setXYValues(x, y, count);
                else if (count == 100)
                    timer.stop();
                count++;
            }
        };

        frame.getContentPane().add(contentPane);
        frame.setSize(300,300);
        frame.setVisible(true);        

        timer = new Timer(40, actionTimer);
        timer.start();
    }
    class MyDraw extends JComponent
    {
        private int x;
        private int y;
        private int count = 0;
        private Timer timer;

        public MyDraw(int x, int y)
        {
            this.x = x;
            this.y = y;
        }

        public int getXValue()
        {
            return x;
        }

        public int getYValue()
        {
            return y;
        }

        public void setXYValues(int x, int y, int count)
        {
            this.x = x;
            this.y = y;
            this.count = count;
            repaint();
        }

        public Dimension getPreferredSize()
        {
            return (new Dimension(300, 300));
        }

        public void paintComponent(Graphics g)
        {
            g.setColor(Color.green);
            g.fillOval(x, y, 40, 40);
        }
    }
}

关于java - 在嵌套 JPanel 上绘制组件 (Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9725077/

相关文章:

java - 将滚动条添加到 JList ,将 JList 添加到 JPanel

java - 是否有更好的方法来分区列表和批量处理以更改源列表

java - 使用显示器野蛮进餐

java - 使用 ActionListener 与 MouseListener 捕获 JButton 上的点击的优缺点

java - 将默认 JFileChooser 目录设置为 JAR 位置的相对路径

java - 是否可以指向特定的 JPanel

Java - 我的字母顺序排序算法并不总是按预期工作

java - 为什么许多 SWT 控件不允许子类化?

Java fireTableDataChanged() 不更新 JTable

java - CardLayout 在调用 next() 时不切换面板