在JAVA中,实现一个简单的图像浏览器,部署“上一张”、“下一张”两个按钮,点击按钮可前后翻阅图片。
答案:4 悬赏:30
解决时间 2021-11-08 04:40
- 提问者网友:残阳碧曼
- 2021-11-07 07:58
在本机查看图片,希望有详细代码,谢谢..
最佳答案
- 二级知识专家网友:猎杀温柔
- 2021-11-07 09:03
我这有一个!图片你自己找!但必须改成1.jpg,2.jpg........你也可以用卡片布局!比较不错!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class bmt1 implements ActionListener
{JFrame f;
JPanel p1;
JPanel p2;
JButton b1;
JButton b2;
JButton b3;
JButton b4;
JLabel l1;
ImageIcon icon1;
ImageIcon icon2;
ImageIcon icon3;
ImageIcon icon4;
Boolean ture;
int n=0;
public bmt1()
{f=new JFrame("版面布局");
f.setSize(400,500);
f.setVisible(true);
Container con=f.getContentPane();
con.setLayout(new GridLayout(2,1));
p1=new JPanel();
p2=new JPanel();
con.add(p1);
con.add(p2);
b1=new JButton("第一张");
b2=new JButton("下一张");
b3=new JButton("前一张");
b4=new JButton("最后一张");
p2.add(b1);
p2.add(b2);
p2.add(b3);
p2.add(b4);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
p2.setLayout(new FlowLayout());
l1=new JLabel();
p1.add(l1);
icon1=new ImageIcon("1.jpg");
icon2=new ImageIcon("2.jpg");
icon3=new ImageIcon("3.jpg");
icon4=new ImageIcon("4.jpg");
f.addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{System.exit(0);}
});
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==b1)
{ n=1;
l1.setIcon(new ImageIcon("1.jpg"));}
if(e.getSource()==b2)
{ n=n+1;
if(n>4){n=1;}
String s=String.valueOf(n);
l1.setIcon(new ImageIcon(s+".jpg"));}
if(e.getSource()==b3)
{ if(n<1){n=4;}
n=n-1;
String s=String.valueOf(n);
l1.setIcon(new ImageIcon(s+".jpg"));}
if(e.getSource()==b4)
{n=4;
l1.setIcon(new ImageIcon("4.jpg"));}
}
public static void main(String args[])
{new bmt1();}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class bmt1 implements ActionListener
{JFrame f;
JPanel p1;
JPanel p2;
JButton b1;
JButton b2;
JButton b3;
JButton b4;
JLabel l1;
ImageIcon icon1;
ImageIcon icon2;
ImageIcon icon3;
ImageIcon icon4;
Boolean ture;
int n=0;
public bmt1()
{f=new JFrame("版面布局");
f.setSize(400,500);
f.setVisible(true);
Container con=f.getContentPane();
con.setLayout(new GridLayout(2,1));
p1=new JPanel();
p2=new JPanel();
con.add(p1);
con.add(p2);
b1=new JButton("第一张");
b2=new JButton("下一张");
b3=new JButton("前一张");
b4=new JButton("最后一张");
p2.add(b1);
p2.add(b2);
p2.add(b3);
p2.add(b4);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
p2.setLayout(new FlowLayout());
l1=new JLabel();
p1.add(l1);
icon1=new ImageIcon("1.jpg");
icon2=new ImageIcon("2.jpg");
icon3=new ImageIcon("3.jpg");
icon4=new ImageIcon("4.jpg");
f.addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{System.exit(0);}
});
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==b1)
{ n=1;
l1.setIcon(new ImageIcon("1.jpg"));}
if(e.getSource()==b2)
{ n=n+1;
if(n>4){n=1;}
String s=String.valueOf(n);
l1.setIcon(new ImageIcon(s+".jpg"));}
if(e.getSource()==b3)
{ if(n<1){n=4;}
n=n-1;
String s=String.valueOf(n);
l1.setIcon(new ImageIcon(s+".jpg"));}
if(e.getSource()==b4)
{n=4;
l1.setIcon(new ImageIcon("4.jpg"));}
}
public static void main(String args[])
{new bmt1();}
}
全部回答
- 1楼网友:湫止没有不同
- 2021-11-07 11:14
可以尝试使用分页来实现。。。
- 2楼网友:ー何必说爱
- 2021-11-07 10:31
前几天刚做过 代码我没放在身上 具体的流程 可以跟你说一下 我是用你个继承jpanl的类做画图面版的 把面版放在继承JFRAM这个类的容器里 然后把所有的图片的路径组成一个数组 然后根据按纽的事件来改变面板的Image
- 3楼网友:摧毁过往
- 2021-11-07 09:32
我们老师教我们需要写俩个类分别控制 上和下 用页面使用 ,其中加入SQL语句在数据库中查询出页数
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