本文目錄一覽:
用java想做一個畫板 在點擊菜單中筆的選項後,應該得到白色背景和藍色的筆,可我現在得不到圖像。
題主的想法是對的的,但寫法不對,
你想獲得Graphics對象的話,要重寫容器的paint()方法,在paint方法里獲得graphics對象
急求如何用JAVA寫一個簡單的計算器 或者 畫板
import java.awt.*;
import java.awt.event.*;
public class jisuanqi extends WindowAdapter {
Panel p1 = new Panel();
Panel p2 = new Panel();
Panel p3 = new Panel();
TextField txt;
private Button[] b = new Button[17];
private String ss[] = { “7”, “8”, “9”, “+”, “4”, “5”, “6”, “-“, “1”, “2”,
“3”, “*”, “清空”, “0”, “=”, “/”, “關閉” };
static double a;
static String s, str;//定義變量 創建對像
public static void main(String args[]) {
(new jisuanqi()).frame();
}
public void frame() {
Frame fm = new Frame(“簡單計算器”);
for (int i = 0; i = 16; i++) {
b[i] = new Button(ss[i]);
}
for (int i = 0; i = 15; i++) {
p2.add(b[i]);
} //創建按鈕 並添加到P2
b[16].setBackground(Color.yellow);
txt = new TextField(15);
txt.setEditable(false);
for (int i = 0; i = 16; i++) {
b[i].addActionListener(new buttonlistener());//添加監聽器
}
b[16].addActionListener(new close());
fm.addWindowListener(this);
fm.setBackground(Color.red);
p1.setLayout(new BorderLayout());
p1.add(txt, “North”);
p2.setLayout(new GridLayout(4, 4));
p3.setLayout(new BorderLayout());
p3.add(b[16]);
fm.add(p1, “North”);
fm.add(p2, “Center”);
fm.add(p3, “South”);
fm.pack();
fm.setVisible(true);//都是些窗中設置 添加相關組件和監聽器
}
public void windowClosing(WindowEvent e) {
System.exit(0);//退出系統
}
class buttonlistener implements ActionListener {//編寫監聽器事件 通過按鍵得出給果
public void actionPerformed(ActionEvent e) {
Button btn = (Button) e.getSource();
if (btn.getLabel() == “=”) {
jisuan();
str = String.valueOf(a);
txt.setText(str);
s = “”;
} else if (btn.getLabel() == “+”) {
jisuan();
txt.setText(“”);
s = “+”;
} else if (btn.getLabel() == “-“) {
jisuan();
txt.setText(“”);
s = “-“;
} else if (btn.getLabel() == “/”) {
jisuan();
txt.setText(“”);
s = “/”;
} else if (btn.getLabel() == “*”) {
jisuan();
txt.setText(“”);
s = “*”;
} else {
txt.setText(txt.getText() + btn.getLabel());
if (btn.getLabel() == “清空”)
txt.setText(“”);
}
}
public void jisuan() {//編寫具體計算方法
if (s == “+”)
a += Double.parseDouble(txt.getText());
else if (s == “-“)
a -= Double.parseDouble(txt.getText());
else if (s == “*”)
a *= Double.parseDouble(txt.getText());
else if (s == “/”)
a /= Double.parseDouble(txt.getText());
else
a = Double.parseDouble(txt.getText());
}
}
}
class close implements ActionListener {//退出
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}
如何用eclipse創建一個畫板
本文介紹如何使用eclipse建立一個java應用程序 材料/工具 myeclipse 方法 eclipse和myeclipse創建java項目的步驟是一樣的,所以 以myeclipse為例。 1、File — New — Java Project 2、進入到下面這個頁面,“Create a java Project”就是創建
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/284505.html