「java窗体怎么」java窗体怎么滚动字暮

博主:adminadmin 2023-01-23 09:24:07 300

本篇文章给大家谈谈java窗体怎么,以及java窗体怎么滚动字暮对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

java怎么给窗体或者面板设置背景图片?

1利用标签组件来设置\x0d\x0a标签本来是一种最简单的组件,为什么可以将它设置成为一个窗体的背景呢?首先还是要了解框架JFrame中的层次结构。JFrame中的层次分布及相对关系是:最底层是JRootPane,第二层是JlayerPane,最上层就是ContentPane,也正是我们常说的内容面板。所以一般我们拖放的控件就是在ContentPane层上。也就是说我们只需将背景图片放在JFrame的第二层是JlayerPane上,再把内容面板ContentPane设置为透明,则第二层JlayerPane上放置的图片即成为内容面板的背景了。具体代码如下:\x0d\x0aJPanelpnlMain=newJPanel();//创建面板pnlMain。\x0d\x0agetContentPane().add(pnlMain);//将pnlMain设置为主面板。\x0d\x0aIconi=newImageIcon("背景.jpg");/*用源图片“背景.jpg”构造一个ImageIcon对象i,需要注意如果图片的路径使用的是相对路径,则图片文件必须放在类文件所在文件夹或项目的根文件夹中,否则图片的路径必须用绝对路径。*/\x0d\x0aJLabellblLogo=newJLabel(i);//用指定的图片构造标签对象lb\x0d\x0athis.getLayeredPane().add(lb,newInteger(Integer.MIN_VALUE));\x0d\x0a//把标签放在第二层JlayerPane上。\x0d\x0alb.setBounds(0,0,ii.getIconWidth(),i.getIconHeight());\x0d\x0a//设置标签的尺寸,即背景图象的大小。\x0d\x0agetConentPane().setOpaque(false);/*把内容面板设置为透明,这样整个框架的背景就不再是内容面板的背景色,而是第二层中标签的图像。*/\x0d\x0apnlMain.add(lb);//将标签添加到主面板pnlMain中。\x0d\x0a用标签组件JLabel来设置窗体背景,其扩展性上比较差,且在标签上不能放置其他组件,比如:在放置一个JButton,整个布局背景图就错乱。导致这种现象是因为Java加载组件是有顺序的,作为背景的JLabel的代码一定要放在全部组件代码的最后,这样JLabel的背景图片才不会被其他组件遮住,从而使整个布局背景错乱。所以采用这种方式来设置窗体背景有很大的局限性。因此最好是采用以下方式来设置窗体背景。\x0d\x0a2通过JPanel面板来设置窗体背景\x0d\x0aJPanel面板是Java中的容器之一。而Java中的容器是没有背景图片的属性的,它们只有背景颜色,如果需要在JPanel面板上设置窗体背景,就需要重写paintComponent(Graphicsg)方法,即把所要设置的背景图片画上作为JPanel面板的背景。具体实现如下:\x0d\x0a首先定义一个JPanel的子类BjPanel,由于JPanel的构造方法不能添加图像,因此在创建此子类的时候先用getImage载入一幅背景图片,在重写paintComponent(Graphicsg)方法时,利用drawImage方法将其逐渐绘制到屏幕上,并将该面板添加到框架中,最后将该面板设置为透明。其代码如下:\x0d\x0aimportjava.awt.*;\x0d\x0aimportjavax.swing.*;\x0d\x0apublicclassbkground\x0d\x0a{\x0d\x0apublicstaticvoidmain(Stringargs[])\x0d\x0a{\x0d\x0aBjFramef=newBjFrame();\x0d\x0af.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);\x0d\x0af.setVisible(true);\x0d\x0a}\x0d\x0a}\x0d\x0aclassBjFrameextendsJFrame\x0d\x0a{\x0d\x0apublicBjFrame()\x0d\x0a{\x0d\x0asetSize(WTH,HHT);\x0d\x0aBjPanelpl=newBjPanel();\x0d\x0aContainercontentPane=getContentPane();\x0d\x0acontentPane.add(pl);\x0d\x0apl.setOpaque(true);\x0d\x0a}\x0d\x0apublicstaticfinalintWTH=300;\x0d\x0apublicstaticfinalintHHT=200;\x0d\x0a}\x0d\x0aclassBjPanelextendsJPanel\x0d\x0a{\x0d\x0aImageim;\x0d\x0apublicBjPanel()\x0d\x0a{\x0d\x0aim=Toolkit.getDefaultToolkit().getImage("背景.jpg");//需要注意的是如果用相对路径载入图片,则图片文件必须放在类文件所在文件夹或项目的根文件夹中,否则必须用绝对路径。\x0d\x0a}\x0d\x0apublicvoidpaintComponent(Graphicsg)\x0d\x0a{\x0d\x0asuper.paintComponent(g);\x0d\x0aintimWidth=image.getWidth(this);\x0d\x0aIntimHeight=image.getHeight(this);//定义图片的宽度、高度\x0d\x0aintFWidth=getWidth();\x0d\x0aintFHeight=getHeight();//定义窗口的宽度、高度\x0d\x0aintx=(FWidth-imWidth)/2;\x0d\x0ainty=(FHeight-imHeight)/2;//计算图片的坐标,使图片显示在窗口正中间\x0d\x0ag.drawImage(image,x,y,null);//绘制图片\x0d\x0a}\x0d\x0a}

