Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 有关ActionListener.image编辑的问题_Java_Swing_Imageicon - Fatal编程技术网

Java 有关ActionListener.image编辑的问题

Java 有关ActionListener.image编辑的问题,java,swing,imageicon,Java,Swing,Imageicon,我得到了一个任务,我必须用Java制作一个程序来拍摄图像。创建3个名为(左对齐、右对齐和居中对齐)的按钮。2个文本字段宽度和高度,我可以在那里输入数字,还有一个调整大小的按钮。4个按钮(左对齐、右对齐、居中对齐和调整大小)必须将图像位置更改为左右或中间,并根据数字分别调整图像大小 我写的代码只是为了向左走,但我不知道该怎么做…我也不知道该怎么调整大小…有人能帮我吗 import java.awt.*; import javax.swing.*; import java.awt.event.*;

我得到了一个任务,我必须用Java制作一个程序来拍摄图像。创建3个名为(左对齐、右对齐和居中对齐)的按钮。2个文本字段宽度和高度,我可以在那里输入数字,还有一个调整大小的按钮。4个按钮(左对齐、右对齐、居中对齐和调整大小)必须将图像位置更改为左右或中间,并根据数字分别调整图像大小

我写的代码只是为了向左走,但我不知道该怎么做…我也不知道该怎么调整大小…有人能帮我吗

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JPanel;

public class Destructor extends JFrame implements ActionListener {

    private JButton red, blue, white, resize;

    public Destructor(String title) {
        super(title);
        Container contentPane = this.getContentPane();
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        red = new JButton("Align Left");
        //red.addActionListener(this);
        blue = new JButton("Align Center");
        blue.addActionListener(this);
        white = new JButton("Align Right");
        white.addActionListener(this);
        //read the Image
        // try {                
        // BufferedImage pic1 = ImageIO.read(new File("PewPew.jpg"));
        //JLabel picLabel = new JLabel(new ImageIcon( pic1 ));
        //add( picLabel );
        ImageIcon pic1 = new ImageIcon("PewPew.jpg");
        add(new JLabel(pic1));
        // } catch (IOException ex) {
        // handle exception...
        // }

        //Action LIsteners
        //add the buttons to the frame
        JPanel north = new JPanel();
        north.add(red);
        north.add(blue);
        north.add(white);
        contentPane.add(north, BorderLayout.NORTH);
        //THe Under Panel
        JPanel south = new JPanel();
        south.setLocation(250, 30);
        resize = new JButton("Resize");
        JLabel Width = new JLabel("Width :");
        JLabel Height = new JLabel("Height :");
        //The text field
        JTextField times = new JTextField();
        JTextField times2 = new JTextField();
        Width.setLabelFor(times);
        Height.setLabelFor(times2);
        Width.setLocation(120, 0);

        south.add(Width);
        south.add(times, BorderLayout.NORTH);
        south.add(Height);
        south.add(times2, BorderLayout.SOUTH);
        south.add(resize);
        contentPane.add(south, BorderLayout.SOUTH);
        GridLayout lay2 = new GridLayout(3, 2);
        south.setLayout(lay2);

        //create a menu bar and attach it to this JFrame
        JMenuBar menuBar = new JMenuBar();
        this.setJMenuBar(menuBar);

        JMenu fileMenu = new JMenu("Options");

        menuBar.add(fileMenu);

        JMenuItem redMenuItem = new JMenuItem("Reset");

        fileMenu.add(redMenuItem);



    }
//Trying to move the picture to the left
    public void actionPerformed(ActionEvent a) {
        contentPane.add(pic1, BorderLayout.NORTH);
    }

