java:分别用不同的类来控制同一个窗体显示不同的图片

2025-01-05 02:42:04
推荐回答(4个)
回答1:

三个类 假设 D 盘根目录下 有 1.jpg 2.jpg两个图片
//====
import javax.swing.ImageIcon;

public class Frame1 extends javax.swing.JFrame {

public Frame1() {
initComponents();
}

private void initComponents() {

jLabel1 = new javax.swing.JLabel();

new Draw1().draw();

setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
);

pack();
}

public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Frame1().setVisible(true);
}
});
}

private javax.swing.JLabel jLabel1;

private class Draw1{
//图片路径
String fileName= "D:/1.jpg";

void draw(){
jLabel1 = new javax.swing.JLabel();
jLabel1.setIcon(new ImageIcon(fileName));
}
}
}
//====
import javax.swing.ImageIcon;

public class Frame2 extends javax.swing.JFrame {

public Frame2() {
initComponents();
}

private void initComponents() {

jLabel1 = new javax.swing.JLabel();

new Draw2().draw();

setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
);

pack();
}

private javax.swing.JLabel jLabel1;

private class Draw2{
//图片路径
String fileName= "D:/2.jpg";

void draw(){
jLabel1 = new javax.swing.JLabel();
jLabel1.setIcon(new ImageIcon(fileName));
}
}
}
//====
//测试类
public class Test {

public static void main(String[] args){
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Frame1().setVisible(true);
}
});

java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Frame2().setVisible(true);
}
});
}
}

回答2:

把Graphics 传过去,估计就可以的,

回答3:

img=toolkit.getImage("E:\\leidian\\dj.jpg");

回答4:

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.JFrame;

public class Gtest extends JFrame
{

Draw d;
public Gtest()
{
this.setSize(300, 500);
d=new Draw(this.getGraphics(),this);
this.show();
}
public static void main(String[] args)
{
new Gtest();

}

}
class Draw
{
Toolkit toolkit;
Image img;
public Draw(Graphics g,Gtest t)
{
toolkit=Toolkit.getDefaultToolkit();
img=toolkit.getImage("E:\\leidian\\dj.jpg");
g.drawImage(img, 0, 0, t);
}

}