这个程序的特点如下:
在JFrame中添加了菜单栏;
在画布中添加了诸多元素;
最后会将图片通过图片缓存保存到本地“frame.png”中。
代码如下:
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
public class FrameWork extends JFrame {
private static final double pi = Math.acos(-1.0);
private static final int Width = 1000;
private static final int Height = 600;
private static JFrame frame = null;
private static int personNumber = 15;
private static String[] names = new String[100];
public FrameWork() {
frame = new JFrame("Java菜单栏");
//JList list = new JList();
//frame.add(list);
JMenuBar menuBar = new JMenuBar();
frame.setJMenuBar(menuBar);
JMenu fileMenu = new JMenu("文件");
JMenu openMenu = new JMenu("打开");
JMenuItem openItem = new JMenuItem("文件");
openMenu.add(openItem);
openItem.addActionListener(new MyAction());
fileMenu.add(openMenu);
menuBar.add(fileMenu);
frame.setLocationRelativeTo(null);
frame.setSize(Width, Height);
frame.setLocation(100, 100);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//JPanel panel = new ImagePanel();
//panel.setBounds(0, 0, Width, Height);
//frame.getContentPane().add(panel);
frame.setVisible(true);
}
private static int getNumber1(int i) {
int n = personNumber;
if((n-1)/2*2-2*i >= 0) return (n-1)/2*2 - 2*i;
return (i-(n-1)/2)*2-1;
}
private static int getNumber2(int i) {
if(i*2+1 <= personNumber) return i*2;
return 2*(personNumber-i)-1;
}
private class MyAction implements ActionListener {
public void actionPerformed(ActionEvent evt) {
//Object s = evt.getSource();
JFileChooser jfc=new JFileChooser();
jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );
jfc.showDialog(new JLabel(), "选择");
File file=jfc.getSelectedFile();
/*if(file.isDirectory()){
System.out.println("文件夹:"+file.getAbsolutePath());
}else if(file.isFile()){
System.out.println("文件:"+file.getAbsolutePath());
}*/
personNumber = 0;
BufferedReader reader;
try {
reader = new BufferedReader(new FileReader(file.getAbsolutePath()));
while((names[personNumber]=reader.readLine()) != null) {
personNumber ++;
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
JPanel panel = new ImagePanel();
panel.setBounds(0, 0, Width, Height);
frame.getContentPane().add(panel);
frame.setVisible(true);
//System.out.println(personNumber);
//for(int i=0;i<personNumber;i++)
//System.out.println(names[i]);
BufferedImage bi = new BufferedImage(frame.getWidth(), frame.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = bi.createGraphics();
frame.paint(g2d);
try {
ImageIO.write(bi, "PNG", new File("D:\\frame.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
}
class ImagePanel extends JPanel {
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.white);
g.fillRect(0, 0, Width, Height);
g.setColor(Color.black);
int delx = (Width - 20) / (personNumber + 1);
for(int i=0;i<personNumber;i++) {
int x = i * delx + 10;
g.drawRect(x, 10, delx, 20);
//String s = "" + getNumber1(i);
int id = getNumber1(i);
String s = names[id];
g.drawString(s, x+2, 22);
//s = "" + getNumber2(i);
id = getNumber2(i);
s = names[id];
int x0 = 440, y0 = 285, r = 170;
int xx = (int) (x0 - r * Math.sin(pi*2*i/personNumber));
int yy = (int) (y0 - r * Math.cos(pi*2*i/personNumber));
g.drawString(s, xx, yy);
}
g.drawOval(300, 130, 300, 300);
}
}
public static void main(String[] args) {
new FrameWork();
}
}