【问题标题】:How to make a simple image class file in Java如何用Java制作一个简单的图像类文件
【发布时间】:2015-12-16 14:50:44
【问题描述】:

Okai,除了我命名为项目 2 的第二个私有类之外,基本上一切正常。在这个程序中,构建了一个 2 x 2 的网格,现在在一个网格空间 (Project3) 中,我想要一个图像显示出来。

  public class Project1 extends JFrame 
    {
        private Project2 topleft;           //  Buttons
        private Project3 topright;          //  Picture
        private Project4 bottomleft;        //  Schedule
        //private Project5 bottomright;     //  Help - Pad

    // Constructor

    public Project1() throws IOException
    {
        // Display a title.
        setTitle(" UAE University Interactive Course Calculator");

        // Specify an action for the close button.
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Create a GridLayout manager.
        setLayout(new GridLayout(2, 2));

        // Create the custom panels.
        topleft = new Project2();
        topright = new Project3();
        bottomleft = new Project4();
        //bottomright = new Project5();


        //bottomright = new Project5();

        // Create the button panel.
        add(topleft);
        add(topright);
        add(bottomleft);
        //add(bottomright);

        // setting formatting options
        pack();
        setResizable(true);
        setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        setVisible(true);
    }

    // Main method
    public static void main(String[] args) throws IOException
    {
        new Project1();
    }   
}

这是我到目前为止的地方,也许我的逻辑是错误的。我所需要的只是要显示的图像,仅此而已

public class Project3 extends JPanel 
{
    private JButton run,fancyButton, hel, but, done_by,exit, past;

    public Project3() 
    {
    Icon java1 = new ImageIcon( "untitled1.jpg" );
        fancyButton = new JButton( "Fancy Button", java1 );
    add(fancyButton);
    }
}

编辑!!!:okai 现在可以了,但它是一个带按钮的图像,我希望它成为一个没有按钮的图像吗?任何想法?

【问题讨论】:

  • 那么你的问题是什么?
  • 你永远不会在任何东西上添加fancyButton
  • 我只是想知道我哪里错了,只想有人告诉我如何调整我的程序,使它成为一个带有简单图像文件的程序
  • Project3 中尝试在fancyButton = new JButton( "Fancy Button", java1 ); 下添加add(fancyButton);。也可以考虑看看Reading/Loading an Image
  • 参考此链接Java ImageIcon/Icon and JLabel is not working。很可能是创建和加载图像图标的问题。

标签: java


【解决方案1】:

在 JPanel 上加载和显示图像的最简单选项之一:

  1. 创建一个ImageIcon
  2. 创建一个JLabel 并将ImageIcon 作为参数传递。
  3. 将 JLabel 添加到 JPanel(或任何其他摆动组件)。

您可以通过阅读How to use icons找到详细示例。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-25
    • 1970-01-01
    • 2021-12-26
    • 2022-01-26
    • 1970-01-01
    • 2012-04-22
    • 2013-03-12
    • 2021-08-03
    相关资源
    最近更新 更多