    public static void main(String[] args) {
        Destructor f = new Destructor("Image App");
        f.setSize(600, 600);
        f.setVisible(true);
    }
}
编辑:我制作了一个程序,在垃圾神的帮助下更改了网站的徽标 此外,我还尝试通过改变URL图像加载和我的图像来改变它
imageLabel=newjlabel(newimageicon(“PewPew.jpg”),它也能工作。调整大小的方式将与向左对齐的方式类似,尽管我应该将文本字段数字“链接”到右侧?我还需要设置PreferredSize并以某种方式获得文本字段的宽度和高度

import java.awt.*;
import java.awt.event.*;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.*;

/** @see http://stackoverflow.com/a/10610126/230513 */
public class AlignImage extends JPanel {

    private JPanel controlPanel = new JPanel();
    private JLabel imageLabel;

    public AlignImage() {
        super(new GridLayout());
        try {
            imageLabel = new JLabel(new ImageIcon(new URL(
                "http://sstatic.net/stackoverflow/img/logo.png")));
        } catch (MalformedURLException ex) {
            ex.printStackTrace(System.err);
        }
        this.add(imageLabel);
        controlPanel.add(new JButton(new AbstractAction("Align Left") {

            @Override
            public void actionPerformed(ActionEvent e) {
                align(JLabel.LEFT);
            }
        }));
          controlPanel.add(new JButton(new AbstractAction("Align Center") {

            @Override
            public void actionPerformed(ActionEvent e) {
                align(JLabel.CENTER);
            }
        }));
        controlPanel.add(new JButton(new AbstractAction("Align Right") {

            @Override
            public void actionPerformed(ActionEvent e) {
                align(JLabel.RIGHT);
            }
        }));


    }

    @Override
    public Dimension getPreferredSize() {
        int w = 3 * imageLabel.getIcon().getIconWidth() / 2;
        int h = 3 * imageLabel.getIcon().getIconHeight() / 2;
        System.out.println(w + " " + h);
        return new Dimension(w, h);
    }

    private void align(int alignment) {
        imageLabel.setHorizontalAlignment(alignment);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame f = new JFrame("Align Left");
                f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
                AlignImage ai = new AlignImage();
                f.add(ai, BorderLayout.CENTER);
                f.add(ai.controlPanel, BorderLayout.NORTH);
                f.pack();
                f.setLocationRelativeTo(null);
                f.setVisible(true);
                JFrame f1 = new JFrame("Align Center");
                f1.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
                 JFrame f2 = new JFrame("Align Right");
                f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

                f2.add(ai, BorderLayout.CENTER);
                f2.add(ai.controlPanel, BorderLayout.NORTH);
                f2.pack();
                f2.setLocationRelativeTo(null);
                f2.setVisible(true);
                f1.add(ai, BorderLayout.CENTER);
                f1.add(ai.controlPanel, BorderLayout.NORTH);
                f1.pack();
                f1.setLocationRelativeTo(null);
                f1.setVisible(true);
               ;

            }
        });

    }
}
编辑2:我制作了一个更接近我需要做的程序。我尝试添加菜单,就像原始程序MenuBar MenuBar=new JMenuBar()一样

我得到一个错误,无法识别这个。setJMenuBar。我也尝试了很多方法,我无法使3个按钮变北,调整大小和文本字段变南…我做错了什么?代码:

import java.awt.*;
import java.awt.event.*;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.*;

/** @see http://stackoverflow.com/a/10610126/230513 */
public class AlignImage extends JPanel {

    private JPanel controlPanel = new JPanel();
    private JLabel imageLabel;

    public AlignImage() {
        super(new GridLayout());

            imageLabel = new JLabel(new ImageIcon("PewPew.jpg"));

        this.add(imageLabel);
        controlPanel.add(new JButton(new AbstractAction("Align Left") {

            @Override
            public void actionPerformed(ActionEvent e) {
                align(JLabel.LEFT);
            }
        }));
          controlPanel.add(new JButton(new AbstractAction("Align Center") {

            @Override
            public void actionPerformed(ActionEvent e) {
                align(JLabel.CENTER);
            }
        }));
        controlPanel.add(new JButton(new AbstractAction("Align Right") {

            @Override
            public void actionPerformed(ActionEvent e) {
                align(JLabel.RIGHT);
            }
        }));

        JPanel south= new JPanel();

    JButton resize=new JButton("Resize");
   JLabel Width = new JLabel("Width :");
   JLabel Height = new JLabel("Height :");
   //The text field
   JTextField times= new JTextField();
   JTextField times2= new JTextField();
   Width.setLabelFor(times);
   Height.setLabelFor(times2);


  south.add(Width);
  south.add(times, BorderLayout.NORTH);
  south.add(Height);
  south.add(times2, BorderLayout.SOUTH);
  south.add(resize);
   controlPanel.add(south, BorderLayout.SOUTH);
    GridLayout lay2 = new GridLayout(3,2); south.setLayout(lay2);
        JMenuBar menuBar = new JMenuBar();

    }

    @Override
    public Dimension getPreferredSize() {
        int w = 3 * imageLabel.getIcon().getIconWidth() / 2;
        int h = 3 * imageLabel.getIcon().getIconHeight() / 2;
        System.out.println(w + " " + h);
        return new Dimension(w, h);
    }

    private void align(int alignment) {
        imageLabel.setHorizontalAlignment(alignment);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame f = new JFrame("Align Left");
                f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
                AlignImage ai = new AlignImage();
                f.add(ai, BorderLayout.CENTER);
                f.add(ai.controlPanel, BorderLayout.NORTH);
                f.pack();
                f.setLocationRelativeTo(null);
                f.setVisible(true);
                JFrame f1 = new JFrame("Align Center");
                f1.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
                 JFrame f2 = new JFrame("Align Right");
                f2.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

                f2.add(ai, BorderLayout.CENTER);
                f2.add(ai.controlPanel, BorderLayout.NORTH);
                f2.pack();
                f2.setLocationRelativeTo(null);
                f2.setVisible(true);
                f1.add(ai, BorderLayout.CENTER);
                f1.add(ai.controlPanel, BorderLayout.NORTH);
                f1.pack();
                f1.setLocationRelativeTo(null);
                f1.setVisible(true);
               JFrame f3 = new JFrame("Res");
                f3.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

                f3.add(ai, BorderLayout.CENTER);
                f3.add(ai.controlPanel, BorderLayout.SOUTH);
                f3.pack();
                f3.setLocationRelativeTo(null);
                f3.setVisible(true);

            }
        });

    }
}

两种方法很常见:

  • 给定一个合适的参数,在调用
    setHorizontalAlignment()
    时使用常数
    JLabel.LEFT
    JLabel.CENTER
    JLabel.RIGHT
    中的一个;如果需要,请使用
    validate()
    repaint()

  • 覆盖
    paintComponent()
    并使用
    drawImage()
    在所需坐标处渲染
    图像。缩放是自动的。左对齐很容易:

    int w = Integer.valueOf(width.getText()); // formerly times
    int h = Integer.valueOf(height.getText()); // formerly times2
    g.drawImage(image, 0, 0, w, h, null);
    
    中间、右侧和异常处理作为练习进行

附录:以下说明了单个路线的第一种方法;尝试使用现有按钮作为向导添加其他两个按钮

import java.awt.*;
import java.awt.event.*;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.*;

/** @see http://stackoverflow.com/a/10610126/230513 */
public class AlignImage extends JPanel {

    private JPanel controlPanel = new JPanel();
    private JLabel imageLabel;

    public AlignImage() {
        super(new GridLayout());
        try {
            imageLabel = new JLabel(new ImageIcon(new URL(
                "http://sstatic.net/stackoverflow/img/logo.png")));
        } catch (MalformedURLException ex) {
            ex.printStackTrace(System.err);
        }
        this.add(imageLabel);
        controlPanel.add(new JButton(new AbstractAction("Align Left") {

            @Override
            public void actionPerformed(ActionEvent e) {
                align(JLabel.LEFT);
            }
        }));
    }

    @Override
    public Dimension getPreferredSize() {
        int w = 3 * imageLabel.getIcon().getIconWidth() / 2;
        int h = 3 * imageLabel.getIcon().getIconHeight() / 2;
        System.out.println(w + " " + h);
        return new Dimension(w, h);
    }

