有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

swing在Java中创建一个简单的自定义JComponent?

我想开始为工作中的项目构建自己的定制JComponent。我下面有一个简单的例子,应该只是在屏幕上创建一个球。(我在网上找到了大部分)但它确实提供了一个不错的起点。我的问题是,为什么这个代码不在我的表单中显示球?我做错了什么

另外,应该为定制JComponent提供哪些基本方法

代码:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridBagLayout;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class testBall {
    public static void main(String[] args) {
        new testBall();
    }

    public testBall() {
        JPanel testPane = new JPanel();
        testPane.setBackground(Color.white);
        testPane.setLayout(new GridBagLayout());
        testPane.add(new MyBall(30,30,10));

        JFrame frame = new JFrame("Testing");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new BorderLayout());
        frame.add(testPane);
        frame.pack();
        frame.setSize(500, 300); 
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

class MyBall extends JComponent
{
    private static final long serialVersionUID = 1L;
    public MyBall() { }
    public MyBall(int x, int y, int diameter)
    {
        super();
        this.setLocation(x, y);
        this.setSize(diameter, diameter);
    }

    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        g.setColor(Color.red);
        g.fillOval(0, 0, 100, 100);
    }
}

在哪里可以找到JComponent类中应该重写的所有方法的列表?(我知道JComponent中应该始终包含一些组件。)

如果我在一个类中创建这个组件的一个实例,并且需要更改圆的颜色,我会从该类中调用repaint()方法吗


共 (3) 个答案

  1. # 1 楼答案

    您正在将MyBall添加到testPane(它有一个GridBagLayout),而不指定任何约束。(也就是说,对add()的调用没有第二个参数。)默认约束很可能不是您想要的。尝试在测试窗格中使用BorderLayout,因为它使用BorderLayout.CENTER作为默认值,这在您的情况下可能是合理的:

    testPane.setLayout(new BorderLayout());
    

    这使得球出现在我面前

    至于你的第二个问题,我认为你的课很好。您想要实现的主要方法是paintComponent()。有时有必要重写get min/max/preferred size方法,但这实际上取决于您希望组件做什么JComponent不是抽象的,所以如果不想重写,就不必重写任何内容。它提供了很多现成的功能,比如键盘焦点、可插拔的外观和感觉、可访问性等。只要你不想改变这些东西,就让它保持原样。实现paintComponent()和各种get*Size()方法,并用它来完成。(你只需要在JavaDoc中挑选合适的方法,看看哪些方法适合重写。)

    另一个选项是扩展JComponent的子类,如果有一个类可以做类似于您想要做的事情。例如,JPanel通常是推动自己的容器的良好起点

    你可能在寻找比“它取决于”更具体的东西,但现在,如果你只想画一个球,那么只需覆盖处理JComponentpaintCompentent(),和get*Size()方法)渲染的方法

    作为补充说明,您真的应该使用SwingUtilities.invokeLater()在Swing线程上创建Swing组件。请参阅http://docs.oracle.com/javase/6/docs/api/javax/swing/package-summary.html上题为“Swing的线程策略”的部分

  2. # 2 楼答案

    对java类做了一些调整,我做的唯一更改是将新的MyBall直接添加到JFrame的内容窗格中,尝试运行此操作,你会在JFrame上看到一个红色圆圈

    public class TestBall {
        public static void main(String[] args) {
            new TestBall();
        }
    
        public TestBall() {
    
            JFrame frame = new JFrame("Testing");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLayout(new BorderLayout());
            frame.getContentPane().add(new MyBall(30,30,10));
            frame.pack();
            frame.setSize(500, 300);
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    }
    
    class MyBall extends JComponent
    {
        private static final long serialVersionUID = 1L;
        public MyBall() { }
        public MyBall(int x, int y, int diameter)
        {
            super();
            this.setLocation(x, y);
            this.setSize(diameter, diameter);
        }
    
        @Override
        public void paintComponent(Graphics g)
        {
            super.paintComponent(g);
            g.setColor(Color.red);
            g.fillOval(0, 0, 100, 100);
        }
    }
    
  3. # 3 楼答案

    构造函数:我们只是将位置设置为通过构造函数参数传递的点。这就是该组件将位于的位置。我们设置大小的方法相同(widthxheight

    paintComponent:这里我们只是在经过的图形对象上绘制一些椭圆形

    另一部分只是测试,它表明组件的创建和显示是正确的