窗體寫好了,運算你自己寫
江南ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場景,ssl證書未來市場廣闊!成為創(chuàng)新互聯(lián)建站的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:028-86922220(備注:SSL證書合作)期待與您的合作!
import java.awt.Button;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class SimpleCalculator {
private JFrame f = new JFrame();
private JPanel firstPanel = new JPanel();
private JLabel firstNumLabel = new JLabel("第一個數(shù)字");
private JTextField number1 = new JTextField();
private JPanel secondPanel = new JPanel();
private Button add = new Button("+");
private Button mul = new Button("*");
private Button clear = new Button("清除");
private JPanel thirdPanel = new JPanel();
private JLabel resultLabel = new JLabel("結(jié)果為");
private JTextField result = new JTextField();
public SimpleCalculator(){
firstPanel.setLayout(new GridLayout(1, 2));
firstPanel.add(firstNumLabel);
firstPanel.add(number1);
secondPanel.setLayout(new GridLayout(1, 3));
secondPanel.add(add);
secondPanel.add(mul);
secondPanel.add(clear);
thirdPanel.setLayout(new GridLayout(1, 2));
thirdPanel.add(resultLabel);
thirdPanel.add(result);
f.add(new JLabel("簡易計算器"));
f.add(firstPanel);
f.add(secondPanel);
f.add(thirdPanel);
f.setLayout(new GridLayout(4, 1));
f.setVisible(true);
f.pack();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new SimpleCalculator();
}
}
importjava.awt.*;importjava.awt.event.*;importjava.awt.geom.*;importjava.util.*;importjavax.swing.*;/***多線程,小球演示.打開Windows任務(wù)管理器,可看到線程變化??伤阉鞯?,run()方法/.start()**du:程序技巧體會:所謂產(chǎn)生一個小球,即是new其類對象,其屬性攜帶畫小球的坐標(biāo)、顏色、所在容器等參數(shù)。**一個類,屬性用來作為參數(shù)容器用,方法.完成功能。**///運行類publicclassBouncePress{//publicstaticvoidmain(String[]args){JFrameframe=newBouncePressFrame();//生成窗口。執(zhí)行構(gòu)造。-----業(yè)務(wù)邏輯。frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//similarto//window//listenerframe.show();}}classBouncePressFrameextendsJFrame{privateBallPressCanvascanvas;publicBouncePressFrame(){setSize(600,500);//窗口大小setTitle("BounceBall");ContainercontentPane=getContentPane();//Swing的窗口不能直接放入東西,只能在其上的ContentPane上放。canvas=newBallPressCanvas();//生成一個新面板。-----canvascontentPane.add(canvas,BorderLayout.CENTER);//窗口中心加入該面板。JPanelbuttonPanel=newJPanel();//再生成一個新面板。----buttonPanel//調(diào)用本類方法addButton。addButton(buttonPanel,"Start",//生成一個按鈕"Start"---加入面板buttonPanelnewActionListener(){//|------按鈕綁上action監(jiān)聽器。publicvoidactionPerformed(ActionEventevt){//|小球容器對象的addBall(Thread.NORM_PRIORITY-4,Color.black);//事件處理時,執(zhí)行---addBall()方法。---產(chǎn)生小球(參數(shù)對象)---加入List中---開始畫球。}});//按一次,addBall()一次---產(chǎn)生一個新小球---加入List中---開始畫此新小球。//---畫球線程BallPressThread的run()---小球(參數(shù)對象).move()---每次畫時,先移動,再判斷,再畫。//---BallPressCanvas類的canvas對象.paint()---自動調(diào)BallPressCanvas類的paintComponent(Graphics//g)方法。//---該方法,從List中循環(huán)取出所有小球,第i個球,---調(diào)該小球BallPress類//.draw()方法---調(diào)Graphics2D方法畫出小球。--使用color/addButton(buttonPanel,"Express",newActionListener(){publicvoidactionPerformed(ActionEventevt){addBall(Thread.NORM_PRIORITY+2,Color.red);}});addButton(buttonPanel,"Close",newActionListener(){publicvoidactionPerformed(ActionEventevt){System.exit(0);}});contentPane.add(buttonPanel,BorderLayout.SOUTH);}publicvoidaddButton(Containerc,Stringtitle,ActionListenerlistener){JButtonbutton=newJButton(title);//生成一個按鈕。c.add(button);//加入容器中。button.addActionListener(listener);//按鈕綁上action監(jiān)聽器。}/**主要業(yè)務(wù)方法。*/publicvoidaddBall(intpriority,Colorcolor){//生成小球(參數(shù)對象)BallPressb=newBallPress(canvas,color);//生成BallPress對象,攜帶、初始化//畫Ball形小球,所需參數(shù):所在容器組件,所需color--black/red.//小球加入List中。canvas.add(b);//面板canvas的ArrayList中加入BallPress對象。BallPressThreadthread=newBallPressThread(b);//生成畫小球的線程類BallPressThread對象。傳入BallPress對象(攜帶了畫球所需//容器、color參數(shù))。thread.setPriority(priority);thread.start();//callrun(),ballstarttomove//畫球線程開始。---BallPressThread的run()---小球(參數(shù)對象).move()---先移動,再畫。canvas.paint---BallPressCanvas類的}}//畫球的線程類。classBallPressThreadextendsThread{privateBallPressb;publicBallPressThread(BallPressaBall){b=aBall;}//畫球開始。publicvoidrun(){try{for(inti=1;i自動繪制面板,且自動調(diào)paintComponent(Graphics//g)方法,---重寫該方法,繪制面板(及其上組件)。//作用2)該類對象屬性ArrayListballs---兼作小球(參數(shù)對象)的容器。classBallPressCanvasextendsJPanel{privateArrayListballs=newArrayList();publicvoidadd(BallPressb){balls.add(b);//向ArrayList中添加球。當(dāng)按下按鈕,添加多個球時,都保存在這個List中。}//重寫了javax.swing.JComponent的paintComponent()方法。//paint()方法自動調(diào)用該方法。publicvoidpaintComponent(Graphicsg){super.paintComponent(g);Graphics2Dg2=(Graphics2D)g;for(inti=0;i=canvas.getWidth()){//小球右邊已經(jīng)到畫板右邊。x=canvas.getWidth()-15;dx=-dx;//開始反向運動。}if(y=canvas.getHeight()){//小球已到畫板頂。y=canvas.getHeight()-15;dy=-dy;}canvas.paint(canvas.getGraphics());//畫出面板對象canvas----(及其上所有組件)////.paint()方法,自動調(diào)用}}/*importjava.awt.*;importjava.awt.event.*;importjava.awt.geom.*;importjava.util.*;importjavax.swing.*;*//***單線程,小球演示搜索不到,run()方法/.start()*//*publicclassBounce{publicstaticvoidmain(String[]args){JFrameframe=newBounceFrame();frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//similarto//window//listenerframe.show();}}不懂的再問啊。。。
怎么用java寫一個窗體程式?
下面介紹如何用簡單的幾句話在eclipse環(huán)境下出現(xiàn)一個視窗。
首先寫一個frame類,繼承Frame,是繼承widows 然后把,出現(xiàn)視窗的語句封裝成一個函式
public void lunchFrame(){
this.setLocation(0,0);
this.setSize(20,20);
setVisible(True); ?一定要寫這句話
}
最后只需要在主函式里面呼叫就可以
Java是一門面向物件程式語言,不僅吸收了C++語言的各種優(yōu)點,還摒棄了C++里難以理解的多繼承、指標(biāo)等概念,因此Java語言具有功能強(qiáng)大和簡單易用兩個特征。Java語言作為靜態(tài)面向物件程式語言的代表,極好地實現(xiàn)了面向物件理論,允許程式設(shè)計師以優(yōu)雅的思維方式進(jìn)行復(fù)雜的程式設(shè)計 。
Java具有簡單性、面向物件、分散式、健壯性、安全性、平臺獨立與可移植性、多執(zhí)行緒、動態(tài)性等特點 。Java可以編寫桌面應(yīng)用程式、Web應(yīng)用程式、分散式系統(tǒng)和嵌入式系統(tǒng)應(yīng)用程式等。
怎么用c#寫一個程式讓一個標(biāo)簽繞窗體走一圈
這個功能很奇葩,樓主說的是窗體應(yīng)用程式么?如果是的話,這是原始碼。
怎么用JAVA寫一個使用者登入程式
同意樓上的說法,具體點可以這樣:建立一個使用者表,里邊包括LoginName(登入名),UserName(使用者名稱),Password(密碼),Age(年齡),Address(地址)。然后編寫Java程式(用MVC架構(gòu))模型層(M):DBConnection.java(負(fù)責(zé)連線資料庫)
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.*;
public class DBConnection {
private static final String DRIVER_CLASS = "sun.jdbc.odbc.JdbcOdbcDriver";
private static final String DB_URL = "jdbc:odbc:text";
public DBConnection() {
}
public static Connection getConnection() {
Connection conn = null;
try {
Class.forName(DRIVER_CLASS);
conn = DriverManager.getConnection(DB_URL);
} catch (SQLException ex) {
System.out.println(ex.getMessage());
} catch (ClassNotFoundException ex) {
System.out.println(ex.getMessage());
}
return conn;
}
}
第2個負(fù)責(zé)資料庫查詢操作的類:DBUserManager.java
import edu.sys.text.model.entity.User;
import edu.sys.text.model.dao.DBConnection;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.DriverManager;
import java.sql.*;
public class DBUserManager {
private static final String SQL_SELECT =
"SELECT LoginName,UserName,PassWord,Age,Address FROM UserInfo WHERE LoginName = ? AND PassWord = ?";
public DBUserManager() {
}
public boolean checkDB(User u) {
boolean b = false;
Connection conn = null;
PreparedStatement p *** t = null;
ResultSet rs = null;
conn = DBConnection.getConnection();
try {
p *** t = conn.prepareStatement(SQL_SELECT);
p *** t.setString(1, u.getLoginName());
p *** t.setString(2, u.getPassWord());
rs = p *** t.executeQuery();
b = rs.next();
if (rs.next()) {
b = true;
}
} catch (SQLException ex) {
System.out.println(ex.getMessage());
} finally {
cleanDB(rs, p *** t, conn);
}
return b;
}
public User checkBC(User u) {
Connection conn = null;
PreparedStatement p *** t = null;
ResultSet rs = null;
User tmp = new User();
conn = DBConnection.getConnection();
try {
p *** t = conn.prepareStatement(SQL_SELECT);
p *** t.setString(1, u.getLoginName());
p *** t.setString(2, u.getPassWord());
rs = p *** t.executeQuery();
if (rs.next()) {
tmp.setLoginName(rs.getString(1));
tmp.setUserName(rs.getString(2));
tmp.setAge(rs.getInt(4));
tmp.setAddress(rs.getString(5));
}
} catch (SQLException ex) {
System.out.println(ex.getMessage());
} finally {
cleanDB(rs, p *** t, conn);
}
return tmp;
}
public void cleanDB(ResultSet rs, PreparedStatement p *** t, Connection conn) {
try {
if (rs != null) {
rs.close();
}
if (p *** t != null) {
p *** t.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException ex) {
System.out.println(ex.getMessage());
}
}
第3個實體使用者類:User.java
package edu.sys.text.model.entity;
public class User {
private String loginName;
private String userName;
private String passWord;
private int age;
private String address;
public User() {
}
public void setLoginName(String loginName) {
this.loginName = loginName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
public void setAge(int age) {
this.age = age;
}
public void setAddress(String address) {
this.address = address;
}
public String getLoginName() {
return loginName;
}
public String getUserName() {
return userName;
}
public String getPassWord() {
return passWord;
}
public int getAge() {
return age;
}
public String getAddress() {
return address;
}
}
然后編寫控制層(C):GetInfoServlet.java
package edu.sys.text.control;
import javax.servlet.*;
import javax.servlet..*;
import java.io.*;
import java.util.*;
import edu.sys.text.model.entity.User;
import edu.sys.text.model.service.UserManager;
public class GetInfoServlet extends HttpServlet {
private static final String CONTENT_TYPE = "text/; charset=GBK";
Initialize global variables
public void init() throws ServletException {
}
Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
}
Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
String loginName = request.getParameter("loginName");
String passWord = request.getParameter("passWord");
User u = new User();
u.setLoginName(loginName);
u.setPassWord(passWord);
UserManager m = new UserManager();
RequestDispatcher d;
if (m.checkUser(u)) {
User o = m.checkBC(u);
request.setAttribute("JavaBEAN",o);
d = request.getRequestDispatcher("GetInfoUser.jsp");
} else {
d = request.getRequestDispatcher("GetInfoFinale.jsp");
}
d.forward(request, response);
}
Clean up resources
public void destroy() {
}
}
最后,建立表示層(V):包括3個Jsp(登入頁面GetInfo.jsp、登入成功頁面GetInfoUser.jsp、登入失敗頁面GetInfoFinale.jsp)
上面的就是Jsp結(jié)合Servlet用MVC架構(gòu)寫的使用者登入程式。
用java編寫一個窗體資料輸入比較程式
使用畫圖功能,關(guān)于比較那是很簡單的邏輯
JFrame frame = new JFrame("XXX");
ShootGame game = new ShootGame(); 面板物件
frame.add(game); 將面板新增到JFrame中
frame.setSize(WIDTH, HEIGHT); 設(shè)定大小
frame.setAlwaysOnTop(true); 設(shè)定其總在最上
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 預(yù)設(shè)關(guān)閉操作
frame.setIconImage(new ImageIcon("images/icon.jpg").getImage()); 設(shè)定窗體的圖示
frame.setLocationRelativeTo(null); 設(shè)定窗體初始位置
frame.setVisible(true); 盡快呼叫paint
game.action(); 啟動執(zhí)行
怎么用java寫一個tomcat的啟動,停止程式
可以利用Runtime類,Runtime用于別是虛擬機(jī)器執(zhí)行時的狀態(tài),它用于封裝JVM虛擬機(jī)器程序。
看看,我給你寫個程式碼:
public class Run {
public static void main(String[] args) throws Exception {
Runtime run=Runtime.getRuntime();
Process process=run.exec("Tomcat.exe");
Thread.sleep(3000);
process.destroy();
}
}
如題,寫一個小程式,用swing介面的桌面應(yīng)用程式就行,用來啟動、停止tomcat伺服器,啟動后不顯示那個cmd視窗
怎么用vc++寫一個登陸的視窗程式
哥連資料庫不?ado還是odbc?什么資料庫?
怎么用JAVA來寫一個小游戲程式
首先你應(yīng)該要具備程式設(shè)計的基礎(chǔ)知識水平,利用Elicpse等軟體來寫程式碼,既而來實現(xiàn)相應(yīng)的功能,也可以用VC++等來實現(xiàn)圖形化介面設(shè)計呢。
importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.awt.event.KeyEvent;importjava.awt.event.KeyListener;importjavax.swing.JButton;importjavax.swing.JEditorPane;importjavax.swing.JFrame;importjavax.swing.JLabel;importjavax.swing.JPanel;importjavax.swing.JTextField;publicclassWindowTestextendsJFrameimplementsActionListener,KeyListener{privatestaticfinallongserialVersionUID=1L;/***主方法*/publicstaticvoidmain(String[]args){WindowTestwin=newWindowTest();}/***下面是具體實現(xiàn)*/JTextFieldtext;JButtonbutton;JEditorPanetextArea;publicWindowTest(){super("測試窗體");text=newJTextField(15);text.addKeyListener(this);JPanelp1=newJPanel();p1.add(newJLabel("輸入字符:"));p1.add(text);button=newJButton("清除");button.addActionListener(this);p1.add(button);p1.setBounds(5,5,220,100);textArea=newJEditorPane();textArea.setBounds(1,1,216,200);JPanelp2=newJPanel();p2.add(newJLabel("顯示字符:"));p2.add(textArea);p2.setBounds(5,115,340,220);JPanelp3=newJPanel();p3.add(p1);p3.add(p2);add(p3);setBounds(160,60,400,300);setVisible(true);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}@OverridepublicvoidkeyPressed(KeyEvente){}@OverridepublicvoidkeyReleased(KeyEvente){if(e.getKeyCode()==KeyEvent.VK_ENTER){textArea.setText("");}else{Stringstr=text.getText();textArea.setText(str);}}@OverridepublicvoidkeyTyped(KeyEvente){}@OverridepublicvoidactionPerformed(ActionEvente){text.setText("");textArea.setText("");}}
public?class?Demo?extends?JFrame
{
JButton?jb;?//一個按鈕
public?static?void?main(String?[]args){
new?Demo();
}
public?Demo()
{
this.setLayout(new?FlowLayout());
jb=new?JButton("按扭");
this.add(jb);
this.setSize(400,300);
this.setVisible(true);
this.setLocation(500,?200);
}
}