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

三个类 假设 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);

}

});

}

}

取消

评论

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值