我在互联网上找到了这个代码。我遵守了它,它执行得很好。然后我将构造函数的一部分移动到一个方法并创建一个我试图调用该方法的对象。我不知道我错过了什么,但框架不会出现。将构造函数转移到方法时有什么问题?
工作
import java.awt.event.MouseEvent;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
import java.io.IOException;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
public class MyFrame extends JFrame
{
Container cont;
JLabel label = new JLabel();
public MyFrame() throws IOException
{
setSize(300,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BufferedImage img = ImageIO.read(new File("C:\\Users\\Public\\Pictures\\Sample Pictures\\Chrysanthemum.jpg"));
System.out.println(img.toString());
label.setIcon(new ImageIcon(img));
cont = (Container) getContentPane().add(label);
cont.setFocusable(true);
InputHandler hand = new InputHandler();
cont.addKeyListener(hand);
cont.addMouseListener(hand);
cont.setBackground(Color.BLACK);
}
public class InputHandler implements KeyListener, MouseListener
{
public void keyTyped(KeyEvent kt)
{
System.out.println(kt.getKeyChar());
}
public void keyPressed(KeyEvent kp)
{
if(kp.getKeyChar() == 'w')
{
cont.setBackground(Color.PINK);
repaint();
}
}
public void keyReleased(KeyEvent kr)
{
System.out.println(kr.getKeyChar());
}
@Override
public void mouseClicked(MouseEvent e) {
System.out.println("Mouse CLicked");
}
@Override
public void mousePressed(MouseEvent e) {
System.out.println("Mosue Pressed");
}
@Override
public void mouseReleased(MouseEvent e) {
System.out.println("Mosue Released");
}
@Override
public void mouseEntered(MouseEvent e) {
System.out.println("Mouse Entered");
}
@Override
public void mouseExited(MouseEvent e) {
System.out.println("Mosue Exited");
}
}
public static void main(String[] args) throws IOException {
new MyFrame().setVisible(true);
}
}
编辑:更改的代码(不工作)
import java.awt.event.MouseEvent;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
import java.io.IOException;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
public class MyFrame extends JFrame
{
Container cont;
JLabel label = new JLabel();
void MyFrame(){
}
public void Display() throws IOException
{
setSize(280,480);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BufferedImage img = ImageIO.read(new File("C:\\Users\\Public\\Pictures\\Sample Pictures\\Chrysanthemum.jpg"));
System.out.println(img.toString());
label.setIcon(new ImageIcon(img));
cont = (Container) getContentPane().add(label);
cont.setFocusable(true);
InputHandler hand = new InputHandler();
cont.addKeyListener(hand);
cont.addMouseListener(hand);
cont.setBackground(Color.BLACK);
rootPane.setVisible(true);
}
public class InputHandler implements KeyListener, MouseListener
{
public void keyTyped(KeyEvent kt)
{
System.out.println(kt.getKeyChar());
}
public void keyPressed(KeyEvent kp)
{
if(kp.getKeyChar() == 'w')
{
cont.setBackground(Color.PINK);
repaint();
}
}
public void keyReleased(KeyEvent kr)
{
System.out.println(kr.getKeyChar());
}
@Override
public void mouseClicked(MouseEvent e) {
System.out.println("Mouse CLicked");
}
@Override
public void mousePressed(MouseEvent e) {
System.out.println("Mosue Pressed");
}
@Override
public void mouseReleased(MouseEvent e) {
System.out.println("Mosue Released");
}
@Override
public void mouseEntered(MouseEvent e) {
System.out.println("Mouse Entered");
}
@Override
public void mouseExited(MouseEvent e) {
System.out.println("Mosue Exited");
}
}
public static void main(String[] args) throws IOException {
MyFrame mf = new MyFrame();
mf.Display();
}
}
答案 0 :(得分:3)
只需使用rootPane.setVisible(true);
方法中的setVisible(true);
替换Display()
,它就会显示您的JFrame
。
答案 1 :(得分:0)
你可能做得不对。
http://download.oracle.com/javase/6/docs/api/javax/swing/JFrame.html
public static void main(String [] args){
JFrame jf = new JFrame("JChooser");
jf.setSize(400,400);
jf.setVisible(true);
}
这在主要方法中起作用。
答案 2 :(得分:0)
由于这一行,你得到一个例外:
BufferedImage img = ImageIO.read(new File("C:\\Users\\Public\\Pictures\\Sample Pictures\\Chrysanthemum.jpg"));
因为它无法加载它......因为你的计算机上可能没有这个文件
要查看此工作,请删除以下行:
BufferedImage img = ImageIO.read(new File("C:\\Users\\Public\\Pictures\\Sample Pictures\\Chrysanthemum.jpg"));
System.out.println(img.toString());
label.setIcon(new ImageIcon(img));