窗體寫好了,運算你自己寫
茂名網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)建站!從網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、成都響應(yīng)式網(wǎng)站建設(shè)等網(wǎng)站項目制作,到程序開發(fā),運營維護。創(chuàng)新互聯(lián)建站于2013年創(chuàng)立到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗和運維經(jīng)驗,來保證我們的工作的順利進行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)建站。
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();
}
}
圖片看起來很模糊,隱約看到需要一個登錄窗口,那就分享一下以前練習(xí)的登錄窗口demo吧。
先上效果圖:
登錄界面
源碼如下:
AbsoluteLoginFrame.java
public class AbsoluteLoginFrame extends JFrame {
private static final int LOGIN_WIDTH = 600;
private static final int LOGIN_HEIGHT = 400;
private static final long serialVersionUID = -2381351968820980500L;
public AbsoluteLoginFrame(){
? //設(shè)置窗口標題
? setTitle("登錄界面");
? //設(shè)置一個初始面板,填充整個窗口
? JPanel loginPanel = new JPanel();
? //設(shè)置背景顏色
? loginPanel.setBackground(new Color(204, 204, 204));//#CCC
? loginPanel.setLayout(null);
? JPanel centerPanel = new JPanel();
? centerPanel.setBackground(Color.WHITE);
? centerPanel.setBounds(114, 70, 360, 224);
? centerPanel.setLayout(null);
? JLabel jLabel = new JLabel("用戶名:");
? jLabel.setOpaque(true);
? jLabel.setBackground(Color.YELLOW);
? jLabel.setBounds(60, 60, 54, 20);
? JLabel label = new JLabel("密? ? 碼:");
? label.setOpaque(true);
? label.setBackground(Color.CYAN);
? label.setBounds(60, 90, 54, 20);
? JTextField textField = new JTextField(15);
? textField.setBounds(130, 60, 166, 21);
? JPasswordField passwordField = new JPasswordField(15);
? passwordField.setBounds(130, 90, 166, 21);
? JButton jButton = new JButton("登錄");
? jButton.setBounds(148, 120, 62, 28);
? centerPanel.add(jLabel);
? centerPanel.add(label);
? centerPanel.add(textField);
? centerPanel.add(jButton);
? centerPanel.add(passwordField);
? loginPanel.add(centerPanel);
? getContentPane().add(loginPanel);//將初始面板添加到窗口中
? setSize(LOGIN_WIDTH, LOGIN_HEIGHT);//設(shè)置窗口大小
? setLocation(Screen.getCenterPosition(LOGIN_WIDTH, LOGIN_HEIGHT));//設(shè)置窗口位置
? setDefaultCloseOperation(EXIT_ON_CLOSE);//設(shè)置窗口默認關(guān)閉方式
? setResizable(false);
? setVisible(true);
}
public static void main(String[] args) {
? new AbsoluteLoginFrame();
}
}
Screen.java
public class Screen {
private int width;
private int height;
public Screen(){
? Toolkit toolkit = Toolkit.getDefaultToolkit();
? Dimension screenSize = toolkit.getScreenSize();
? this.width = screenSize.width;
? this.height = screenSize.height;
}
public static Point getCenterPosition(int width, int height){
? Screen screen = new Screen();
? int x = (screen.getWidth() - width) / 2;
? int y = (screen.getHeight() - height) / 2;
? return new Point(x, y);
}
public int getWidth() {
? return width;
}
public void setWidth(int width) {
? this.width = width;
}
public int getHeight() {
? return height;
}
public void setHeight(int height) {
? this.height = height;
}
}
importjava.awt.*;importjava.awt.event.*;importjava.awt.geom.*;importjava.util.*;importjavax.swing.*;/***多線程,小球演示.打開Windows任務(wù)管理器,可看到線程變化??伤阉鞯?,run()方法/.start()**du:程序技巧體會:所謂產(chǎn)生一個小球,即是new其類對象,其屬性攜帶畫小球的坐標、顏色、所在容器等參數(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中添加球。當按下按鈕,添加多個球時,都保存在這個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();}}不懂的再問啊。。。
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);
}
}
網(wǎng)站欄目:java窗體界面代碼 java窗體設(shè)計代碼
轉(zhuǎn)載源于:http://bm7419.com/article4/ddepsie.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供軟件開發(fā)、面包屑導(dǎo)航、ChatGPT、用戶體驗、服務(wù)器托管、做網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)