我得到了一个任务,我不得不在Java中的PROGRAMM接受一个Image.Creates 3个按钮命名(左对齐,右和中心)。2个文本框的宽度和高度,我可以输入数字和有一个按钮resize.The 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);
}
}
编辑:我做了一个程序,它改变了网站的徽标与trashgod的帮助,此外,我试图用做我的形象改变URL图像加载改变它有点imageLabel = new JLabel(new ImageIcon("PewPew.jpg"));
和它的作品too.The调整大小是会被还挺做了同样的方式,它的左对齐虽然我应该“链接”文本框中的数字吧?我还需要做出setPreferedSize并以某种方式得到文本框的宽度和高度?
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:我做了更接近我需要做,我尝试添加菜单太像原来PROGRAMM菜单栏菜单栏=新的JMenuBar()一PROGRAMM;
this.setJMenuBar(menuBar);
JMenu fileMenu = new JMenu("Options");
menuBar.add(fileMenu);
JMenuItem redMenuItem = new JMenuItem("Reset");
fileMenu.add(redMenuItem);
而我得到的是不承认this.setJMenuBar.Also我尝试过很多办法,我不能让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);
}
});
}
}