【问题标题】:How to add scrollbar and scale images?如何添加滚动条和缩放图像?
【发布时间】:2014-04-12 01:17:00
【问题描述】:

好的,基本上我一直在尝试使用 swing、awt 等制作交互式地图。在直接进入我的项目之前,我正在测试一些东西,我在图像大小和添加滚动条方面遇到了困难,图像是 2000x1000 png,但它似乎只在中心显示中间 500x500 像素(我将提供图像)我也查了一些滚动条,但它似乎没有做任何事情?基本上我希望整个图像与滚动条一起可以在任何其他窗口中查看。这是我的代码。

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class GuiM extends JFrame{
    private JPanel map;
    private JPanel buttons;
    private JLabel imageLabel;
    public GuiM(){
        JScrollPane scroller = new JScrollPane(map);  
        this.getContentPane().add(scroller, BorderLayout.EAST); 
        map = new JPanel();
        map.setLayout(new GridLayout());
        buttons = new JPanel();
        imageLabel =  new JLabel(new ImageIcon(getClass().getResource("MapLI.png")));
        map.add(imageLabel);
        setLayout(new GridLayout());
        setExtendedState(JFrame.MAXIMIZED_BOTH);
        setVisible(true);
        setResizable(true);
        add(map);
        add(buttons);
        pack();
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
}

public class MainM {

    public static void main(String[] args){ 
        GuiM gui = new GuiM();
    }
}

这里有一些图片
这张图片是我想在我的面板中显示的

但这就是我得到的。

【问题讨论】:

  • 而不是使用地图面板,将标签添加到滚动窗格的位置

标签: java swing scroll awt scaling


【解决方案1】:
 JScrollPane scroller = new JScrollPane(map);

上面的语句什么也不做,因为 map 变量是空的。摆脱它。

  this.getContentPane().add(scroller, BorderLayout.EAST);

上面的语句没有做任何事情,因为您稍后将布局更改为 GridLayout 并且滚动条不包含任何要显示的组件。摆脱它。

您想将滚动条添加到 GUI,而不是地图,所以代码应该是这样的:

//add(map);
JScrollPane scroller = new JScrollPane(map); // or add the "imageLabel"
add(scroller);

或者,将标签添加到滚动窗格会更容易,不需要创建一个面板来保存标签。

另外,setVisible(true) 语句应该移到程序的底部,在组件被添加到框架之后和 pack() 之后。

【讨论】:

  • 非常感谢,现在我的图像只从屏幕的中心到左侧占据,有没有办法让它占据整个屏幕?
  • @Adamc23,您将框架设置为使用 GridLayout 并在框架中添加了两个组件,因此每个组件都获得 50% 的空间。我会保留默认的 BorderLayout,然后将 scrollPane 添加到CENTER,然后将您的buttonsPanel 添加到SOUTH。我建议您阅读 Layout Managers 上的 Swing 教程以获取更多解释和工作示例。该示例还将帮助您更好地构建代码。你的代码是错误的,因为你不应该扩展 JFrame,你应该使用 SwingUtilities.invokeLater() 构建 GUI。
  • 知道了,我一定会调查一下,我想添加一些按钮,所以我认为 Grid 会有所帮助,但正如你提到的,我绝对应该重新考虑这一点,我会给布局管理器一个再看一遍,非常感谢。
【解决方案2】:

您没有像下面那样将标签 imagelabel 添加到 JScrollPane。请将此添加到您的代码中:

修改后的代码如下所示。

GuiM.java

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class GuiM extends JFrame{
    private JPanel map;
    private JPanel buttons;
    private JLabel imageLabel;
    public GuiM(){
        JScrollPane scroller = new JScrollPane(map);  
        this.getContentPane().add(scroller, BorderLayout.EAST); 
        map = new JPanel();
        map.setLayout(new GridLayout());
        buttons = new JPanel();
        imageLabel =  new JLabel(new ImageIcon(getClass().getResource("MapLI.png")));
        JScrollPane scrollpane=new JScrollPane(imageLabel);
        map.add(scrollpane);
        setLayout(new GridLayout());
        setExtendedState(JFrame.MAXIMIZED_BOTH);
        setVisible(true);
        setResizable(true);
        add(map);
        add(buttons);
        pack();
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
}

和 MainM.java

public class MainM {

    public static void main(String[] args){ 
        GuiM gui = new GuiM();
    }
}

【讨论】:

    【解决方案3】:

    您可以使用 java.awt 类将滚动条添加到您的代码中。完整代码请参考链接。 http://www.fixoncloud.com/Home/LoginValidate/OneProblemComplete_Detailed.php?problemid=558

    【讨论】:

      猜你喜欢
      • 2015-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-30
      • 2019-01-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多