三个类 假设 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);
}
});
}
}
取消
评论