您的位置:首页 > 移动开发

java中一个简单的Applet(图片浏览…

2016-09-23 20:07 211 查看
第一个程序是简单的图片浏览器,
第二个是页面添加按钮,控制控制面板的颜色变化。
运行界面:

运行结果界面:





package applet1;

import java.awt.*;
import java.awt.event.*;
import java.io.*;

import javax.swing.*;

public class ImageViewer {

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable(){
@Override
public void run() {
JFrame frame=new ImageFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setTitle("ImageViewer");
}
});
}
}
class ImageFrame extends JFrame{
private JLabel label;
private JFileChooser chooser;
private static final int DEFAULT_WIDTH=400;
private static final int DEFAULT_HEIGHT=300;
public ImageFrame(){
setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
//use a label to display the images
label=new JLabel();
add(label);
//set up the file chooser
chooser=new JFileChooser();
chooser.setCurrentDirectory(new File("."));
//set up the menu bar
JMenuBar menuBar=new JMenuBar();
setJMenuBar(menuBar);
JMenu menu=new JMenu("File");
menuBar.add(menu);
JMenuItem openItem=new JMenuItem("Open");
menu.add(openItem);
openItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
//show file chooser dialog
int result=chooser.showOpenDialog(null);
//if file selected,set it as icon of the label
if(result==JFileChooser.APPROVE_OPTION){
String name=chooser.getSelectedFile().getPath();
label.setIcon(new ImageIcon(name));
}
}
});
JMenuItem exitItem=new JMenuItem("Exit");
menu.add(exitItem);
exitItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
System.exit(0);
}
});
}
}
第二个:颜色控制器:




源代码:package buttonFrame;

import java.awt.Color;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class ButtonFrame{
public static void main(String[] args){
EventQueue.invokeLater(new Runnable(){
@Override
public void run() {
JFrame frame=new ButtonsFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setTitle("Color");
}
});
}
}
//A frame with a button panel
class ButtonsFrame extends JFrame{
private JPanel buttonPanel;
private static final int DEFAULT_WIDTH=300;
private static final int DEFAULT_HEIGHT=200;
public ButtonsFrame(){
setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
//creat buttons 
JButton yellowButton=new JButton("Yellow");
JButton blueButton=new JButton("Blue");
JButton reon=new JButton("red");
buttonPanel=new JPanel();
//add buttons to Panel
buttonPanel.add(yellowButton);
buttonPanel.add(blueButton);
buttonPanel.add(reon);
//add panel to frame
add(buttonPanel);
//creat button actions
ColorAction yellowAction=new ColorAction(Color.YELLOW);
ColorAction blueAction=new ColorAction(Color.BLUE);
ColorAction redAction=new ColorAction(Color.RED);
//associate actions with buttons
yellowButton.addActionListener(yellowAction);
blueButton.addActionListener(blueAction);
reon.addActionListener(redAction);
}
private class ColorAction implements ActionListener{
private Color backgroundColor;
public ColorAction(Color c){
backgroundColor=c;
}
public void actionPerformed(ActionEvent event){
buttonPanel.setBackground(backgroundColor);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: