如何在JFrame的标题栏中添加图像?

时间:2019-03-23 12:28:28

标签: java swing jframe titlebar

如果通过使用stayOnTop(boolean status)方法调用displayLockIcon(boolean status)方法将窗口设置为始终位于顶部,则我试图将锁定图像添加到标题栏。我尝试将其添加到JLabel并将该JLabel添加到JFrame this.add(jLabelWithImage),并且尝试了this.setImageIcon(image)。 所以问题是,如果启用了始终位于最前面的设置,我该如何将图像放在标题栏上?哦,我正在使用IntelliJ IDEA IDE,它是gui形式。

package com.negassagisila.axeereraa;

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

public class AxeereraaUI extends JFrame { 
    private JPanel axRootPanel;
    private JScrollPane axRootScrollPane;
    private JTextArea axRootTextArea;
    private JMenuBar axMenuBar;


    public AxeereraaUI() {
     UIManager.setLookAndFeel(axRunner.getLookAndFeel());

    setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

    add(axRootPanel);
    setSize(300, 250);
    setTitle("Axeereraa");

    buildUI();
    setJMenuBar(axMenuBar);
}  

private void buildUI() {
    axMenuBar = new JMenuBar();

 axRootScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);

 axRootScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

 axRootScrollPane.getVerticalScrollBar().setPreferredSize(
            new Dimension(4, Integer.MAX_VALUE));
  /**
     * as the name suggests this sets up the JMenus and their 
 corresponding JMenuItems
     */
    SetUpMenuAndMenuItems setUpMenuAndMenuItems = new 
    SetUpMenuAndMenuItems().invoke();
    JMenu fileMenu = setUpMenuAndMenuItems.getFileMenu();
    JMenu editMenu = setUpMenuAndMenuItems.getEditMenu();
    JMenu viewMenu = setUpMenuAndMenuItems.getViewMenu();
    JMenu helpMenu = setUpMenuAndMenuItems.getHelpMenu();

    axMenuBar.add(fileMenu);
    axMenuBar.add(editMenu);
    axMenuBar.add(viewMenu);
    axMenuBar.add(helpMenu);

    axRootTextArea.setLineWrap(true);
    axRootTextArea.setWrapStyleWord(true);
}

   /**
   * This method is responsible for setting the application always on top
   * @param status boolean value to be passed to the instance of the UI.
   */

  private void stayOnTop(boolean status) {

    /**
     * called on every instance of the UI, method from the JFrame class.
     */

    this.setAlwaysOnTop(status);

    /**
     * changes the icon to lock to show that the result
     */

    displayLockIcon(status);
  }

  /**
   * changes the icon to lock to show that the result
   * @param status boolean value of that checks if the always on top has been set
   */

  private void displayLockIcon(boolean status) { 
    if(status) {
    final JLabel jLabelWithImage = new JLabel(new 
    ImageIcon("src/main/resources/images/lock.png"));
    this.add(jLabelWithImage);
    //ImageIcon imageIcon = new 
    ImageIcon("src/main/resources/images/lock.png");
    //this.setImageIcon(imageIcon); 
 }
 }  

谢谢。

0 个答案:

没有答案