
正文
java多窗口代码 java多窗口切换
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
JAVA用frame实现图中2个窗口 怎么写啊?
图片看起来很模糊java多窗口代码,隐约看到需要一个登录窗口java多窗口代码,那就分享一下以前练习java多窗口代码的登录窗口demo吧。
先上效果图java多窗口代码:
登录界面
源码如下:
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;
}
}
相关问答
Q1: 100张票,用java多线程实现3个窗口按顺序依次卖票,如何实现
很简单, 出票里加锁就行了完整代码:
public class Test {
public static void main(String[] args) {
for(int i=0; i3; i++){
new Thread("线程 " + i){
public void run() {
while(true){
int p = getNumber();
if(p 0 ){
System.out.println(getName() + " 票号: " + p);
}else{
System.out.println("没票了");
break;
}
}
};
}.start();
}
}
public static int num = 100; //总票数
/**
* synchronized 同步锁
* @return
*/
public static synchronized int getNumber(){
if(num 0){
return num --; //如果大于0, 则返回当前票并减少一张
}
return 0;
}
}
Q2: java 传值 两个窗口
下面的代码演示了两种方法传递x值到 B 窗口中,一种是通过 B 的构造方法,一种是通过 B 中的 x 的 setter 传递。
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
class A extends JFrame {
private int x = 10;
public A() {
this.setTitle("A");
this.setSize(300, 200);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new FlowLayout());
JButton button = new JButton("Open B");
button.addActionListener(e - {
// 通构造方法传递
B b = new B(this.x);
// 通过 setter 方法传递
b.setX(x);
b.setVisible(true);
});
this.add(button);
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
}
class B extends JFrame {
private int x;
public B(int x) {
this.setTitle("B");
this.setSize(300, 200);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setLayout(new FlowLayout());
this.x = x;
JButton button = new JButton("显示x的值");
button.addActionListener(e - {
JOptionPane.showMessageDialog(this, x);
});
this.add(button);
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
}
public class App {
public static void main(String[] args) {
new A().setVisible(true);
}
}
Q3: java怎么用代理模式实现多窗口运行
本文实例形式详述了java实现一个程序运行时的启动窗口效果,如常用的microsoft word、 borland jbuilder 等,这样的窗口称为信息窗口。使用信息窗口的好处是可以使用户在等待软件主界面出现前的一段时间中得知软件运行状态。本例将演示如何来实现信息窗口,当打开程序时,信息窗口先显示,并在窗口上倒计时,直到“waiting 0”时,关闭该窗口,显示程序的主窗口。
该功能的主要实现方法如下:
一般来说,大多数的信息窗口是没有标题栏的,因此信息窗口不能由继承jframe 类来实现,一种简单的做法是通过继承jwindow 来实现(当然继承window 类也可以,但一个原则是尽量使用swing 中的界面
类)。另外,本例用到java.awt 包中的mediatracker 类。使用该类的好处是可以更好地管理程序中要使用的图片,同时还可以保证图片和界面同时显示,避免了窗口显示后很久才显示图片的缺点。
具体操作步骤如下:
1.新建一个project,取名为jspleshwindowdemo,其他设置按默认值。
2.新建一个application ,取名为jspleshwindowdemo,主窗口取名为mainframe,主窗口标题取名为jspleshwindowdemo。
3.先来编写信息窗口的代码。新建一个新类spleshwindow.java,继承java.swing.jwindow类。在spleshwindow 类中,定义新的属性,代码如下:
private string statusstr=null; //信息窗口中要显示的信息
private image logoimg=null; //信息窗口中的显示图片
4.向构造方法中添加代码,加载图片并初始化窗体,实现代码如下:
public spleshwindow(jframe owner) { //以jframe 对象为参数,可以是信息窗口和主窗口交互
super( owner );
// 加载图片
logoimg=gettoolkit().getimage( classloader.getsystemresource("images/splesh.jpg") );
// 等待图片加载完成
java.awt.mediatracker tracker=new java.awt.mediatracker( this ); //创建一个mediatracker 对象
tracker.addimage( logoimg , 0 ); //将图片放入mediatracker 对象中,序号为0
try{ //等待直到图片加载完成
tracker.waitforall();
}catch ( interruptedexception e ) {
e.printstacktrace();
}
// 设置信息窗体在屏幕上的显示位置
setlocation( gettoolkit().getscreensize().width/2 - logoimg.getwidth(this)/2 , gettoolkit().getscreensize().height/2 -
logoimg.getheight(this)/2 );
setsize( logoimg.getwidth(this) , logoimg.getheight(this) ); // 设置窗口大小
}
5.编写设置显示信息的方法,代码如下:
public void setstatus( string status ){
statusstr=status;
paint( getgraphics() ); // 重画窗口来更新信息窗口中的显示信息
}
6.重置paint()方法来绘制图片和显示信息的方法,代码如下:
public void paint(graphics g) {
/**@todo override this java.awt.component method*/
super.paint(g);
//绘制图片
if ( logoimg!=null )
java多窗口代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java多窗口切换、java多窗口代码的信息别忘了在本站进行查找喔。





