我做了一些研究,但我无法找出问题所在,所以我希望有人可以帮助我。我想制作一个程序,当我点击不同的按钮时,会得到一个图像并改变它的长度,宽度和颜色。它工作到目前为止,但我有按钮的问题。首先,我想创建自己的Button类,但它们没有那么好用,所以我决定在测试类的主要方法中实现Buttons,如下所示:
JButton test = new JButton("test");
test.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
BufferedImage myPicture = ImageIO.read(new File("myImage.jpg"));
JPanel p = new DrawNew(myPicture);
center.removeAll();
center.add(p);
} catch (IOException ex) {
System.out.println("didnt work");
System.exit(0);
}
}
});
问题是,当我按下按钮时,它会做任何事情。我只尝试了center.removeAll()但它没有做任何事情。看起来Button中的中心似乎与主类中心不同。这是主要的课程:
package app.exercise.test;
import app.exercise.data.JButtonExit;
import app.exercise.data.JButtonOriginal;
import app.exercise.data.JButtonGrayscale;
import app.exercise.data.JButtonPattern;
import app.exercise.data.DrawNew;
import java.awt.BorderLayout;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class AppTest {
public static void main(String[] args) {
JFrame f = new JFrame("App");
JPanel center = new JPanel();
JPanel bottom = new JPanel();
try {
BufferedImage myPicture = ImageIO.read(new File("myImage.jpg"));
JPanel p = new DrawNew(myPicture);
center.add(p);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} catch (IOException ex) {
System.out.println("didnt work");
System.exit(0);
}
JButton exitButton = new JButton("Exit");
JButtonExit j = new JButtonExit();
exitButton.addActionListener(j);
int h =1;
JButton test = new JButton("test");
test.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
BufferedImage myPicture = ImageIO.read(new File("myImage.jpg"));
JPanel p = new DrawNew(myPicture);
center.removeAll();
center.add(p);
} catch (IOException ex) {
System.out.println("didnt work");
System.exit(0);
}
}
});
JButton originalButton = new JButton("Original");
JButtonOriginal k = new JButtonOriginal();
originalButton.addActionListener(k);
JButton grayscaleButton = new JButton("Grayscale");
JButtonGrayscale l = new JButtonGrayscale();
grayscaleButton.addActionListener(l);
JButton patternButton = new JButton("Pattern");
JButtonPattern m = new JButtonPattern();
patternButton.addActionListener(m);
Box boxBottom = Box.createHorizontalBox();
boxBottom.add(Box.createHorizontalGlue());
boxBottom.add(exitButton);
Box topBox = Box.createHorizontalBox();
topBox.add(originalButton);
topBox.add(grayscaleButton);
topBox.add(patternButton);
topBox.add(test);
f.add(topBox, BorderLayout.NORTH);
f.add(center, BorderLayout.CENTER);
f.add(boxBottom, BorderLayout.SOUTH);
f.pack();
f.setVisible(true);
}
}
DrawNew类看起来像这样:
package app.exercise.data;
import java.awt.BorderLayout;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.awt.image.ColorModel;
public class DrawNew extends JPanel {
private int height;
private int width;
private BufferedImage myPic;
public DrawNew (BufferedImage myPicture) {
height = myPicture.getHeight();
width = myPicture.getWidth();
myPic = myPicture;
}
public Dimension getPreferredSize() {
return new Dimension(width*2, height*2);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
int select = 1;
switch(select) {
case 1: select = 1;
for (int i = 0; i < width; i++) {
for (int k = 0; k<height;k++) {
Color mycolor = new Color(myPic.getRGB(i,k));
g.setColor(mycolor);
g.fillRect(2*i,2*k,2,2);
}
}
break;
case 2: select = 2;
for (int i = 0; i < width; i++) {
for (int k = 0; k<height;k++) {
Color mycolor = new Color(myPic.getRGB(i,k));
int h = mycolor.getRed();
int t = mycolor.getRed();
int f = mycolor.getRed();
int mittel = (h+f+t)/3;
Color newColor = new Color (mittel,mittel,mittel);
g.setColor(newColor);
g.fillRect(2*i,2*k,2,2);
}
}
break;
case 3: select = 3;
for (int i = 0; i < width; i++) {
for (int k = 0; k<height;k++) {
Color mycolor = new Color(myPic.getRGB(i,k));
int h = mycolor.getRed();
int t = mycolor.getRed();
int f = mycolor.getRed();
int mittel = (h+f+t)/3;
if (mittel <= 51) {
g.setColor(Color.BLACK);
g.fillRect(2*i,2*k,2,2);
} else if(mittel <=102) {
g.setColor(Color.WHITE);
g.drawLine(2*i,2*k,2*i,2*k);
g.setColor(Color.BLACK);
g.drawLine(2*i+1,2*k+1,2*i+1,2*k+1);
g.drawLine(2*i+1,2*k,2*i+1,2*k);
g.drawLine(2*i,2*k+1,2*i,2*k+1);
} else if(mittel <=153) {
g.setColor(Color.BLACK);
g.drawLine(2*i,2*k,2*i,2*k);
g.drawLine(2*i+1,2*k+1,2*i+1,2*k+1);
g.setColor(Color.WHITE);
g.drawLine(2*i+1,2*k,2*i+1,2*k);
g.drawLine(2*i,2*k+1,2*i,2*k+1);
} else if(mittel <=204) {
g.setColor(Color.BLACK);
g.drawLine(2*i,2*k,2*i,2*k);
g.setColor(Color.WHITE);
g.drawLine(2*i+1,2*k,2*i+1,2*k);
g.drawLine(2*i,2*k+1,2*i,2*k+1);
g.drawLine(2*i+1,2*k+1,2*i+1,2*k+1);
} else if(mittel <=255) {
g.setColor(Color.WHITE);
g.fillRect(2*i,2*k,2,2);
}
}
}
break;
}
}
}
所以我的问题是:如何在我的Button中输入center.add(p)。它起初工作没有任何问题,但由于某些原因不在按钮中。
我感谢任何帮助。