我创建了5个java类,他们的目标是将英语翻译成法语,并在单击Jbutton时设置在Jfield1中写入的每个单词的图像。 我的小程序到达将JField1中写的每个英文单词翻译成JField2中的法语单词。我添加了两个类(ImageImplement和ImageInFrame)来设置图像但遗憾的是没有任何工作。我真的不知道如何使用imageArray。 请帮帮我!!!
对不起,我的英语不太好。
这是我的应用
我的第一堂课是StringArrayEnglishWord
public class StringArrayEnglishWords扩展了JPanel {
String[] names;
Graphics2D g2d;
public StringArrayEnglishWords(){
EnglishWords();
// drawNames();
}
public void paintComponent (Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
}
public void EnglishWords(){
names=new String[16];
names[0]="Tomatoes";
names[1]="Chairs";
names[2]="Car";
names[3]="Computer";
names[4]="Flowers";
names[5]="Family";
names[6]="Sister";
names[7]="Husband";
names[8]="God";
names[9]="Book";
names[10]="Watch";
names[11]="Pencil";
names[12]="Bottle";
names[13]="Map";
names[14]="Wife";
names[15]="Bag";
}
public void drawNames(){
for (int i=0; i<names.length; i++){
g2d.drawString(names[i], 50, (1+i)*16);
}
}
public int indexOfWord(String userInput)
{
for(int i=0;i<names.length;i++)
{
if(userInput.equals(names[i])){return i;}
}
return -1;
}
}
我的第二个类是StringArrayFrenchWords:
public class StringArrayFrenchWords扩展了JPanel {
String[] names2;
Graphics2D g2d;
public StringArrayFrenchWords(){
FrenchWorld();
// drawNames();
}
public void paintComponent (Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
}
public void FrenchWorld(){
names2=new String[16];
names2[0]="Tomate";
names2[1]="Chaise";
names2[2]="Voiture";
names2[3]="Ordinateur";
names2[4]="Fleurs";
names2[5]="Familles";
names2[6]="Soeur";
names2[7]="Epoux, Mari";
names2[8]="Dieu";
names2[9]="Livre";
names2[10]="Montre, Regarder";
names2[11]="Crayon";
names2[12]="Bouteille";
names2[13]="Carte,plan";
names2[14]="Femmme";
names2[15]="Sac";
}
public void drawNames(){
for (int i=0; i<names2.length; i++){
g2d.drawString(names2[i], 50, (1+i)*16);
}
}
}
我的第三个Classe是扩展Japplet(译者):
public class Translator extends JApplet实现了ActionListener {
String StringField1, StringImage;
StringArrayEnglishWords Englishw;
StringArrayFrenchWords Frenchw;
ImageInJframe img;
ImageImplement Images;
Image Image;
JButton[] buttons;
JComboBox[] comboBoxes;
JButton TranslateButton;
JPanel JPanel0,JPanel1;
JPanel buttonPanel;
JPanel comboBoxesPanel;
JTextField field1,field2;
ImageIcon Imageicon;
JLabel ImageLabel;
public void init(){
setUpFields();
Englishw=new StringArrayEnglishWords();
Frenchw=new StringArrayFrenchWords();
img = new ImageInJframe(); // this was for set up the image
setUpimages();// for setup image too
}
public void setUpFields() {
TranslateButton = new JButton("TranslateButton");
field1 = new JTextField(10);
field2 = new JTextField(10);
field2.setEditable(false);
JPanel0 = new JPanel(new FlowLayout());
JPanel0.add(field1);
TranslateButton.addActionListener(this);
field1.addActionListener(this);
field2.addActionListener(this);
JPanel0.add(TranslateButton);
JPanel0.add(field2);
add(JPanel0, BorderLayout.SOUTH);
}
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
}
public void setUpimages() {//setUp images too
ImageLabel.setIcon((Icon) img);
JPanel1 = new JPanel(new FlowLayout());
JPanel1.add(ImageLabel);
add(JPanel1, BorderLayout.CENTER);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == TranslateButton) {
System.out.println(field1);
StringField1 = field1.getText();
int index = Englishw.indexOfWord(StringField1);//this will give you the index of the word
if (index == -1) {
field2.setText("Word not found.");
System.out.println(field2);
} else {
field2.setText(Frenchw.names2[index]);
System.out.println("English Words=" + field1.getText());
System.out.println("French Words =" + field2.getText());
//setup Image
Image = img.getIconImage();
int index2 = img.indexOfImage(StringImage);
JPanel1.add(img);
}
}
}
}
我的两个课程是使用JFame和imageArray设置图像:ImageInJframe和ImageImplement
公共类ImageInJframe扩展了JFrame {
JFrame Frame;
Image[] pics;
String[] names = {"apple.jpg", "bags.jpg", "bathroom.jpg", "battled.jpg", "car.png",
"chairs.jpg", "Computer.jpg", "family.jpg", "flowers.jpg", "god.jpg", "house.png", "map.jpg"
, "men.jpg", "pencil.jpg", "sisters.jpg", "tomato.jpg", "watch.jpg", "women.jpg", "book.gif"};
final int NUM_PICS=names.length;
Graphics g;
public static void main(String args[])
{
new ImageInJframe().start();
}
public void start()
{
ImageImplement panel = new ImageImplement(new ImageIcon(String.valueOf(pics)).getImage());
add(panel);
setVisible(true);
setSize(400,400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setImage();
}
public void setImage() {
pics = new Image[NUM_PICS];
}
public int indexOfImage(String userInput)
{
for(int i=0;i<pics.length;i++)
{
if(userInput.equals(pics[i])){return i;}
}
return -1;
}
}
ImageImplement扩展JPanel:
公共类ImageImplement扩展了JPanel {
Image img;
public ImageImplement(Image img) {
this.img = img;
Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
setPreferredSize(size);
setMinimumSize(size);
setMaximumSize(size);
setSize(size);
setLayout(null);
}
public void paintComponent(Graphics g) {
g.drawImage(img, 0, 0, null);
}
答案 0 :(得分:2)
根据需要进行绘制,当调用paintComponent
时,您需要重新绘制UI的当前状态。也许你应该首先看看Painting in AWT and Swing和Performing Custom Painting,了解有关绘画如何运作的更多信息
基本上,在paintComponent
,您需要致电draw
,但您应该将传递给Graphics
方法的paintComponent
上下文传递给它。保持对您未创建的Graphics
上下文的引用是危险且不明智的
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new StringArrayEnglishWords());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class StringArrayEnglishWords extends JPanel {
String[] names;
public StringArrayEnglishWords() {
EnglishWords();
}
@Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
drawNames(g2d);
g2d.dispose();
}
public void EnglishWords() {
names = new String[16];
names[0] = "Tomatoes";
names[1] = "Chairs";
names[2] = "Car";
names[3] = "Computer";
names[4] = "Flowers";
names[5] = "Family";
names[6] = "Sister";
names[7] = "Husband";
names[8] = "God";
names[9] = "Book";
names[10] = "Watch";
names[11] = "Pencil";
names[12] = "Bottle";
names[13] = "Map";
names[14] = "Wife";
names[15] = "Bag";
}
public void drawNames(Graphics2D g2d) {
for (int i = 0; i < names.length; i++) {
g2d.drawString(names[i], 50, (1 + i) * 16);
}
}
public int indexOfWord(String userInput) {
for (int i = 0; i < names.length; i++) {
if (userInput.equals(names[i])) {
return i;
}
}
return -1;
}
}
}
在修改Graphics
上下文时也应该小心,它由同一窗口内的所有组件共享,在大多数情况下,我发现拍摄{{1}的快照很有用} context(Graphics
)并使用它,然后我不需要记得在我完成后重置它。
Applet也是一种死技术,该插件已被弃用,不再受支持,在大多数情况下,大多数浏览器都会主动阻止该插件。有关详细信息,请参阅Java Plugin support deprecated和Moving to a Plugin-Free Web
<强>更新强>
更改...
Graphics#create
为...
ImageImplement panel = new ImageImplement(new ImageIcon(String.valueOf(pics)).getImage());
add(panel);
假设图像文件位于执行程序的当前目录中,那么它应该可以工作。如果它们包含在包含类文件的包中,那么您需要使用JLabel panel = new JLabel(new ImageIcon(names[0]).getImage());
add(panel);
获取对图像的引用,并将生成的Class#getResource
传递给URL
您的ImageIcon
面板什么都不做,所以即使它有适当的图像显示,它也不会显示任何内容。
ImageImplement
毫无意义
你发布了一大堆,似乎是不相关的代码,很难知道你指的是什么问题
此...
String.valueOf(pics)
没有意义,ImageLabel.setIcon((Icon) img);
是img
的一个实例,它与ImageInJFrame
甚至有什么关联?
我正式不知道你要做什么