导航:首页 > 编程语言 > java图形界面代码

java图形界面代码

发布时间:2024-08-12 22:11:50

⑴ 编写一个具有图形用户界面的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();
}
}

阅读全文

与java图形界面代码相关的资料

热点内容
上滑命令 浏览:901
linux红帽服务器版 浏览:65
检查jdk版本命令 浏览:962
如何加入叶枫服务器 浏览:863
android虚拟机权限 浏览:73
趋势平均力度源码 浏览:280
做程序员好难 浏览:240
晚年程序员的生活 浏览:411
安卓什么型号可以用两年不卡 浏览:188
安卓怎么一边玩游戏一边打电话 浏览:282
体育综合分的算法 浏览:599
用友客户端连服务器P地址 浏览:525
程序员小工具有哪些 浏览:850
android难用 浏览:253
2021金砖论坛数据算法盛宴 浏览:744
职校学计算机出来可以当程序员吗 浏览:478
androidxml命名 浏览:85
批命令if 浏览:101
手机桌面出现安卓图标怎么办 浏览:965
php网站生成app 浏览:731