重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
1、首先,我们需要在代码中导入相应的包,以便能够使用 JFrame 类。然后,新建一个窗口类继承自 JFrame 类。
创新互联建站是一家专注于成都网站设计、成都网站建设与策划设计,集贤网站建设哪家好?创新互联建站做网站,专注于网站建设十载,网设计领域的专业建站公司;建站业务涵盖:集贤等地区。集贤做网站价格咨询:18982081108
2、在窗口类中创建一个初始化方法,我们需要在该方法中初始化窗口类对象,并将其显示出来。
3、对窗口对象进行初始化时,我们先设置好窗口的标题。
4、再设置窗口的大小,参数分别为窗口的长和宽,单位是像素。
5、接着设置窗口左上角的坐标位置,以确定窗口的位置。参数分别为窗口左上角顶点的 x 坐标和 y 坐标。
6、最后,调用 setVisible 方法将窗口显示出来。参数为 true 表示显示,为 false 表示隐藏。
7、窗口类写好后,我们在 main 方法中创建一个窗口类对象,然后调用该对象的初始化方法就可以将窗口显示出来了。
1到2,需要2的对象,2到3需要3的对象。 在3中加一个属性,属性类型就是2, 2在跳转3的时候,产生3的对象,3对象.属性(这个属性是2类型)=this(这个this就是2本身),3打开后,3.属性在打开,就回到2.
不知道你懂不懂,就是一个引用的传递,将自己传过去,因为如果不传自己,将打开新的窗体,不是之前的。
计算器:import java.awt.*;
import java.awt.event.*;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.TextField;
import java.awt.Rectangle;
import java.awt.Button;
import java.awt.Label;public class suanfa extends JFrame {private static final long serialVersionUID = 1L;private JPanel jContentPane = null;private TextField textField = null;private TextField textField1 = null;private Button button = null;private Button button1 = null;private Button button2 = null;private Button button3 = null;private Label label = null;
double awn;
/**
* This is the default constructor
*/
public suanfa() {
super();
initialize();
}/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(300, 250);
this.setContentPane(getJContentPane());
this.setTitle("JFrame");
}/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
label = new Label();
label.setBounds(new Rectangle(12, 129, 90, 27));
label.setText(String.format("%.6f",awn));
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(getTextField(), null);
jContentPane.add(getTextField1(), null);
jContentPane.add(getButton(), null);
jContentPane.add(getButton1(), null);
jContentPane.add(getButton2(), null);
jContentPane.add(getButton3(), null);
jContentPane.add(label, null);
}
return jContentPane;
}/**
* This method initializes textField
*
* @return java.awt.TextField
*/
private TextField getTextField() {
if (textField == null) {
textField = new TextField();
textField.setBounds(new Rectangle(11, 20, 75, 32));
}
return textField;
}/**
* This method initializes textField1
*
* @return java.awt.TextField
*/
private TextField getTextField1() {
if (textField1 == null) {
textField1 = new TextField();
textField1.setBounds(new Rectangle(11, 67, 75, 32));
}
return textField1;
}/**
* This method initializes button
*
* @return java.awt.Button
*/
private Button getButton() {
if (button == null) {
button = new Button("加");
button.setBounds(new Rectangle(134, 17, 71, 29));
button.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
try{
awn=Double.valueOf(textField.getText()) + Double.valueOf(textField1.getText());
label.setText(String.format("%.6f",awn));
}
catch(Exception ex){
return;
}
}
});
}
return button;
}/**
* This method initializes button1
*
* @return java.awt.Button
*/
private Button getButton1() {
if (button1 == null) {
button1 = new Button("减");
button1.setBounds(new Rectangle(134, 52, 71, 29));
button1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
try{
awn=Double.valueOf(textField.getText()) - Double.valueOf(textField1.getText());
label.setText(String.format("%.6f",awn));
}
catch(Exception ex){
return;
}}
});
}
return button1;
}/**
* This method initializes button2
*
* @return java.awt.Button
*/
private Button getButton2() {
if (button2 == null) {
button2 = new Button("乘");
button2.setBounds(new Rectangle(134, 91, 71, 29));
button2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
try{
awn=Double.valueOf(textField.getText()) * Double.valueOf(textField1.getText());
label.setText(String.format("%.6f",awn));
}
catch(Exception ex){
return;
}}
});
}
return button2;
}/**
* This method initializes button3
*
* @return java.awt.Button
*/
private Button getButton3() {
if (button3 == null) {
button3 = new Button("除");
button3.setBounds(new Rectangle(134, 133, 71, 29));
button3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
try{
awn=Double.valueOf(textField.getText()) / Double.valueOf(textField1.getText());
label.setText(String.format("%.6f",awn));
}
catch(Exception ex){
return;
}}
});
}
return button3;
}
public static void main(String[] args)
{
new suanfa().setVisible(true);
}
}
这样:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.Graphics;
public class MainClass extends JFrame {
public JComboBox box;
int flag = 0;
jpNewPanel jpNewPanel;
public static void main(String[] args) {
MainClass frame = new MainClass();
frame.setBounds(650,300,550,550);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("信号灯");
frame.setVisible(true);
}
public MainClass() {
box = new JComboBox();
box.addItem("请选择");
box.addItem("红灯");
box.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
flag = box.getSelectedIndex();
jpNewPanel.repaint();
}
});
box.addItem("黄灯");
box.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
flag = box.getSelectedIndex();
jpNewPanel.repaint();
}
});
box.addItem("绿灯");
box.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
flag = box.getSelectedIndex();
jpNewPanel.repaint();
}
});
add(box, BorderLayout.NORTH);
jpNewPanel = new jpNewPanel();
add(jpNewPanel, BorderLayout.CENTER);
}
class jpNewPanel extends JPanel {
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawOval(150, 0, 120, 120);
if (flag == 1) {
g.setColor(Color.RED);
g.fillOval(150, 0, 120, 120);
} else if (flag == 2) {
g.setColor(Color.YELLOW);
g.fillOval(150, 0, 120, 120);
} else if (flag == 3) {
g.setColor(Color.GREEN);
g.fillOval(150, 0, 120, 120);
}
}
}
}
扩展资料:
注意事项
每个Road对象都有一个name成员变量来代表方向,有一个vehicles成员变量来代表方向上的车辆集合。
在Road对象的构造方法中启动一个线程每隔一个随机的时间向vehicles集合中增加一辆车(用一个“路线名_id”形式的字符串进行表示)。
在Road对象的构造方法中启动一个定时器,每隔一秒检查该方向上的灯是否为绿,是则打印车辆集合和将集合中的第一辆车移除掉。
图片看起来很模糊,隐约看到需要一个登录窗口,那就分享一下以前练习的登录窗口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();}}不懂的再问啊。。。