簡體   English   中英

JButton只出現在鼠標上?

[英]JButton only appears on mouse over?

是我的代碼:我拿出了一些我覺得沒必要的東西。 我也可能已經取出了一些括號,但我只想展示我的內容。

當我運行程序時,背景圖像描繪(它是資源中的PNG),只出現一個按鈕(我的PLAY按鈕),這是第一個按鈕 - 它是自動選擇的。

我實際上有四個按鈕,但我只在我的代碼中包含了播放和說明。 除非我將鼠標放在它們上面,否則其他三個不顯示。 我知道這可能與paint方法有些奇怪,但我不知道如何修復它。

如果我選擇一個不同的按鈕並最小化窗口然后再次打開它,則所選按鈕是唯一出現的按鈕。 我必須將鼠標移開才能顯示其他按鈕。

我也在paint方法中添加了super.paint() ,我得到了所有按鈕,但背景是灰色的。 我認為問題是super.paint()繪制了我的所有按鈕,而g.drawImage(bg, 0, 0, null)只繪制我的背景,我不能不繪制另一個。

對不起,如果這是一團糟。 我是Java的新手,我很難說出我想說的話。

public class MainMenu extends JFrame {

    private JPanel contentPane;

    /**
     * Launch the application.
     */

    //variables
    public static Image bg;

    public static void main(String[] args) {

        MainMenu mainFrame = new MainMenu();
        mainFrame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
        mainFrame.setResizable(false);
        mainFrame.setLocationRelativeTo(null);
        mainFrame.setTitle ("Zumby");
        mainFrame.setLayout(null);

        // Loads the background image and stores in bg object.
        try {
            bg = ImageIO.read(new File("zumby.png"));
        } catch (IOException e) {
        }
        mainFrame.setVisible(true);
    }

    /**
     * Overrides the paint method.
     * MONDAY
     */
     public void paint(Graphics g)
     {
        // Draws the img to the BackgroundPanel.
        System.out.println("paint");
        g.drawImage(bg, 0, 0, null);
     }

    /**
     */
    public MainMenu() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 800, 500);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setOpaque(false);
        setContentPane(contentPane);
        contentPane.setLayout(null);

        //create buttons
        JButton btnPlay = new JButton("PLAY");
        btnPlay.setBackground(Color.BLACK);
        btnPlay.setForeground(Color.WHITE);
        btnPlay.setFont(font);
        btnPlay.setBorder(border);
        btnPlay.setFocusPainted(false);

        //if "Play" is clicked

        btnPlay.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent click) {
                setVisible(false);
                new GamePlay(); //opens up GamePlay window
            }
        });
        btnPlay.setBounds(600, 64, 141, 61);
        contentPane.add(btnPlay);

        JButton btnInstructions = new JButton("INSTRUCTIONS");

        btnInstructions.setBounds(600, 160, 141, 61);
        btnInstructions.setBackground(Color.BLACK);
        btnInstructions.setFocusPainted(false);
       // btnInstructions.setEnabled(true);

        contentPane.add(btnInstructions);
        repaint();
        pack(); 
        setVisible(true);

    }

}

Swing使用“分層”概念進行繪畫......

paint調用paintComponentpaintBorderpaintChildren 通過覆蓋paint並且無法調用super.paint ,您已經阻止組件繪制它的各個層。

在Swing中,最好使用paintComponent來提供自定義繪制,這允許您在可能添加到組件的任何其他組件下面繪制。

在此輸入圖像描述

public class TestPaint01 {

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

  public TestPaint01() {
    EventQueue.invokeLater(new Runnable() {
      @Override
      public void run() {
        try {
          UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception ex) {
        }

        JFrame frame = new JFrame("Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new TestPane());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

      }
    });
  }

  public class TestPane extends JPanel {

    private Image backgroundImage;

    public TestPane() {
      try {
        BufferedImage background = ImageIO.read(new File("/path/to/image.jpg"));
        //backgroundImage = background.getScaledInstance(-1, background.getHeight() / 4, Image.SCALE_SMOOTH);
        backgroundImage = background;
      } catch (IOException ex) {
        ex.printStackTrace();
      }
      setLayout(new GridBagLayout());
      add(new JButton("Hello"));
    }

    @Override
    public Dimension getPreferredSize() {
      return backgroundImage == null ? super.getPreferredSize() : new Dimension(backgroundImage.getWidth(this), backgroundImage.getHeight(this));
    }

    @Override
    protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      int x = (getWidth() - backgroundImage.getWidth(this)) / 2;
      int y = (getHeight() - backgroundImage.getHeight(this)) / 2;
      g.drawImage(backgroundImage, x, y, this);
    }

  }

}

你可能會發現更仔細地看看 AWT 中的Paint MechanismPainting以及Swing的信息。

我認為這是因為你重寫了paint方法。 最好覆蓋重繪,然后調用super.repaint(); 像這樣:

public void repaint(Graphics g)
        {
             super.repaint(g);
         // Draws the img to the BackgroundPanel.
             System.out.println("paint");
           g.drawImage(bg, 0, 0, null);
        }

然后重新繪制組件。

但是,如果你想做的只是顯示圖像,背景請參見此處。

你正在覆蓋paint()但不要調用super.paint() 因此,不執行JFrame的paint()方法實現所完成的組件的正常繪制。

由於您使用的是Swing和JFrame因此用於覆蓋paintComponent的繪制機制不會paint通常與applet或AWT一起使用的paint

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM