有关ActionListener.image编辑的问题 [英] Problems about ActionListener.image editing
问题描述
我得到一个分配,我必须用Java编写一个程序来获取图像.创建3个名为(左对齐,右对齐和居中对齐)的按钮.2个文本字段Width和Height,可以在其中输入数字并调整按钮的大小.四个按钮(左对齐,右对齐,居中和调整大小)必须分别将图像位置更改为左对齐或居中并按数字分别调整图像的大小
I got an assignment that i have to make a programm in Java that takes an Image.Creates 3 Buttons named(Align left,right and center).2 textfields Width and Height that i can input numbers there and a button resize.The 4 buttons(align left,right,center and resize) have to change the image place to left right or center and resize the image as the numbers were given respectively
我只为左走而编写了代码,但我不知道该怎么做...我也不知道该怎么做...可以有人帮我吗?
I have written the code just for going left but i cant figure what to do...I also have no idea what to do on resizing...Can someone help me?
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 = new JLabel(new ImageIcon("PewPew.jpg"));
并且它也可以工作.虽然我应该链接"文本字段数字正确,但调整大小的方式与左对齐的方式相同?我还需要设置setPreferedSize并以某种方式获取文本字段的宽度和高度?
I made a program that changes the Logo of the site with the help of trashgod
Also i tried to change it a bit by changing the URL image loading with my image by doing
imageLabel = new JLabel(new ImageIcon("PewPew.jpg"));
and it works too.The Resize is gonna be done kinda the same way that its aligned left though i should "link" the text field numbers right?I need to also make a setPreferedSize and somehow get the width and height of textfields?
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);
;
}
});
}
}
我制作的程序更接近我需要做的程序,我也尝试添加菜单,就像原始程序MenuBar menuBar = new JMenuBar();
Edit 2: I made a programm that comes closer to what i need to do.I try to add the menu too like the original programm MenuBar menuBar = new JMenuBar();
this.setJMenuBar(menuBar);
JMenu fileMenu = new JMenu("Options");
menuBar.add(fileMenu);
JMenuItem redMenuItem = new JMenuItem("Reset");
fileMenu.add(redMenuItem);
我得到一个无法识别this.setJMenuBar的错误.我也尝试了很多方法,但我无法使3个按钮向北移动,而调整大小和文本字段却向南...我在做什么错了?代码:>
and i get an error that does not recognize this.setJMenuBar.Also i tried many ways and i cant make the 3 buttons go north and the resize and textfields go south...What i am doing wrong?Code:
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()
进行此操作.
Given a suitable layout manager, use one of the constants
JLabel.LEFT
,JLabel.CENTER
orJLabel.RIGHT
in your call tosetHorizontalAlignment()
; follow this withvalidate()
andrepaint()
, if required.
覆盖paintComponent()
,并使用drawImage()
在所需坐标处渲染Image
.缩放是自动的.左对齐很容易:
Override paintComponent()
and use drawImage()
to render the Image
at the desired coordinates. Scaling is automatic. Left alignment is easy:
int w = Integer.valueOf(width.getText()); // formerly times
int h = Integer.valueOf(height.getText()); // formerly times2
g.drawImage(image, 0, 0, w, h, null);
居中,正确和异常处理作为练习.
Center, right and exception handling are left as an exercise.
附录:下面的 sscce 说明了第一种单一对齐方式.尝试使用现有按钮作为指南添加其他两个按钮.
Addendum: The sscce below illustrates the first approach for a single alignment; try adding the other two buttons using the existing button as a guide.
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);
}
});
}
}
这篇关于有关ActionListener.image编辑的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!