怎么用java写一个窗体程式?

怎么用java写一个窗体程式?

下面介绍如何用简单的几句话在eclipse环境下出现一个视窗。

首先写一个frame类,继承Frame,是继承widows 然后把,出现视窗的语句封装成一个函式

public void lunchFrame(){

this.setLocation(0,0);

this.setSize(20,20);

setVisible(True);  一定要写这句话

}

最后只需要在主函式里面呼叫就可以

Java是一门面向物件程式语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承、指标等概念,因此Java语言具有功能强大和简单易用两个特征。Java语言作为静态面向物件程式语言的代表,极好地实现了面向物件理论,允许程式设计师以优雅的思维方式进行复杂的程式设计 。

Java具有简单性、面向物件、分散式、健壮性、安全性、平台独立与可移植性、多执行绪、动态性等特点 。Java可以编写桌面应用程式、Web应用程式、分散式系统和嵌入式系统应用程式等。

怎么用c#写一个程式让一个标签绕窗体走一圈

这个功能很奇葩,楼主说的是窗体应用程式么?如果是的话,这是原始码。

怎么用JAVA写一个使用者登入程式

同意楼上的说法,具体点可以这样:建立一个使用者表,里边包括LoginName(登入名),UserName(使用者名称),Password(密码),Age(年龄),Address(地址)。然后编写Java程式(用MVC架构)模型层(M):DBConnection.java(负责连线资料库)

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个负责资料库查询操作的类: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结合Servlet用MVC架构写的使用者登入程式。

用java编写一个窗体资料输入比较程式

使用画图功能,关于比较那是很简单的逻辑

JFrame frame = new JFrame("XXX");

ShootGame game = new ShootGame(); 面板物件

frame.add(game); 将面板新增到JFrame中

frame.setSize(WIDTH, HEIGHT); 设定大小

frame.setAlwaysOnTop(true); 设定其总在最上

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 预设关闭操作

frame.setIconImage(new ImageIcon("images/icon.jpg").getImage()); 设定窗体的图示

frame.setLocationRelativeTo(null); 设定窗体初始位置

frame.setVisible(true); 尽快呼叫paint

game.action(); 启动执行

怎么用java写一个tomcat的启动,停止程式

可以利用Runtime类,Runtime用于别是虚拟机器执行时的状态,它用于封装JVM虚拟机器程序。

看看,我给你写个程式码:

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介面的桌面应用程式就行,用来启动、停止tomcat伺服器,启动后不显示那个cmd视窗

怎么用vc++写一个登陆的视窗程式

