使用JFrame以外的组件在JavaGUI中显示图像
我不熟悉JavaGUI编程。我正在开发一个需要在UI上显示图像的应用程序,以及一些其他按钮/控件。我有一段使用JFrame的工作代码 我的要求是,我想添加一些更多的小部件,如启动/停止显示图像的按钮等。当我使用Jframe显示图像时,它占据了整个Jframe,我无法添加其他控件使用JFrame以外的组件在JavaGUI中显示图像,java,swing,user-interface,awt,Java,Swing,User Interface,Awt,我不熟悉JavaGUI编程。我正在开发一个需要在UI上显示图像的应用程序,以及一些其他按钮/控件。我有一段使用JFrame的工作代码 我的要求是,我想添加一些更多的小部件,如启动/停止显示图像的按钮等。当我使用Jframe显示图像时,它占据了整个Jframe,我无法添加其他控件 我正在寻找可以显示图像并将其作为组件添加到JFrame中的东西。有人能解释一下怎么做吗?有几个组件支持显示图标。其中之一是JLabel: 另一个选项是扩展JPanel并重写paintComponent()方法以显示所需的
我正在寻找可以显示图像并将其作为组件添加到JFrame中的东西。有人能解释一下怎么做吗?有几个组件支持显示图标。其中之一是JLabel: 另一个选项是扩展JPanel并重写paintComponent()方法以显示所需的任何内容,包括图像: 我不熟悉JavaGUI编程。我正在开发一个需要在UI上显示图像的应用程序,以及一些其他按钮/控件。我有一段使用JFrame的工作代码 一般来说,我建议您尽量避免让您的类扩展顶级窗口类,如JFrame,因为这将极大地限制您对该类的操作。如果您需要扩展Swing组件,使其成为JPanel,则更好,因为这可以放在JFrame、JDialog、JOptionPane、另一个JPanel等中,。。。几乎可以在GUI中的任何位置。请注意,大多数情况下,您甚至不需要让类扩展组件 我的要求是,我想添加一些更多的小部件,如启动/停止显示图像的按钮等。当我使用Jframe显示图像时,它占据了整个Jframe,我无法添加其他控件 这有点令人困惑,因为JFrame本身没有显示图像的机制 我正在寻找可以显示图像并将其作为组件添加到JFrame中的东西。有人能解释一下如何做到这一点吗 您的要求有点模糊,但请考虑:
- 为要显示的每个图像制作图像图标,并保存图标
- 创建一个用于显示图像的JLabel
- 在JLabel上调用
以交换图像setIcon(someIcon)
- 如果您的图像需要更改大小以适应显示它的组件,则让您的显示组件扩展JPanel,并按照Kevin Workman的回答(1+对它!)在JPanel的
方法中显示您的图像paintComponent(Graphics g)
如果这些信息对你没有帮助,那么请考虑通过提供更多相关的背景信息和相关的代码来更多地解决你的问题。 < P> >一个简单的方法是在<代码> Jabel> <代码>中显示图像。 图像查看器
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.util.Random;
public class ImageViewer {
JPanel gui;
/** Displays the image. */
JLabel imageCanvas;
/** Set the image as icon of the image canvas (display it). */
public void setImage(Image image) {
imageCanvas.setIcon(new ImageIcon(image));
}
public void initComponents() {
if (gui==null) {
gui = new JPanel(new BorderLayout());
gui.setBorder(new EmptyBorder(5,5,5,5));
imageCanvas = new JLabel();
JPanel imageCenter = new JPanel(new GridBagLayout());
imageCenter.add(imageCanvas);
JScrollPane imageScroll = new JScrollPane(imageCenter);
imageScroll.setPreferredSize(new Dimension(300,100));
gui.add(imageScroll, BorderLayout.CENTER);
}
}
public Container getGui() {
initComponents();
return gui;
}
public static Image getRandomImage(Random random) {
int w = 100 + random.nextInt(400);
int h = 50 + random.nextInt(200);
BufferedImage bi = new BufferedImage(
w,h,BufferedImage.TYPE_INT_RGB);
return bi;
}
public static void main(String[] args) throws Exception {
Runnable r = new Runnable() {
@Override
public void run() {
JFrame f = new JFrame("Image Viewer");
// TODO Fix kludge to kill the Timer
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final ImageViewer viewer = new ImageViewer();
f.setContentPane(viewer.getGui());
f.pack();
f.setLocationByPlatform(true);
f.setVisible(true);
ActionListener animate = new ActionListener() {
Random random = new Random();
@Override
public void actionPerformed(ActionEvent arg0) {
viewer.setImage(getRandomImage(random));
}
};
Timer timer = new Timer(1500,animate);
timer.start();
}
};
SwingUtilities.invokeLater(r);
}
}