java窗口相关代码 java 窗口
JAVA用frame实现图中2个窗口 怎么写啊?
图片看起来很模糊,隐约看到需要一个登录窗口,那就分享一下以前练习的登录窗口demo吧。
创新互联建站-专业网站定制、快速模板网站建设、高性价比恩平网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式恩平网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖恩平地区。费用合理售后完善,10年实体公司更值得信赖。
先上效果图:
登录界面
源码如下:
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;
}
}
java关闭当前窗口代码
方法一:
类 JFrame
javax.swing.JFrame
JFrame中的方法void setDefaultCloseOperation(int)可以设置
以下为改方法的用法:
setDefaultCloseOperation
public void setDefaultCloseOperation(int operation)设置用户在此窗体上发起
"close" 时默认执行的操作。必须指定以下选项之一:
DO_NOTHING_ON_CLOSE(在 WindowConstants 中定义):不执行任何操作;要求程序在已注册的
WindowListener 对象的 windowClosing 方法中处理该操作。
HIDE_ON_CLOSE(在 WindowConstants 中定义):调用任意已注册的 WindowListener
对象后自动隐藏该窗体。
DISPOSE_ON_CLOSE(在 WindowConstants 中定义):调用任意已注册 WindowListener
的对象后自动隐藏并释放该窗体。
EXIT_ON_CLOSE(在 JFrame 中定义):使用 System exit
方法退出应用程序。仅在应用程序中使用。
默认情况下,该值被设置为 HIDE_ON_CLOSE。更改此属性的值将导致激发属性更改事件,其属性名称为
"defaultCloseOperation"。
注:当 Java 虚拟机 (VM) 中最后一个可显示窗口被释放后,虚拟机可能会终止
要实现你说的,应该采用
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
方法二:
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class Test extends JFrame {
public Test(){
this.setTitle("title");
this.setSize(300,200);
this.setLocation(100,100);
//设置关闭时什么也不做
this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
//监听关闭按钮的点击操作
this.addWindowListener(new WindowAdapter(){
//new 一个WindowAdapter 类 重写windowClosing方法
//WindowAdapter是个适配器类 具体看jdk的帮助文档
public void windowClosing(WindowEvent e) {
//这里写对话框
if(JOptionPane.showConfirmDialog(null,
"退出","提
示",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE)==JOptionPane.YES_OPTION){
System.exit(0);
}
}
});
this.setVisible(true);
}
public static void main(String[] args) {
new Test();
}
}
java编写有窗口界面应用程序 求代码
package image;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class PanelRunner extends JFrame
{
private static final long serialVersionUID = 1L;
private static void initPanels ( JFrame pr )
{
for ( int i = 0; i 5; i++ )
{
JPanel panel = new JPanel ();
switch (i)
{
case 0:
panel.setBackground (Color.RED);
panel.setPreferredSize (new Dimension (200, 100));
pr.add (panel, BorderLayout.NORTH);
break;
case 1:
panel.setBackground (Color.YELLOW);
panel.setPreferredSize (new Dimension (200, 300));
pr.add (panel, BorderLayout.EAST);
break;
case 2:
panel.setBackground (Color.ORANGE);
panel.setPreferredSize (new Dimension (200, 100));
pr.add (panel, BorderLayout.SOUTH);
break;
case 3:
panel.setBackground (Color.WHITE);
panel.setPreferredSize (new Dimension (200, 300));
pr.add (panel, BorderLayout.WEST);
break;
case 4:
pr.add (panel, BorderLayout.CENTER);
panel.setPreferredSize (new Dimension (200, 100));
panel.setLayout (new GridLayout (1, 2));
for ( int j = 0; j 2; j++ )
{
JPanel subPanel = new JPanel ();
subPanel.setPreferredSize (new Dimension (200, 100));
Color color = j == 0 ? Color.BLUE : Color.GREEN;
subPanel.setBackground (color);
panel.add (subPanel);
}
break;
default:
break;
}
}
}
private static void initFrame ( JFrame pr )
{
pr.setLayout (new BorderLayout ());
pr.setSize (600, 300);
pr.setLocationRelativeTo (null);
pr.setResizable (false);
pr.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
}
public static void main ( String[] args )
{
SwingUtilities.invokeLater (new Runnable ()
{
@Override
public void run ()
{
PanelRunner pr = new PanelRunner ();
initFrame (pr);
initPanels (pr);
pr.setVisible (true);
}
});
}
}
分享文章:java窗口相关代码 java 窗口
文章位置:http://azwzsj.com/article/dohgpph.html