導航:首頁 > 編程語言 > java圖形編程實例

java圖形編程實例

發布時間:2022-09-24 16:10:01

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編程題:設計三個圖形類

真不知道這是考寫代碼還是考數學。
給你一個思路吧,定義一個抽象類表示圖形,有顏色屬性、粗細屬性、求面積方法、比較大小的方法。
然後在定義一個類表示三角形,繼承這個抽象類,三角形類有三個屬性,分別表示它的三個頂點坐標。
也定義一個類表示矩形,繼承抽象類,它有兩個屬性,分別表示它左上角和右下角的坐標。
再定義一個類表示圓形,它有兩個屬性,分別表示圓心和圓上任一點的坐標。

㈢ 求java圖形界面樹類編程源碼舉例。類似windows資源管理器那樣的。如附圖,2層2項即可。

publicvoidinit(){
ContainercontentPane=null;
=newDefaultMutableTreeNode("我的電腦");
1=newDefaultMutableTreeNode("網路");
2=newDefaultMutableTreeNode("硬碟");
1_1=newDefaultMutableTreeNode("無限");
1_2=newDefaultMutableTreeNode("有限");
2_1=newDefaultMutableTreeNode("A");
2_2=newDefaultMutableTreeNode("B");
treeNode.add(treeNode1);
treeNode.add(treeNode2);
treeNode1.add(treeNode1_1);
treeNode1.add(treeNode1_2);
treeNode2.add(treeNode2_1);
treeNode2.add(treeNode2_2);
JTreetree=newJTree(treeNode);
contentPane=getContentPane();
JPaneljp=newJPanel();
jp.add(tree);
contentPane.add(jp);
this.setSize(300,250);
this.setLocation(400,300);
this.setVisible(true);
}

㈣ 用JAVA編寫個圖形

public class draw_Pic{
public static void main(String args[]){
System.out.println("*");
System.out.println("**");
System.out.println("***");
System.out.println("****");
System.out.println("*****");
}
}

㈤ JAVA圖形界面編程JBUtton

button1.addActionListener(new ActionListener() {//為button1綁定事件監聽器
public void actionPerformed(ActionEvent e) {//重寫actionPerformed方法,方法內的代碼為點擊後 //執行的代碼
JFrame frame = new JFrame(); //實例化一個新的窗口
frame.setVisible(true);//設置為可見
frame.setBounds(200,200,200,200); //設置窗口的大小和坐標

}
});

㈥ java圖形界面編程想要加背景圖,就是處於所有圖層最下面的,有沒有什麼示例代碼

我之前寫過一個這樣的,可以給你參考

其中背景圖是alarmImage

JLayeredPane alarmLayeredPane=new JLayeredPane();
JPanel jPanel=new JPanel(); //新建面板容器
JLabel jLabel=new JLabel(); //新建標簽
jPanel.setBounds(0, -5, alarmImage.getIconWidth(), alarmImage.getIconHeight());//設置面板大小
jLabel=new JLabel(alarmImage); //創建具有指定圖像的JLabel實例
jPanel.add(jLabel); //將標簽加到面板容器中去
alarmLayeredPane.add(jPanel, alarmLayeredPane.DEFAULT_LAYER); //再將面板容器放到層次面板的底層,即背景圖

㈦ 用Java編寫一個繪制圖形的小程序

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;

//不規則圖形的繪制

public class IrregularShapeDemo extends JFrame {

GeneralPath gPath= new GeneralPath(); //GeneralPath對象實例
Point aPoint;

//構造函數
public IrregularShapeDemo() {
super("不規則圖形的繪制"); //調用父類構造函數
enableEvents(AWTEvent.MOUSE_EVENT_MASK|AWTEvent.MOUSE_MOTION_EVENT_MASK); //允許事件

setSize(300, 200); //設置窗口尺寸
setVisible(true); //設置窗口可視
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //關閉窗口時退出程序
}

public void paint(Graphics g) { //重載窗口組件的paint()方法
Graphics2D g2D = (Graphics2D)g; //獲取圖形環境
g2D.draw(gPath); //繪制路徑
}

public static void main(String[] args) {
new IrregularShapeDemo();
}

protected void processMouseEvent(MouseEvent e) { //滑鼠事件處理
if(e.getID() == MouseEvent.MOUSE_PRESSED) {
aPoint = e.getPoint(); //得到當前滑鼠點
gPath = new GeneralPath(); //重新實例化GeneralPath對象
gPath.moveTo(aPoint.x,aPoint.y); //設置路徑點
}
}

protected void processMouseMotionEvent(MouseEvent e) { //滑鼠運動事件處理
if(e.getID() == MouseEvent.MOUSE_DRAGGED) {
aPoint = e.getPoint(); //得到當前滑鼠點
gPath.lineTo(aPoint.x, aPoint.y); //設置路徑
gPath.moveTo(aPoint.x, aPoint.y);
repaint(); //重繪組件
}
}
}