    private void align(int alignment) {
        imageLabel.setHorizontalAlignment(alignment);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame f = new JFrame("Align Left");
                f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
                AlignImage ai = new AlignImage();
                f.add(ai, BorderLayout.CENTER);
                f.add(ai.controlPanel, BorderLayout.SOUTH);
                f.pack();
                f.setLocationRelativeTo(null);
                f.setVisible(true);
            }
        });

    }
}

因此,您不知道如何将图像与
JPanel
的左/中/右对齐?要求你扔掉一段巨大的代码,基本上要求我们完成你的任务?这是你问题的正确摘要吗?我建议你试着写一篇文章,尝试将某个东西向左对齐,如果你没有成功,你可以发布代码并请求帮助。实际上,我试着这样做,public void actionPerformed(ActionEvent a){contentPane.add(pic1,BorderLayout.NORTH);}但是它不起作用,我不知道/可以找到另一种方法来做这件事。编辑代码以便你能更好地看到它。这是一项作业。我不会简单地给你代码。您需要研究Swing中可用的不同布局管理器。一个很好的起点是,事实上我曾经尝试过这样做……事实上,我去java实验室寻求帮助,他们不知道该怎么做,我想我可以得到一些帮助……正如你所看到的,我编辑了代码并写了一条注释,实际上我试图将图像移到左侧。我尝试过编译,但它没有将图像识别为我想是对象吗?我在问一些相互作用/提示…我不想要快速或简单的解决方案我做了一些搜索,因为我知道有人以奇妙的方式回答了这个问题,这里是指向这个奇妙答案的链接,也可以通过@HovercraftFullOfEels应用到您的场景中(每个答案可能会增加您的知识)我试着做了第一件事和第二件事,虽然我不确定,但事情是在我的ActionPerformed方法中,它根本不识别标签。它说找不到变量label1。请编辑您的问题,包括一个显示您当前方法的。可能是一个方便的占位符。为什么文本字段对移动我的图像很重要?我尝试使用public void actionPerformed(ActionEvent a){pic1/label1.drawImage(image,120,50,300,300,null);}随机数,但它无法识别我的pic1/label1,因此它可以真正移动它,我不知道为什么删除注释不是一个好方法。我在上面添加了一个名为
AlignImage
的完整示例,其中有一个练习供您完成。如果您不理解某些内容,请编辑您的问题以显示您的工作版本的
AlignImage
。非常好;要更改大小,您必须单独加载图像并将其删除。这应该是一个新问题。当你发布你的sscce时,别忘了使用。
import java.awt.*;
import java.awt.event.*;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.*;

/** @see http://stackoverflow.com/a/10610126/230513 */
public class AlignImage extends JPanel {

    private JPanel controlPanel = new JPanel();
    private JLabel imageLabel;

    public AlignImage() {
        super(new GridLayout());
        try {
            imageLabel = new JLabel(new ImageIcon(new URL(
                "http://sstatic.net/stackoverflow/img/logo.png")));
        } catch (MalformedURLException ex) {
            ex.printStackTrace(System.err);
        }
        this.add(imageLabel);
        controlPanel.add(new JButton(new AbstractAction("Align Left") {

            @Override
            public void actionPerformed(ActionEvent e) {
                align(JLabel.LEFT);
            }
        }));
    }

    @Override
    public Dimension getPreferredSize() {
        int w = 3 * imageLabel.getIcon().getIconWidth() / 2;
        int h = 3 * imageLabel.getIcon().getIconHeight() / 2;
        System.out.println(w + " " + h);
        return new Dimension(w, h);
    }

    private void align(int alignment) {
        imageLabel.setHorizontalAlignment(alignment);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame f = new JFrame("Align Left");
                f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
                AlignImage ai = new AlignImage();
                f.add(ai, BorderLayout.CENTER);
                f.add(ai.controlPanel, BorderLayout.SOUTH);
                f.pack();
                f.setLocationRelativeTo(null);
                f.setVisible(true);
            }
        });

    }
}