在Swing中添加ActionEvent

class ImageComponent extends JComponent 

implements MouseListener, MouseMotionListener {

private final BufferedImage img;

private Point p1, p2;

public ImageComponent(File file) throws IOException {

img = ImageIO.read(file);

setPreferredSize(new Dimension(img.getWidth(), img.getHeight()));

addMouseListener(this);

addMouseMotionListener(this);

}

@Override protected void paintComponent(Graphics g) {

super.paintComponent(g);

g.drawImage(img, 0, 0, img.getWidth(), img.getHeight(), this);

if (p1 != null && p2 != null)

g.drawLine(p1.x, p1.y, p2.x, p2.y);

}

@Override public void mousePressed(MouseEvent e) {

p1 = e.getPoint();

}

@Override public void mouseDragged(MouseEvent e) {

mouseReleased(e);

}

@Override public void mouseReleased(MouseEvent e) {

p2 = e.getPoint();

repaint();

}

@Override public void mouseMoved(MouseEvent e) {}

@Override public void mouseClicked(MouseEvent e) {}

@Override public void mouseEntered(MouseEvent e) {}

@Override public void mouseExited(MouseEvent e) {}

public static void main(String[] args) throws Exception {

JFileChooser chooser=new JFileChooser();

chooser.setCurrentDirectory(new File(" "));

chooser.showOpenDialog(new JFrame());

File file=chooser.getSelectedFile();

final ImageComponent image = new ImageComponent(file);

JFrame frame = new JFrame("Test");

frame.add(new JScrollPane(image));

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setSize(400, 300);

frame.setVisible(true);

}

}

在此代码中,它直接使用文件JFileChooser。 现在我想添加一个按钮,当我点击时,它应该生成一个ActionEvent,并且应该打开一个JFileChooser,我可以从中选择一个文件,然后它应该在框架上向我显示该图像。我怎样才能做到这一点? 我创建了类似的东西在Swing中添加ActionEvent

JButton open_button=new JButton("Add File"); 

open_button.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

try {

JFileChooser chooser = new JFileChooser();

chooser.setCurrentDirectory(new File("."));

chooser.showOpenDialog(new JFrame());

File file = chooser.getSelectedFile();

ImageComponent image=new ImageComponent(file);

} catch (IOException ex) {

JOptionPane.showMessageDialog(null, "IOException Occured");

}

}

但它不工作..

回答:

此答案仅供参考(请参阅原始的question/answer):

public static void main(String[] args) { 

final JFrame frame = new JFrame("Test");

final ImageComponent imageComponent = new ImageComponent();

frame.add(new JButton(new AbstractAction("Load") {

@Override

public void actionPerformed(ActionEvent e) {

JFileChooser c = new JFileChooser(new File("."));

if (c.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION) {

try {

imageComponent.set(ImageIO.read(c.getSelectedFile()));

} catch (IOException e1) {

e1.printStackTrace();

}

}

}

}), BorderLayout.NORTH);

frame.add(new JScrollPane(imageComponent), BorderLayout.CENTER);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setSize(400, 300);

frame.setVisible(true);

}

而且ImageComponent

static class ImageComponent extends JComponent 

implements MouseListener, MouseMotionListener {

BufferedImage img;

Point p1, p2;

public ImageComponent() {

addMouseListener(this); addMouseMotionListener(this);

}

public void set(BufferedImage img) {

this.img = img;

setPreferredSize(new Dimension(img.getWidth(), img.getHeight()));

repaint();

}

@Override protected void paintComponent(Graphics g) {

super.paintComponent(g);

if (img != null)

g.drawImage(img, 0, 0, img.getWidth(), img.getHeight(), this);

if (p1 != null && p2 != null)

g.drawLine(p1.x, p1.y, p2.x, p2.y);

}

@Override public void mousePressed(MouseEvent e) {

p1 = e.getPoint();

}

@Override public void mouseDragged(MouseEvent e) {

mouseReleased(e);

}

@Override public void mouseReleased(MouseEvent e) {

p2 = e.getPoint();

repaint();

}

@Override public void mouseMoved(MouseEvent e) {}

@Override public void mouseClicked(MouseEvent e) {}

@Override public void mouseEntered(MouseEvent e) {}

@Override public void mouseExited(MouseEvent e) {}

}

回答:

添加按钮在frame(在JFrame的)对象,并也addActionListener方法,对于按钮单击的Java

http://leepoint.net/notes-java/GUI/components/20buttons/10jbutton.html

以上是 在Swing中添加ActionEvent 的全部内容, 来源链接: utcz.com/qa/260832.html

回到顶部