【问题标题】:Using Images as Buttons, getting a Blank Screen使用图像作为按钮,得到一个空白屏幕
【发布时间】:2014-05-15 03:00:12
【问题描述】:

我正在尝试向我的 GUI 添加一个图像按钮,但我得到的只是一个空白屏幕。谁能告诉我我的代码有什么问题?这是我目前所拥有的。

package fallingStars;

import java.awt.*;
import java.awt.event.*;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class GUI extends JFrame 
{
    private JLabel label;
    private JButton button;
    private JPanel buttonPanel;

    public static void startButton() 
    {
        ImageIcon start = new ImageIcon("Start.png");
        JButton play = new JButton(start);
        play.setBounds(150,100,100,50);
    }

    public static void main(String args[]) 
    {
        GUI gui = new GUI();
        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gui.setSize(300, 500);
        gui.setVisible(true);
        gui.setResizable(false);
        gui.setTitle("Falling Stars");

        JPanel panel = new JPanel();

        startButton();

    }
}

【问题讨论】:

  • 您在哪里将您的 JButton 添加到您的 JPanel 中?
  • 您的startButton 方法创建一个ImageIcon 然后创建一个JButton。你认为哪一部分应该让它出现?您还可以在 main 方法中创建一个 JPanel panel,然后您什么也不做。您还创建了 3 个组件作为实例变量,但也从不使用它们。
  • play.setBounds(150,100,100,50); Java GUI 可能必须在多个平台、不同的屏幕分辨率和使用不同的 PLAF 上工作。因此,它们不利于组件的精确放置。要为强大的 GUI 组织组件,请改用布局管理器或 combinations of them,以及 white space 的布局填充和边框。
  • 1) ImageIcon start = new ImageIcon("Start.png"); 到部署时,这些资源可能会变成embedded-resource。在这种情况下,资源必须由URL 而不是File 访问。请参阅标签的info page,了解形成URL 的方法。 2) 另请参阅chess GUI Q&A,了解在按钮中使用图像的工作示例。

标签: java image swing jbutton imageicon


【解决方案1】:

您必须使用 container.add(JButton); 将 JButton 添加到容器中;

如果你不实际添加它,你会得到一个空白屏幕:)

【讨论】:

    【解决方案2】:

    首先只需在 JLabel 中插入一个 ImageIcon。如果你想要一个图像作为按钮,你必须像我一样添加 MouseListener 并实现 mouseClicked()

    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import javax.swing.*;
    
    
    public class MyFrame extends JFrame{
        private JLabel label;
        private static MyFrame frame;
    
        public MyFrame(){
            initComponent();
            label.addMouseListener(new MouseListener(){
    
                @Override
                public void mouseClicked(MouseEvent arg0) {
                    JOptionPane.showMessageDialog(frame, "You just clicked the image");         
                }
    
                @Override
                public void mouseEntered(MouseEvent arg0) {}
                @Override
                public void mouseExited(MouseEvent arg0) {}
                @Override
                public void mousePressed(MouseEvent arg0) {}
                @Override
                public void mouseReleased(MouseEvent arg0) {}
    
            });
        }
    
        private void initComponent() {
            this.setSize(400, 400);
            this.setLocationRelativeTo(null);
            label = new JLabel();
            label.setIcon(new ImageIcon(getClass().getResource("res/image.png")));
            add(label);     
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setVisible (true);
        }
    
        public static void main(String[] args) {
            frame = new MyFrame();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-12-05
      • 1970-01-01
      • 1970-01-01
      • 2019-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-03
      • 1970-01-01
      相关资源
      最近更新 更多