㈧ java圖形編程

/////////////////////////

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class SimpleGUI {

JFrame f = new JFrame("簡單的GUI");
JButton b = new JButton("點擊我");
static boolean valid = true;

public SimpleGUI() {
f.setBounds(400, 300, 100, 100);
f.setLayout(new BorderLayout());
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
if(valid) {
b.setBackground(Color.RED);
} else {
b.setBackground(Color.BLUE);
}
valid = !valid;
}
});
f.add(b, BorderLayout.CENTER);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}

public static void main(String [] args) {
new SimpleGUI();

}
}

㈨ 用JAVA寫一個簡單圖形類

public class Test013 {

/**
* 編寫一個圖形類MyGraphic。 1)它有兩個基本屬性:圖形線條的顏色String lineColor和圖形的填充顏色String
* fillColor。 2)設計矩形類CRectangle,有屬性double rLong和寬double rWidth, 使用方法 float
* calCircum()可以返回矩形的周長,使用方法float calSquare()可以返回矩形的面積。
* 編寫方法show(),顯示圖形的線條顏色和填充顏色,輸出面積和方法。 3)設計圓形類CCircle,定義屬性:半徑double
* radius,可以通過同名方法計算周長和面積。 編寫方法show(),顯示圖形的線條顏色和填充顏色,輸出面積和方法。
* 4)編寫出應用程序對CRectangle類和CCircle類進行驗證。 完成上述要求即可
*/
public static void main(String[] args) {
MyGraphic rectangle = new CRectangle(10, 5);
rectangle.setFillColor("紫色"); //設定矩形填充顏色
rectangle.setLineColor("白色"); //設定矩形線條顏色
rectangle.show();
System.out.println("矩形周長 = " + rectangle.calCircum());
System.out.println("矩形面積 = " + rectangle.calSquare());

MyGraphic circle = new CCircle(3);
circle.setFillColor("紅色");
circle.setLineColor("黃色");
circle.show();
System.out.println("園形周長 = " + circle.calCircum());
System.out.println("園形面積 = " + circle.calSquare());
}

}

/**
* 圖形類
*
*/
abstract class MyGraphic {
private String lineColor; // 圖形線條的顏色
private String fillColor; // 圖形的填充顏色

public String getLineColor() {
return lineColor;
}

public void setLineColor(String lineColor) {
this.lineColor = lineColor;
}

public String getFillColor() {
return fillColor;
}

public void setFillColor(String fillColor) {
this.fillColor = fillColor;
}

public MyGraphic(String lineColor, String fillColor) {
this.lineColor = lineColor;
this.fillColor = fillColor;
}

public MyGraphic() {
}

/**
* 顯示圖形的顏色
*/
public abstract void show();

/**
* 計算圖形的周長
*/
public abstract float calCircum();

/**
* 計算圖形的面積
*/
public abstract float calSquare();
}

/**
* 矩形類
*
*/
class CRectangle extends MyGraphic {

private double rLong; // 長
private double rWidth; // 寬

/**
* 通過構造函數為圖形的屬性賦值
*
* @param rLong
* @param rWidth
*/
public CRectangle(double rLong, double rWidth) {
this.rLong = rLong;
this.rWidth = rWidth;
}

/**
* @return 矩形的周長
*/
@Override
public float calCircum() {
return (float) (2 * (rLong + rWidth));
}

/**
* @return 矩形的面積
*/
@Override
public float calSquare() {
return (float) (rLong * rWidth);
}

@Override
public void show() {
System.out.println("矩形線條的顏色: " + super.getLineColor());
System.out.println("矩形填充顏色: " + super.getFillColor());
}

public double getrLong() {
return rLong;
}

public void setrLong(double rLong) {
this.rLong = rLong;
}

public double getrWidth() {
return rWidth;
}

public void setrWidth(double rWidth) {
this.rWidth = rWidth;
}
}

/**
* 圓形類
*
*/
class CCircle extends MyGraphic {
private double radius; // 圓形半徑

public CCircle(double radius) {
this.radius = radius;
}

/**
* @return 圓形的周長
*/
@Override
public float calCircum() {
return (float) (2 * Math.PI * radius);
}

/**
* @return 圓形的面積
*/
@Override
public float calSquare() {
return (float) (Math.PI * radius * radius);
}

@Override
public void show() {
System.out.println("圓形線條的顏色: " + super.getLineColor());
System.out.println("圓形填充顏色: " + super.getFillColor());
}

public double getRadius() {
return radius;
}

public void setRadius(double radius) {
this.radius = radius;
}
}

㈩ 使用Java的GUI圖形用戶界面編程設計並編寫一個計算器程序

