在正确的框架中加载图像时出现问题
这是我来自主框架窗口的代码:
public class DynamicalSystem {
public static void createAndShowGraphic() {
//Create and set up the window.
JFrame frame = new JFrame("Dynamical System: The beauty of Chaos");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel emptyLabel = new JLabel("");
emptyLabel.setPreferredSize(new Dimension(500, 500));
frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);
//Display the window.
MenuLook menubar = new MenuLook(); //display menubar
frame.setJMenuBar(menubar.createMenuBar());
frame.pack();
frame.setVisible(true);
}
}
这是来自我的 bufferdimage:
public class LabelDemo extends JPanel
{
//path of image
private String path;
//image object
private Image img;
public LabelDemo(String path) throws IOException
{
//save path
this.path = path;
//load image
img = ImageIO.read(new File(path));
}
//override paint method of panel
public void paint(Graphics g)
{
//draw the image
if( img != null)
g.drawImage(img,0,0, this);
}
}
//class image frame periexei tin methodo createloadimage i opoia pernei
//to path apo ton filechooser kai kanei load tin eikona
class ImageFrame{
public static void createLoadImage(){
try
{
//create frame
JFrame f = new JFrame();
//ask for image file
JFileChooser chooser = new JFileChooser();
chooser.showOpenDialog(f);
//create panel with selected file
LabelDemo panel = new LabelDemo( chooser.getSelectedFile().getPath() );
//add panel to pane
f.getContentPane().add(panel);
//show frame
f.setBounds(0,0,800,800);
f.setVisible(true);
}
catch(Exception e)
{
System.out.println ( "Den dialeksate eikona!");
}
}
}
我希望图像在我的主窗口中打开,而不是在新窗口中打开。我怎样才能做到呢?
This is my code from the main frame window:
public class DynamicalSystem {
public static void createAndShowGraphic() {
//Create and set up the window.
JFrame frame = new JFrame("Dynamical System: The beauty of Chaos");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel emptyLabel = new JLabel("");
emptyLabel.setPreferredSize(new Dimension(500, 500));
frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);
//Display the window.
MenuLook menubar = new MenuLook(); //display menubar
frame.setJMenuBar(menubar.createMenuBar());
frame.pack();
frame.setVisible(true);
}
}
and this is from my bufferdimage:
public class LabelDemo extends JPanel
{
//path of image
private String path;
//image object
private Image img;
public LabelDemo(String path) throws IOException
{
//save path
this.path = path;
//load image
img = ImageIO.read(new File(path));
}
//override paint method of panel
public void paint(Graphics g)
{
//draw the image
if( img != null)
g.drawImage(img,0,0, this);
}
}
//class image frame periexei tin methodo createloadimage i opoia pernei
//to path apo ton filechooser kai kanei load tin eikona
class ImageFrame{
public static void createLoadImage(){
try
{
//create frame
JFrame f = new JFrame();
//ask for image file
JFileChooser chooser = new JFileChooser();
chooser.showOpenDialog(f);
//create panel with selected file
LabelDemo panel = new LabelDemo( chooser.getSelectedFile().getPath() );
//add panel to pane
f.getContentPane().add(panel);
//show frame
f.setBounds(0,0,800,800);
f.setVisible(true);
}
catch(Exception e)
{
System.out.println ( "Den dialeksate eikona!");
}
}
}
I want the image to open in my main window not in a new one. How I can do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许我遗漏了一些东西,但看起来你实际上根本没有将图像放入主框架中,从你的问题来看,它似乎是 DynamicalSystem。相反,看起来您正在 ImageFrame 中创建一个新窗口并将图像放在那里。尝试从 DynamicalSystem 调用
LabelDemo panel = new LabelDemo( Chooser.getSelectedFile().getPath() );
并将 LabelDemo 放入该帧而不是 ImageFrame 中。
Maybe I'm missing something, but it looks like at no point are you actually putting your image in the main frame, which from your question appears to be DynamicalSystem. Instead, it looks like you're creating a new window in ImageFrame and putting the image there instead. Try calling
LabelDemo panel = new LabelDemo( chooser.getSelectedFile().getPath() );
from DynamicalSystem and putting the LabelDemo in that frame instead of ImageFrame.