java:分别用不同的类来控制同一个窗体显示不同的图片
比如我想用一个Draw1类在窗体Frame1中显示一张图片,然后再用另外一个类Draw2在窗体Frame1中显示另外一张图片。该怎么做?给出具体的代码...
比如我想用一个Draw1类在窗体Frame1中显示一张图片,然后再用另外一个类Draw2在窗体Frame1中显示另外一张图片。该怎么做?给出具体的代码
展开
4个回答
2013-12-19
展开全部
三个类 假设 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);
}
});
}
}
//====
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);
}
});
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-12-19
展开全部
把Graphics 传过去,估计就可以的,
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-12-19
展开全部
img=toolkit.getImage("E:\\leidian\\dj.jpg");
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-12-19
展开全部
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);
}
}
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);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询