java 事件监听
importjava.awt.*;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener...
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.util.*;
public class Demo1 extends JFrame implements ActionListener{
int i;
public static void main(String[] args) {
// TODO 自动生成方法存根
Demo1 demo1=new Demo1();
}
public Demo1()
{
this.i=i;
JLabel jl1=new JLabel(new ImageIcon("images/2.jpg"));
JLabel jl2=new JLabel(new ImageIcon("images/3.jpg"));
ArrayList al=new ArrayList(2);
al.add(jl1);
al.add(jl2);
System.out.println(i);
JLabel jL= (JLabel)al.get(i);
this.add(jL,BorderLayout.CENTER);
JButton jb1=new JButton("上一张");
JButton jb2=new JButton("下一张");
JPanel jp=new JPanel();
this.add(jp,BorderLayout.SOUTH);
jp.add(jb1,BorderLayout.EAST);
jp.add(jb2,BorderLayout.WEST);
jb1.addActionListener(this);
jb1.setActionCommand("上一张");
jb2.addActionListener(this);
jb2.setActionCommand("下一张");
this.setSize(560, 800);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void actionPerformed(ActionEvent arg0) {
// TODO 自动生成方法存根
if(arg0.getActionCommand().equals("下一张"))
{
i++;
}
}
}
要添加怎么的代码,才能实现通过点击Button按钮可以切换图片 展开
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.util.*;
public class Demo1 extends JFrame implements ActionListener{
int i;
public static void main(String[] args) {
// TODO 自动生成方法存根
Demo1 demo1=new Demo1();
}
public Demo1()
{
this.i=i;
JLabel jl1=new JLabel(new ImageIcon("images/2.jpg"));
JLabel jl2=new JLabel(new ImageIcon("images/3.jpg"));
ArrayList al=new ArrayList(2);
al.add(jl1);
al.add(jl2);
System.out.println(i);
JLabel jL= (JLabel)al.get(i);
this.add(jL,BorderLayout.CENTER);
JButton jb1=new JButton("上一张");
JButton jb2=new JButton("下一张");
JPanel jp=new JPanel();
this.add(jp,BorderLayout.SOUTH);
jp.add(jb1,BorderLayout.EAST);
jp.add(jb2,BorderLayout.WEST);
jb1.addActionListener(this);
jb1.setActionCommand("上一张");
jb2.addActionListener(this);
jb2.setActionCommand("下一张");
this.setSize(560, 800);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void actionPerformed(ActionEvent arg0) {
// TODO 自动生成方法存根
if(arg0.getActionCommand().equals("下一张"))
{
i++;
}
}
}
要添加怎么的代码,才能实现通过点击Button按钮可以切换图片 展开
3个回答
展开全部
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.util.*;
public class Demo1 extends JFrame implements ActionListener {
int i=0;
ArrayList al = new ArrayList(2);
JPanel img=new JPanel();
public static void main(String[] args) {
// TODO 自动生成方法存根
Demo1 demo1 = new Demo1();
}
public Demo1() {
JLabel jl1=new JLabel(new ImageIcon("images/2.jpg"));
JLabel jl2=new JLabel(new ImageIcon("images/3.jpg"));
al.add(jl1);
al.add(jl2);
System.out.println(i);
JLabel jL = (JLabel) al.get(i);
img.add(jL, BorderLayout.CENTER);
this.add(img, BorderLayout.CENTER);
JButton jb1 = new JButton("上一张");
JButton jb2 = new JButton("下一张");
JPanel jp = new JPanel();
this.add(jp, BorderLayout.SOUTH);
jp.add(jb1, BorderLayout.EAST);
jp.add(jb2, BorderLayout.WEST);
jb1.addActionListener(this);
jb1.setActionCommand("上一张");
jb2.addActionListener(this);
jb2.setActionCommand("下一张");
this.setSize(560, 600);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void actionPerformed(ActionEvent arg0) {
// TODO 自动生成方法存根
if (arg0.getActionCommand().equals("下一张")) {
i++;
if(i>=al.size()){//翻到最后一张了,重新从第一张开始
i=0;
}
img.removeAll();
img.add((JLabel)al.get(i), BorderLayout.CENTER);
this.validate();//刷新组件
this.repaint();
}
if (arg0.getActionCommand().equals("上一张")) {
i--;
if(i<0){
i=al.size()-1;
}
img.removeAll();
img.add((JLabel)al.get(i), BorderLayout.CENTER);
this.validate();
this.repaint();
}
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.util.*;
public class Demo1 extends JFrame implements ActionListener {
int i=0;
ArrayList al = new ArrayList(2);
JPanel img=new JPanel();
public static void main(String[] args) {
// TODO 自动生成方法存根
Demo1 demo1 = new Demo1();
}
public Demo1() {
JLabel jl1=new JLabel(new ImageIcon("images/2.jpg"));
JLabel jl2=new JLabel(new ImageIcon("images/3.jpg"));
al.add(jl1);
al.add(jl2);
System.out.println(i);
JLabel jL = (JLabel) al.get(i);
img.add(jL, BorderLayout.CENTER);
this.add(img, BorderLayout.CENTER);
JButton jb1 = new JButton("上一张");
JButton jb2 = new JButton("下一张");
JPanel jp = new JPanel();
this.add(jp, BorderLayout.SOUTH);
jp.add(jb1, BorderLayout.EAST);
jp.add(jb2, BorderLayout.WEST);
jb1.addActionListener(this);
jb1.setActionCommand("上一张");
jb2.addActionListener(this);
jb2.setActionCommand("下一张");
this.setSize(560, 600);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void actionPerformed(ActionEvent arg0) {
// TODO 自动生成方法存根
if (arg0.getActionCommand().equals("下一张")) {
i++;
if(i>=al.size()){//翻到最后一张了,重新从第一张开始
i=0;
}
img.removeAll();
img.add((JLabel)al.get(i), BorderLayout.CENTER);
this.validate();//刷新组件
this.repaint();
}
if (arg0.getActionCommand().equals("上一张")) {
i--;
if(i<0){
i=al.size()-1;
}
img.removeAll();
img.add((JLabel)al.get(i), BorderLayout.CENTER);
this.validate();
this.repaint();
}
}
}
展开全部
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.util.*;
public class Demo1 extends JFrame implements ActionListener {
int i=0;
ArrayList al = new ArrayList(2);
JPanel img=new JPanel();
public static void main(String[] args) {
// TODO 自动生成方法存根
Demo1 demo1 = new Demo1();
}
public Demo1() {
JLabel jl1=new JLabel(new ImageIcon("images/2.jpg"));
JLabel jl2=new JLabel(new ImageIcon("images/3.jpg"));
al.add(jl1);
al.add(jl2);
System.out.println(i);
JLabel jL = (JLabel) al.get(i);
img.add(jL, BorderLayout.CENTER);
this.add(img, BorderLayout.CENTER);
JButton jb1 = new JButton("上一张");
JButton jb2 = new JButton("下一张");
JPanel jp = new JPanel();
this.add(jp, BorderLayout.SOUTH);
jp.add(jb1, BorderLayout.EAST);
jp.add(jb2, BorderLayout.WEST);
jb1.addActionListener(this);
jb2.addActionListener(this);
this.setSize(560, 600);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
// TODO 自动生成方法存根
if (e.getRource==jb1) {
i++;
if(i>=al.size()){//翻到最后一张了,重新从第一张开始
i=0;
}
img.removeAll();
img.add((JLabel)al.get(i), BorderLayout.CENTER);
this.validate();//刷新组件
this.repaint();
}
if ((e.getRource==jb2) {
i--;
if(i<0){
i=al.size()-1;
}
img.removeAll();
img.add((JLabel)al.get(i), BorderLayout.CENTER);
this.validate();
this.repaint();
}
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.util.*;
public class Demo1 extends JFrame implements ActionListener {
int i=0;
ArrayList al = new ArrayList(2);
JPanel img=new JPanel();
public static void main(String[] args) {
// TODO 自动生成方法存根
Demo1 demo1 = new Demo1();
}
public Demo1() {
JLabel jl1=new JLabel(new ImageIcon("images/2.jpg"));
JLabel jl2=new JLabel(new ImageIcon("images/3.jpg"));
al.add(jl1);
al.add(jl2);
System.out.println(i);
JLabel jL = (JLabel) al.get(i);
img.add(jL, BorderLayout.CENTER);
this.add(img, BorderLayout.CENTER);
JButton jb1 = new JButton("上一张");
JButton jb2 = new JButton("下一张");
JPanel jp = new JPanel();
this.add(jp, BorderLayout.SOUTH);
jp.add(jb1, BorderLayout.EAST);
jp.add(jb2, BorderLayout.WEST);
jb1.addActionListener(this);
jb2.addActionListener(this);
this.setSize(560, 600);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
// TODO 自动生成方法存根
if (e.getRource==jb1) {
i++;
if(i>=al.size()){//翻到最后一张了,重新从第一张开始
i=0;
}
img.removeAll();
img.add((JLabel)al.get(i), BorderLayout.CENTER);
this.validate();//刷新组件
this.repaint();
}
if ((e.getRource==jb2) {
i--;
if(i<0){
i=al.size()-1;
}
img.removeAll();
img.add((JLabel)al.get(i), BorderLayout.CENTER);
this.validate();
this.repaint();
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
jl2.seticon('');
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询