導航:首頁 > 編程語言 > 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圖形界面代碼相關的資料

熱點內容
pythonunittestapi 瀏覽:332
ug轉圖的編譯器位置 瀏覽:765
程序員兩萬的台式機 瀏覽:494
手指速演算法38怎麼算 瀏覽:518
程序員的英語單詞 瀏覽:904
做單片機開發的可以做到多少歲 瀏覽:84
可以做pdf 瀏覽:855
解壓是什麼意思怎麼解壓 瀏覽:420
衛星電視加密有用嗎 瀏覽:534
什麼app新用戶有優惠券 瀏覽:762
idea編譯方法 瀏覽:725
單片機繪制光滑曲線 瀏覽:852
python協程快還是多線程快 瀏覽:110
android文字自動滾動 瀏覽:391
ruby獲取伺服器地址 瀏覽:977
安卓適配器中如何調用其他函數 瀏覽:441
重慶lol的伺服器雲主機 瀏覽:993
javaajax跨域 瀏覽:14
數控加工基礎編程尺寸 瀏覽:816
ssu命令是什麼意思 瀏覽:882