【问题标题】:Why do I need to hover my mouse over a button for it to show up?为什么我需要将鼠标悬停在按钮上才能显示?
【发布时间】:2013-05-03 21:25:29
【问题描述】:

我在这里遇到了问题,我的按钮工作正常,直到我添加背景图像,当我尝试将鼠标放在按钮和文本字段上以显示它们时。无论我的鼠标是点击它还是点击它,我的标签也不起作用?这是类的代码。

public class launcher extends JFrame{
    private static final long serialVersionUID = 1L;

     protected JPanel window = new JPanel();
     protected JFrame f = new JFrame("stackoverflow");

    private Rectangle rAncient, rMedieval, rModern, rFuture, rFinancial, rUpdate;
    private JButton ancient, medieval, modern, future, financial, update;
    private JLabel time, name;
    private JTextField tName;


     protected int width = 600;
     protected int height = 400;
     protected int button_width = 80;
     protected int button_height = 40;


    public launcher(int id) throws IOException{
        try{
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e){
            e.printStackTrace();
        }
        setTitle("Choose your Path");
        setSize(new Dimension(width, height));
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        getContentPane().add(window);
        setLocationRelativeTo(null);
        setResizable(false);
        setVisible(true);
        window.setLayout(null);
        if(id==0){
        drawButtons();
    }
        repaint();
    }
    private void drawButtons(){
        ancient = new JButton("Ancient");
        rAncient = new Rectangle(140, 150, 70, 40);
        ancient.setBounds(rAncient);
        window.add(ancient);

        medieval = new JButton("Medieval");
        rMedieval = new Rectangle(220, 150, 80, 40);
        medieval.setBounds(rMedieval);
        window.add(medieval);

        modern = new JButton("Modern");
        rModern = new Rectangle(310, 150, 70, 40);
        modern.setBounds(rModern);
        window.add(modern);

        future = new JButton("Future");
        rFuture = new Rectangle(350, 150, 70, 40);
        future.setBounds(rFuture);
        window.add(future);

        financial = new JButton("Financial");
        rFinancial = new Rectangle(390, 150, 70, 40);
        future.setBounds(rFinancial);
        window.add(financial);

        update = new JButton("Update");
        rUpdate = new Rectangle(250, 300, 100, 50);
        update.setBounds(rUpdate);
        window.add(update);

        time = new JLabel("Choose your desired time period");
        time.setBounds(220, 90, 200, 50);
        window.add(time);

        name = new JLabel("Name: ");
        name.setBounds(210, 220, 200, 50);
        window.add(name);

        tName = new JTextField();
        tName.setBounds(250, 235, 150, 20);
        window.add(tName);
        tName.setText("Bob");

          ancient.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent a){
                System.out.println("Starting in Ancient mode...");
            }
          });

          medieval.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent a){
                System.out.println("Starting in Medieval mode...");
            }
          });

          modern.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent a){
                System.out.println("Starting in Modern mode...");
            }
          });

          future.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent a){
                System.out.println("Starting in Future mode...");
            }
          });

          financial.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent a){
                System.out.println("Starting in Financial mode...");
            }
          });

          update.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent a){
                System.out.println("Opening browser to http://ubergamesproductions.weebly.com/adventure");
                try{
                    String URL ="http://ubergamesproductions.weebly.com/adventure";
                    java.awt.Desktop.getDesktop().browse(java.net.URI.create(URL));
                }catch(Exception e){
                    JOptionPane.showMessageDialog(null, e.getMessage());
                }
            }
          });
    }
    public static void main(String[] args)throws IOException{
    new launcher(0);
    }
      private Image backgroundImage = ImageIO.read(new File("C:/Users/Samuels Laptop/Dropbox/Java/Questor/Launcher_UGP.png"));
      public void paint(Graphics g) { 
        super.paint(g);
        g.drawImage(backgroundImage, 0, 0, null);
      }
};

【问题讨论】:

    标签: java swing cursor jlabel jtextfield


    【解决方案1】:

    公共启动器(int id)抛出 IOException{

    使用标准 Java 命名约定。类名应以大写字符开头。

    window.setLayout(null);
    ...
    ancient = new JButton("Ancient");
    rAncient = new Rectangle(140, 150, 70, 40);
    ancient.setBounds(rAncient);
    

    不要使用空布局和 setBounds(...)。使用适当的布局管理器并让布局管理器完成其工作。在这种情况下,您可能可以使用 JPanel 及其默认 FlowLayout。

    public void paint(Graphics g) { 
        super.paint(g);
        g.drawImage(backgroundImage, 0, 0, null);
    

    不要覆盖顶级容器的paint() 方法。在您的情况下,您的代码会绘制框架和所有组件。然后你绘制图像。所以图像覆盖了组件。该按钮响应 mouseEntered 事件以 repaint() 边框,​​这就是它突然出现的原因。

    相反,自定义绘画是通过覆盖 JPanel 的 paintComponent(...) 方法来完成的。然后将面板添加到框架中,并将组件添加到面板中。阅读 Custom Painting 上的 Swing 教程中的部分以获取更多信息。花时间阅读整个教程。在Layout Managers上也有一节。

    【讨论】:

    • 请注意JFrameImageObserver
    • @AndrewThompson,这对问题或答案有何影响?我认为 ImageObserver 只是意味着当图像完全加载时组件将重新绘制自己。我认为这不是问题所在。
    • 我不这么认为。 OTOH,如果您有ImageObserver,为什么不使用它来期待有人使用带有异步加载图像的类?好像不用再打字了。
    • @AndrewThompson,我认为您是说在 drawImage(...) 方法中不应使用“null”,而应该使用“this”来潜在地利用 JPanel 作为 ImageObserver 时自定义绘画在 paintComponent() 方法中完成。
    猜你喜欢
    • 1970-01-01
    • 2014-06-30
    • 1970-01-01
    • 1970-01-01
    • 2018-06-29
    • 2021-01-19
    • 1970-01-01
    • 2012-10-06
    • 1970-01-01
    相关资源
    最近更新 更多