如何缩放GUI图片和按钮位置

时间:2016-04-07 02:21:16

标签: java jpanel tooltip

大家好,我又问了另一个问题。所以我有这个程序,我基本上做了它应该做的事情,但我并没有真正实现程序中的滚动面板的想法,因为滚动面板的尺寸非常大。我只是把它做得很大,因为我可以在没有移动的情况下拥有隐形按钮。如果您了解我所说的内容,如何让我的隐形按钮停留在图像的特定位置而不是布局?我希望按钮停留在图像上的某个点上,而不是布局上,这样如果我移动滚动面板尺寸,它就随之移动。抱歉,如果我不善于解释这里的事情,那就是我的代码。感谢您抽出时间来阅读。

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

public class TransitMap
{
   //-----------------------------------------------------------------
   //  Presents a frame containing a scroll pane used to view a large
   //  map of the New York transit system.
   //-----------------------------------------------------------------
   public static void main(String[] args)
   {
      JFrame frame = new JFrame("New York Transit Map");

      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      //frame.setLayout(null);
      //frame.setPreferredSize(new Dimension(900, 900));

      JPanel mainPane = new JPanel();
      mainPane.setLayout(null);
      mainPane.setPreferredSize(new Dimension(1300,800));

      ImageIcon image = new ImageIcon("fortress.jpg");
      JLabel imageLabel = new JLabel(image);

      JScrollPane sp = new JScrollPane(imageLabel);
      sp.setPreferredSize(new Dimension(400, 400));
      sp.setBounds(0, 0, 1300, 800);

      JButton button = new JButton("test");
      button.setBounds(640,360,50,50);
      button.setOpaque(false);
      button.setContentAreaFilled(false);
      button.setBorderPainted(false);
      button.setToolTipText("<html><img src=\"" +
             TransitMap.class.getResource("Bala_Hissar.jpg") +
             "\"> <br> This is the fortress ");
      mainPane.add(button);
      mainPane.add(sp);
      frame.getContentPane().add(mainPane);
      frame.pack();
      frame.setVisible(true);
   }

}

0 个答案:

没有答案