哥连资料库不?ado还是odbc?什么资料库?

怎么用JAVA来写一个小游戏程式

首先你应该要具备程式设计的基础知识水平,利用Elicpse等软体来写程式码,既而来实现相应的功能,也可以用VC++等来实现图形化介面设计呢。

java关闭窗体的六种方法

前段时间集中精力写了两篇论文 很久没写博文了 现在继续了

使用JFrame的enableEvents和processWindowEvent

//Frame java

import java awt *;

import java awt event *;

import javax swing *;

public class Frame extends JFrame {

public Frame () {

enableEvents(AWTEvent WINDOW_EVENT_MASK);

this setSize(new Dimension( ));

this setTitle( Frame );

}

protected void processWindowEvent(WindowEvent e) {

super processWindowEvent(e);

if (e getID() == WindowEvent WINDOW_CLOSING) {

System exit( );

}

}

}

直接实现WindowListener接口

//Frame java

import java awt *;

import java awt event *;

public class Frame extends Frame implements WindowListener {

public Frame () {

this setSize(new Dimension( ));

this setTitle( Frame );

this addWindowListener(this);

}

public void windowClosing(WindowEvent windowEvent) {

System exit( );

}

public void windowOpened(WindowEvent windowEvent) {  }

public void windowClosed(WindowEvent windowEvent) {  }

public void windowIconified(WindowEvent windowEvent) {  }

public void windowDeiconified(WindowEvent windowEvent) {  }

public void windowActivated(WindowEvent windowEvent) {  }

public void windowDeactivated(WindowEvent windowEvent) {  }

}

直接继承窗体适配器WindowAdapter

//Frame java

import java awt *;

import java awt event *;

public class Frame extends  WindowAdapter {

public Frame () {

Frame f=new Frame();

f setSize(new Dimension( ));

f setTitle( Frame );

f addWindowListener(this);

f setVisible(true);

}

public static void main(String[] s){

new Frame ();

}

public void windowClosing(WindowEvent windowEvent) {

System exit( );

}

}

间接继承窗体适配器WindowAdapter

//Frame java

import java awt *;

import java awt event *;

public class Frame extends  Frame {

public Frame () {

this setSize(new Dimension( ));

this setTitle( Frame );

this addWindowListener(new winAdapter());

this setVisible(true);

}

public static void main(String[] s){

new Frame ();

}

}

class winAdapter extends WindowAdapter{

public void windowClosing(WindowEvent windowEvent) {

System exit( );

}

}

间接实现WindowListener接口

//Frame java

import java awt *;

import java awt event *;

public class Frame extends  Frame {

public Frame () {

this setSize(new Dimension( ));

this setTitle( Frame );

this addWindowListener(new winEventHandle());

this setVisible(true);

}

public static void main(String[] s){

new Frame ();

}

}

class winEventHandle implements WindowListener {

public void windowClosing(WindowEvent windowEvent) {

System exit( );

}

public void windowOpened(WindowEvent windowEvent) {  }

public void windowClosed(WindowEvent windowEvent) {  }

public void windowIconified(WindowEvent windowEvent) {  }

public void windowDeiconified(WindowEvent windowEvent) {  }

public void windowActivated(WindowEvent windowEvent) {  }

public void windowDeactivated(WindowEvent windowEvent) {  }

}

使用Inner Class

//Frame java

import java awt *;

import java awt event *;

public class Frame {

public Frame (){

Frame f=new Frame();

f addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){

System exit( );

}

});

f setSize(new Dimension( ));

f setVisible(true);

}

public static void main(String[] s){

new Frame ();

}

}

Jframe的关闭方法

setDefaultCloseOperation(EXIT_ON_CLOSE);

frame的关闭方法如下

this addWindowListener(new java awt event WindowAdapter() {

public void windowClosing(java awt event WindowEvent e) {

System exit( );

}

lishixinzhi/Article/program/Java/hx/201311/27073

java窗体怎么的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java窗体怎么滚动字暮、java窗体怎么的信息别忘了在本站进行查找喔。