進口的java.awt.BorderLayout;
進口java.awt.GridLayout中;

進口java.awt.event.ActionEvent;

進口java.awt.event.ActionListener;

進口的javax。 swing.JButton;

進口javax.swing.JFrame中;

進口javax.swing.JPanel;

進口javax.swing.JTextField中;

/ /暫時不考慮這個問題甚至添加

/ /點的第二個操作點的操作員點運算符點=結果

公共類計算器實現的ActionListener {
> JTextField的JTF =新的JTextField(10);

私人布爾追加= FALSE;

私人字元串OP1 =「0」;

私人字元串運算符=「+」;

@覆蓋

公共無效的actionPerformed(ActionEvent的E){

字元串COMN = e.getActionCommand();。!

/ /處理數字

如果(「0123456789」的indexOf(COMN)= -1){

如果(追加){/ /追加

字元串溫度= jtf.getText() ;

jtf.setText(溫度+ COMN);

}其他{/ /替換

jtf.setText(COMN);

追加= TRUE;

/ /處理運營商

否則,如果(「+ - * /」的indexOf(COMN)= -1){

OP1 = JTF。的getText();

運算符= COMN;

追加= FALSE;

的} else if(「=」的indexOf(COMN)= -1。){

字元串OP2 = jtf.getText();

雙D1 = Double.parseDouble(OP1);

雙D2 = Double.parseDouble(OP2);

如果(「+」等於(操作者)。){

D1 = D1 + D2;

}否則,如果(「 - 」等於(操作員)。){

D1 = D1 - D2;

}否則,如果(「*」的equals(操作員)。){

D1 = D1 * D2;

的} else if(「/」等於(操作者)。){

D1 = D1 / D2;

jtf.setText(D1 +「」);

追加= FALSE;

}否則,如果(等於(COMN)「。」){

字元串溫度= jtf.getText();

如果(temp.indexOf(「;」)== -1){

jtf.setText(溫度+「。」);

追加= TRUE;

否則,如果(「+ / - 」的equals(COMN)){

字元串溫度= jtf.getText();

如果(temp.startsWith(「-1」)){

JTF。的setText(temp.substring(1));

}其他{

jtf.setText(「 - 」+溫度);

否則,如果(「退格」。等於(COMN)){

字元串溫度= jtf.getText();

如果(temp.length()> 0){

jtf.setText(temp.substring(0,temp.length() - 1)); ..

}

的} else if(「CE」等於(COMN)| |「C」等於(COMN)){

jtf.setText(「0」);

追加= FALSE;

公眾計算器(){

JFrame的JF =新的JFrame(「計算器」);

jf.add(JTF,BorderLayout.NORTH);

字串[] S1 = {「退格」,「CE」,「C」,「+」,「7」,「8」,「9」,「/」,「4」,

「5」,「6」,「*」,「1」,「2」,「3」,「 - 」,「0」,「+ / - 」,「」,。 「=」};

的JPanel JP =新的JPanel();

jf.add(JP,使用BorderLayout.CENTER);

網格布局GL =新的網格布局(5,4); jp.setLayout(GL);

JButton的[] JB =新的JButton [s1.length];

為(int i = 0;我<s1.length,我+ +){

JB [我] =新的JButton(S1由[i]);

jp.add(JB由[i]);。

JB [I] addActionListener方法,(這);

} jf.add(JP);

jtf.setEditable(假);

jf.setLocation(400,300);

jf.pack();

jf.setResizable(假);/ /設置窗口不可改變

jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);

jf.setVisible(真);

公共靜態無效的主要(字串[] args){

新的計算器();
這個函數比較簡單,不知道能不能滿足要求

閱讀全文

與java圖形編程實例相關的資料

熱點內容
編譯器怎麼調用構造函數的 瀏覽:95
高質量cpdf 瀏覽:821
福建電信代理伺服器雲主機 瀏覽:616
美圖看看pdf 瀏覽:432
編譯後報錯 瀏覽:291
網路設備怎麼加密 瀏覽:785
hbuilderx文件夾有哪些 瀏覽:102
空調壓縮機生產板塊 瀏覽:612
開源多媒體伺服器都有什麼 瀏覽:392
反編譯了別人的app會被發現嗎 瀏覽:918
上海光裕汽車壓縮機有限公司 瀏覽:333
連接ps4伺服器地址 瀏覽:136
新神魔大陸三星賬號是什麼伺服器 瀏覽:677
壓縮機lj100cy 瀏覽:556
王者系統怎麼轉回安卓系統 瀏覽:749
linux查看路由表命令 瀏覽:506
高手程序員使用什麼筆記本 瀏覽:440
ios壓縮圖片app 瀏覽:839
排隊論pdf 瀏覽:520
python調用無參函數 瀏覽:799