窗体写好了,运算你自己写
茂名网站制作公司哪家好,找创新互联建站!从网页设计、网站建设、微信开发、APP开发、成都响应式网站建设等网站项目制作,到程序开发,运营维护。创新互联建站于2013年创立到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选创新互联建站。
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("第一个数字");
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("结果为");
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();
}
}
图片看起来很模糊,隐约看到需要一个登录窗口,那就分享一下以前练习的登录窗口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(){
//设置窗口标题
setTitle("登录界面");
//设置一个初始面板,填充整个窗口
JPanel loginPanel = new JPanel();
//设置背景颜色
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);//设置窗口大小
setLocation(Screen.getCenterPosition(LOGIN_WIDTH, LOGIN_HEIGHT));//设置窗口位置
setDefaultCloseOperation(EXIT_ON_CLOSE);//设置窗口默认关闭方式
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任务管理器,可看到线程变化。可搜索到,run()方法/.start()**du:程序技巧体会:所谓产生一个小球,即是new其类对象,其属性携带画小球的坐标、颜色、所在容器等参数。**一个类,属性用来作为参数容器用,方法.完成功能。**///运行类publicclassBouncePress{//publicstaticvoidmain(String[]args){JFrameframe=newBouncePressFrame();//生成窗口。执行构造。-----业务逻辑。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//调用本类方法addButton。addButton(buttonPanel,"Start",//生成一个按钮"Start"---加入面板buttonPanelnewActionListener(){//|------按钮绑上action监听器。publicvoidactionPerformed(ActionEventevt){//|小球容器对象的addBall(Thread.NORM_PRIORITY-4,Color.black);//事件处理时,执行---addBall()方法。---产生小球(参数对象)---加入List中---开始画球。}});//按一次,addBall()一次---产生一个新小球---加入List中---开始画此新小球。//---画球线程BallPressThread的run()---小球(参数对象).move()---每次画时,先移动,再判断,再画。//---BallPressCanvas类的canvas对象.paint()---自动调BallPressCanvas类的paintComponent(Graphics//g)方法。//---该方法,从List中循环取出所有小球,第i个球,---调该小球BallPress类//.draw()方法---调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监听器。}/**主要业务方法。*/publicvoidaddBall(intpriority,Colorcolor){//生成小球(参数对象)BallPressb=newBallPress(canvas,color);//生成BallPress对象,携带、初始化//画Ball形小球,所需参数:所在容器组件,所需color--black/red.//小球加入List中。canvas.add(b);//面板canvas的ArrayList中加入BallPress对象。BallPressThreadthread=newBallPressThread(b);//生成画小球的线程类BallPressThread对象。传入BallPress对象(携带了画球所需//容器、color参数)。thread.setPriority(priority);thread.start();//callrun(),ballstarttomove//画球线程开始。---BallPressThread的run()---小球(参数对象).move()---先移动,再画。canvas.paint---BallPressCanvas类的}}//画球的线程类。classBallPressThreadextendsThread{privateBallPressb;publicBallPressThread(BallPressaBall){b=aBall;}//画球开始。publicvoidrun(){try{for(inti=1;i自动绘制面板,且自动调paintComponent(Graphics//g)方法,---重写该方法,绘制面板(及其上组件)。//作用2)该类对象属性ArrayListballs---兼作小球(参数对象)的容器。classBallPressCanvasextendsJPanel{privateArrayListballs=newArrayList();publicvoidadd(BallPressb){balls.add(b);//向ArrayList中添加球。当按下按钮,添加多个球时,都保存在这个List中。}//重写了javax.swing.JComponent的paintComponent()方法。//paint()方法自动调用该方法。publicvoidpaintComponent(Graphicsg){super.paintComponent(g);Graphics2Dg2=(Graphics2D)g;for(inti=0;i=canvas.getWidth()){//小球右边已经到画板右边。x=canvas.getWidth()-15;dx=-dx;//开始反向运动。}if(y=canvas.getHeight()){//小球已到画板顶。y=canvas.getHeight()-15;dy=-dy;}canvas.paint(canvas.getGraphics());//画出面板对象canvas----(及其上所有组件)////.paint()方法,自动调用}}/*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);
}
}
网站栏目:java窗体界面代码 java窗体设计代码
转载源于:https://www.cdcxhl.com/article4/ddepsie.html
成都网站建设公司_创新互联,为您提供软件开发、面包屑导航、ChatGPT、用户体验、服务器托管、做网站
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联