【问题标题】:How to make my images in JPanel scalable to window size?如何使 JPanel 中的图像可缩放到窗口大小?
【发布时间】:2016-07-30 20:56:56
【问题描述】:

我正在使用溢出的面板,其中包含可以在左侧单击然后在右侧显示的图像列表,我的图像有些大。我想让图像可以随窗口大小缩放。因此,如果我将鼠标拖到 EXIT 按钮附近并使窗口变大,则图片会变大,反之亦然。目前我的 JFrame 是 FIXED 默认窗口大小,但即便如此,图像也太大而无法完全看到。

这是我的代码:

驱动类:

import java.awt.*;
import javax.swing.*;
public class PickImage
{
   //-----------------------------------------------------------------
   // Creates and displays a frame containing a split pane. The
   // user selects an image name from the list to be displayed.
   //-----------------------------------------------------------------
   public static void main(String[] args)
   {
      JFrame frame = new JFrame("Pick Image");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setSize(500, 300);

      JLabel imageLabel = new JLabel();
      JPanel imagePanel = new JPanel();
      imagePanel.add(imageLabel);
      imagePanel.setBackground(Color.white);

      ListPanel imageList = new ListPanel(imageLabel);

      JSplitPane sp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
                                     imageList, imagePanel);

      sp.setOneTouchExpandable(true);

      frame.getContentPane().add(sp);

      frame.setVisible(true);
   }
}

ListPanel 类:

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

public class ListPanel extends JPanel
{

   private JLabel label;
   private JList list;
   public ListPanel(JLabel imageLabel)
   {
      label = imageLabel;

      String[] fileNames = { "Denali2.jpg",
                             "denali.jpg",
                             "MauiLaPerouseBay.jpg",
                              };
      list = new JList(fileNames);
      list.addListSelectionListener(new ListListener());
      list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
      add(list);
      setBackground(Color.white);
}
   private class ListListener implements ListSelectionListener
   {
      public void valueChanged(ListSelectionEvent event)
      {
         if (list.isSelectionEmpty())
            label.setIcon(null);
         else
         {
            String fileName = (String)list.getSelectedValue();
            ImageIcon image = new ImageIcon(fileName);
            label.setIcon(image);
         }
      }
   }
}  

【问题讨论】:

标签: java swing user-interface jframe jpanel


【解决方案1】:
  JPanel imagePanel = new JPanel();
  imagePanel.add(imageLabel);

首先,JPanel 默认使用 FlowLayout。因此组件以其首选大小显示,因此您的标签永远不会调整大小。

因此您需要更改布局,以便标签可以调整到可用空间:

  JPanel imagePanel = new JPanel( new BorderLayout() );

然后您可以使用Stretch Icon。此图标将自动缩放以填充其父组件中的可用空间。

另一种选择是自己在面板上绘制图像,然后您可以在绘制时缩放图像。您需要覆盖面板的paintComponent() 方法:

@Override
protected void paintComponent(Graphics g)
{
    super.paintComponent(g);
    Dimension d = getSize();
    g.drawImage(image, 0, 0, d.width, d.height, this);

}

阅读 Custom Painting 上的 Swing 教程部分,了解更多信息和示例。

或者您可以查看Background Panel,它是这种方法的一个更高级的实现。它允许您以原始大小绘制图像 1) 缩放、2) 平铺 3)。

【讨论】:

  • paintComponent 方法是进入 ListListener 事件类还是它自己的实体?我得到的唯一错误是在paintComponent方法中找不到图像符号
  • @UsmanBashiru,阅读自定义绘画教程并下载演示代码并测试演示代码,以便您了解基础知识。然后你可以自定义它。保留指向 Swing 教程的链接,以获取您可以自学的 Swing 基础知识的参考。或者您可以下载 BackgroundPanel 并查看那里的代码以了解自定义绘画的工作原理。
猜你喜欢
  • 2023-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多