java - 帮助简单的框架和图形

标签 java user-interface

对于作业,我试图创建一个具有框架的“自定义按钮”,在该框架中,我绘制了两个三角形,并在其上绘制了一个正方形。它应该为用户提供按下按钮后按下按钮的效果。因此,对于初学者来说,我尝试设置开始图形,绘制两个三角形和一个正方形。我遇到的问题是,虽然我将框架设置为 200、200,并且我绘制的三角形我认为是框架尺寸的正确端部,但当我运行程序时,我必须扩展窗 Eloquent 能制作整个艺术品,我的“自定义按钮”可见。这正常吗?谢谢。

代码:

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


public class CustomButton
{
    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                CustomButtonFrame frame = new CustomButtonFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
            }
        });
    }
}

class CustomButtonFrame extends JFrame
{
    // constructor for CustomButtonFrame
    public CustomButtonFrame()
    {
        setTitle("Custom Button");
        setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
        CustomButtonSetup buttonSetup = new CustomButtonSetup();
        this.add(buttonSetup);
    }

    private static final int DEFAULT_WIDTH = 200;
    private static final int DEFAULT_HEIGHT = 200;

}

class CustomButtonSetup extends JComponent
{
    public void paintComponent(Graphics g)
    {
        Graphics2D g2 = (Graphics2D) g;

        // first triangle coords
        int x[] = new int[TRIANGLE_SIDES];
        int y[] = new int[TRIANGLE_SIDES];
        x[0] = 0;   y[0] = 0;
        x[1] = 200; y[1] = 0;
        x[2] = 0;   y[2] = 200;
        Polygon firstTriangle = new Polygon(x, y, TRIANGLE_SIDES);

        // second triangle coords
        x[0] = 0;   y[0] = 200;     
        x[1] = 200; y[1] = 200;
        x[2] = 200; y[2] = 0;
        Polygon secondTriangle = new Polygon(x, y, TRIANGLE_SIDES);

        g2.drawPolygon(firstTriangle);
        g2.setColor(Color.WHITE);
        g2.fillPolygon(firstTriangle);

        g2.drawPolygon(secondTriangle);
        g2.setColor(Color.GRAY);
        g2.fillPolygon(secondTriangle);

        // draw rectangle 10 pixels off border
        g2.drawRect(10, 10, 180, 180);

    }
    public static final int TRIANGLE_SIDES = 3;
}

最佳答案

尝试添加

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

到您的 CustomButtonSetup 类。

然后做

    setTitle("Custom Button");
    //setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
    CustomButtonSetup buttonSetup = new CustomButtonSetup();
    this.add(buttonSetup);
    pack();

(来自 pack() 的 api 文档:)

Causes this Window to be sized to fit the preferred size and layouts of its subcomponents.

你应该得到类似的东西:

enter image description here

关于java - 帮助简单的框架和图形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2871586/

相关文章:

java - Java中奇怪的数组声明

java - 在单独的线程中将对象返回到池

java - 将多个实时图表从 XChart 添加到 JPanel

java - 在 Camel Route 中获取 JMSXDeliveryCount

java - paintComponent 和 super.paintComponent

java - 如何用 | 分割字符串但不与\\|

c# - 我们可以更改 PropertyGrid 中单个属性的文本/背景颜色吗

java - 良好的用户界面设计 : How to handle empty ListView?

user-interface - 无法移动 Canvas 或更改任何变换值

java - 在 Android 中动态创建/删除按钮