JAVA题 高手来
编写一个程序,功能如下,用户当点击第一幅、上一幅、下一幅、最后幅四个按钮,可以浏览程序中加载好的五幅图片,实现一个简单的图片浏览器。要求用JPanel跟JFrame类来实...
编写一个程序,功能如下,用户当点击第一幅、上一幅、下一幅、最后幅四个按钮,可以浏览程序中加载好的五幅图片,实现一个简单的图片浏览器。
要求用JPanel 跟 JFrame 类来实现
最好加注释 急用 谢谢啦!
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class DrawImage1 extends JPanel{
public void paintComponent(Graphics g){
super.paintComponent(g);
Image ca=Toolkit.getDefaultToolkit().getImage("001.jpg");
g.drawImage(ca,100,80,this);
}
}
class DrawImage2 extends JPanel{
public void paintComponent(Graphics g){
super.paintComponent(g);
Image ca=Toolkit.getDefaultToolkit().getImage("002.jpg");
g.drawImage(ca,15,10,this);
}
}
class DrawImage3 extends JPanel{
public void paintComponent(Graphics g){
super.paintComponent(g);
Image ca=Toolkit.getDefaultToolkit().getImage("003.jpg");
g.drawImage(ca,15,10,this);
}
}
class DrawImage4 extends JPanel{
public void paintComponent(Graphics g){
super.paintComponent(g);
Image ca=Toolkit.getDefaultToolkit().getImage("004.jpg");
g.drawImage(ca,15,10,this);
}
}
class DrawImage5 extends JPanel{
public void paintComponent(Graphics g){
super.paintComponent(g);
Image ca=Toolkit.getDefaultToolkit().getImage("005.jpg");
g.drawImage(ca,15,10,this);
}
}
public class TestImage extends JFrame implements ActionListener{
DrawImage1 image1=new DrawImage1();
DrawImage2 image2=new DrawImage2();
DrawImage3 image3=new DrawImage3();
DrawImage4 image4=new DrawImage4();
DrawImage5 image5=new DrawImage5();
JButton frist,back,next,last;
Container contentPane;
public TestImage(){
super("Image Usage Demo");
frist=new JButton("第一幅");
back=new JButton("上一幅");
next=new JButton("下一幅");
last=new JButton("最后一幅");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane=this.getContentPane();
contentPane.setLayout(new FlowLayout());
contentPane.add(frist);
contentPane.add(back);
contentPane.add(next);
contentPane.add(last);
frist.addActionListener(this);
back.addActionListener(this);
next.addActionListener(this);
last.addActionListener(this);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==frist){
contentPane.add(image1);}
}
public static void main(String[] args){
TestImage app=new TestImage();
app.setSize(600,400);
app.show();
}
}
这是我编的程序 怎么样点击第一幅按钮就显示001图呢?
好像是面板问题 谁帮忙我改下啊 ? 谢谢 展开
要求用JPanel 跟 JFrame 类来实现
最好加注释 急用 谢谢啦!
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class DrawImage1 extends JPanel{
public void paintComponent(Graphics g){
super.paintComponent(g);
Image ca=Toolkit.getDefaultToolkit().getImage("001.jpg");
g.drawImage(ca,100,80,this);
}
}
class DrawImage2 extends JPanel{
public void paintComponent(Graphics g){
super.paintComponent(g);
Image ca=Toolkit.getDefaultToolkit().getImage("002.jpg");
g.drawImage(ca,15,10,this);
}
}
class DrawImage3 extends JPanel{
public void paintComponent(Graphics g){
super.paintComponent(g);
Image ca=Toolkit.getDefaultToolkit().getImage("003.jpg");
g.drawImage(ca,15,10,this);
}
}
class DrawImage4 extends JPanel{
public void paintComponent(Graphics g){
super.paintComponent(g);
Image ca=Toolkit.getDefaultToolkit().getImage("004.jpg");
g.drawImage(ca,15,10,this);
}
}
class DrawImage5 extends JPanel{
public void paintComponent(Graphics g){
super.paintComponent(g);
Image ca=Toolkit.getDefaultToolkit().getImage("005.jpg");
g.drawImage(ca,15,10,this);
}
}
public class TestImage extends JFrame implements ActionListener{
DrawImage1 image1=new DrawImage1();
DrawImage2 image2=new DrawImage2();
DrawImage3 image3=new DrawImage3();
DrawImage4 image4=new DrawImage4();
DrawImage5 image5=new DrawImage5();
JButton frist,back,next,last;
Container contentPane;
public TestImage(){
super("Image Usage Demo");
frist=new JButton("第一幅");
back=new JButton("上一幅");
next=new JButton("下一幅");
last=new JButton("最后一幅");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane=this.getContentPane();
contentPane.setLayout(new FlowLayout());
contentPane.add(frist);
contentPane.add(back);
contentPane.add(next);
contentPane.add(last);
frist.addActionListener(this);
back.addActionListener(this);
next.addActionListener(this);
last.addActionListener(this);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==frist){
contentPane.add(image1);}
}
public static void main(String[] args){
TestImage app=new TestImage();
app.setSize(600,400);
app.show();
}
}
这是我编的程序 怎么样点击第一幅按钮就显示001图呢?
好像是面板问题 谁帮忙我改下啊 ? 谢谢 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询