⑴ 编写一个具有图形用户界面的java程序
import javax.imageio.*;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.io.*;
/**
* @author Hardneedl
*/
class ImageZoom extends JFrame {
private static final Dimension minSize = new Dimension(300, 200);
private static final Dimension maxSize = new Dimension(1024, 768);
private static final Dimension preferredSize = new Dimension(600, 400);
public Dimension getMaximumSize() {return maxSize;}
public Dimension getMinimumSize() {return minSize;}
public Dimension getPreferredSize() {return preferredSize;}
public String getTitle() {return "Frame Title";}
private class ImageCanvas extends JComponent{
private Image img;
private AffineTransform af=new AffineTransform();
private ImageCanvas(Image g,float s) {setImage(g);setScale(s);}
private void setImage(Image img){
this.img = img;
if (isVisible()) paintImmediately(getBounds());
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (img!=null) {
Graphics2D g2d = (Graphics2D)g.create();
g2d.drawImage(img,af,this);
}
}
private void setScale(float scale){
af = AffineTransform.getScaleInstance(scale,scale);
if (isVisible()) paintImmediately(getBounds());
}
};
private static ImageCanvas canvas;
private Image img;private float s;
ImageZoom(Image g,float s) throws HeadlessException {
img =g;this.s=s;
init();
doLay();
attachListeners();
}
private void init() {
canvas = new ImageCanvas(img,s);
}
private void doLay() {
Container container = getContentPane();
container.add(canvas,BorderLayout.CENTER);
pack();
}
private void attachListeners() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(final String[] args) {
SwingUtilities.invokeLater(
new Runnable(){
public void run() {
try {
new ImageZoom(ImageIO.read(new FileInputStream(args[0])),Float.parseFloat(args[1])).setVisible(true);
} catch (IOException e) {
e.printStackTrace();
}
}
}
);
}
}
⑵ java编写一个图形界面程序
importjava.awt.*;
importjava.awt.event.*;
importjava.util.*;
importjavax.swing.*;
importjavax.swing.border.Border;
classMainFrameextendsJFrame{
=1L;
privateMap<String,Integer>sizes=newHashMap<String,Integer>();
privateMap<String,Integer>styles=newHashMap<String,Integer>();
privateMap<String,Integer>toppings=newHashMap<String,Integer>();
publicMainFrame(){
sizes.put("ExtraLarge",10);
sizes.put("Large",8);
sizes.put("Medium",5);
sizes.put("Small",3);
styles.put("DeepDish",20);
styles.put("Regular",10);
styles.put("ThinCrust",5);
styles.put("Chicago",3);
toppings.put("Cheese",8);
toppings.put("Tomato",7);
toppings.put("Peppers",6);
toppings.put("Peperoni",5);
this.setTitle("布局及事件处理");
this.setSize(450,350);
this.setLayout(newBorderLayout());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabellblTitle=newJLabel();
lblTitle.setText("PizzeriaJuno");
lblTitle.setFont(newFont("宋体",Font.BOLD,36));
lblTitle.setHorizontalAlignment(SwingConstants.CENTER);
this.add("North",lblTitle);
JPanelbodyPanel=newJPanel();
bodyPanel.setLayout(newGridLayout(2,1));
this.add("Center",bodyPanel);
JPanellistPanel=newJPanel();
listPanel.setLayout(newGridLayout(1,3));
listPanel.setSize(200,200);
bodyPanel.add(listPanel);
BorderlineBorder=BorderFactory.createLineBorder(Color.BLACK);
JPanelsizePanel=newJPanel();
sizePanel.setLayout(newBorderLayout());
listPanel.add(sizePanel);
JLabelsizeTitle=newJLabel();
sizeTitle.setText("Sizes");
sizePanel.add("North",sizeTitle);
JListsizeList=newJList(sizes.keySet().toArray());
sizeList.setSize(100,100);
sizeList.setBorder(lineBorder);
sizeList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
sizePanel.add(sizeList);
JPanelstylePanel=newJPanel();
stylePanel.setLayout(newBorderLayout());
listPanel.add(stylePanel);
JLabelstyleTitle=newJLabel();
styleTitle.setText("Styles");
stylePanel.add("North",styleTitle);
JListstyleList=newJList(styles.keySet().toArray());
styleList.setSize(100,100);
styleList.setBorder(lineBorder);
styleList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
stylePanel.add(styleList);
JPaneltoppingPanel=newJPanel();
toppingPanel.setLayout(newBorderLayout());
listPanel.add(toppingPanel);
JLabeltoppingTitle=newJLabel();
toppingTitle.setText("Toppings");
toppingPanel.add("North",toppingTitle);
JListtoppingList=newJList(toppings.keySet().toArray());
toppingList.setSize(100,100);
toppingList.setBorder(lineBorder);
toppingPanel.add(toppingList);
JTextAreatxtResult=newJTextArea();
txtResult.setEditable(false);
bodyPanel.add(txtResult);
JPanelbottomPanel=newJPanel();
bottomPanel.setLayout(newGridLayout(1,3));
this.add("South",bottomPanel);
JLabellabel1=newJLabel("Clicktocompleteorder");
bottomPanel.add(label1);
JButtonbtnRingUp=newJButton("Ringup");
btnRingUp.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
if(sizeList.getSelectedValue()==null){
JOptionPane.showMessageDialog(MainFrame.this,"Pleaseselectsize.");
return;
}
if(styleList.getSelectedValue()==null){
JOptionPane.showMessageDialog(MainFrame.this,"Pleaseselectstyle.");
return;
}
if(toppingList.getSelectedValue()==null){
JOptionPane.showMessageDialog(MainFrame.this,"Pleaseselecttopping.");
return;
}
floattotal=0;
Stringsize=sizeList.getSelectedValue().toString();
total+=sizes.get(size);
Stringstyle=styleList.getSelectedValue().toString();
total+=styles.get(style);
Stringresult=size+"Pizza,"+style+"Style";
Object[]toppings=toppingList.getSelectedValues();
for(Objecttopping:toppings){
result+="
+"+topping.toString();
total+=MainFrame.this.toppings.get(topping.toString());
}
result+="
Total:"+total;
txtResult.setText(result);
}
});
bottomPanel.add(btnRingUp);
JButtonbtnQuit=newJButton("Quit");
btnQuit.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
MainFrame.this.dispose();
}
});
bottomPanel.add(btnQuit);
}
}
publicclassApp{
publicstaticvoidmain(String[]args){
MainFramemainFrame=newMainFrame();
mainFrame.setVisible(true);
}
}
⑶ JAVA编程:编写一个图形界面的Application程序
//:SaveContents.java
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class SaveContents extends JFrame implements ActionListener{
public static void main(String[] args) {new SaveContents().setVisible(true);}
private JTextField cont;
private JButton save;
private String filePath="c:/cont.txt";//文件的路径
public SaveContents(){
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().setLayout(null);
this.setSize(205,130);
this.setLocationRelativeTo(null);
cont = new JTextField();
cont.setBounds(10,10,180,20);
add(cont);
save=new JButton("Save");
save.setBounds(110,50,78,22);
add(save);
save.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
try {save();} catch (Exception e1) {}
}
//保存内容
private void save() throws Exception{
File f = new File(filePath);
FileWriter fw = new FileWriter(f);
fw.write(cont.getText());
fw.flush();
fw.close();
}
}