对于让JFame窗口居中,我们可以自己写方法来处理,为了方便,我把代码封装在了一个方法里:

有个想不通的问题,请大神帮我一下~java返回值的问题 50C

整体代码如下:
父类是这个样子的
package login;

    public void moveToCenter(JFrame frame) {
        //先获得屏幕大小
        Dimension screenSize =
Toolkit.getDefaultToolkit().getScreenSize();
        double width =
screenSize.getWidth();//屏幕宽
        double height =
screenSize.getHeight();//屏幕高
        //设置frame居中
        frame.setLocation((int) (width – frame.getWidth()) / 2, (int) (height – frame.getHeight()) / 2);
    }

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import java.security.AccessController;
import java.security.PrivilegedAction;

 

import javax.swing.ImageIcon;
import javax.swing.JButton;
永利皇宫手机版下载 ,import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRootPane;
import javax.swing.JTextField;

       我以前写窗口程序就是这么做的,不过现在好了,Window类中有一个方法:

public class loginFrame
{ Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
int width = screensize.getWidth();
int height = screensize.getHeight();
JFrame loginin1 = new JFrame(); //窗体框架
JPanel login1 = new JPanel();
//窗体主面板
public JTextField user = new JTextField();
public JPasswordField password = new JPasswordField();

public void setLocationRelativeTo(Component c)

protected String user1 ="空值";protected String password1="空值";protected String verifycode1="空值";protected String character1="空值";JLabel l1 = new JLabel;JLabel l2 = new JLabel;JLabel l3 = new JLabel;JLabel l4 = new JLabel;public String code2;public JComboBox cb1 = new JComboBox();public JTextField verify = new JTextField();ImageIcon bg = new ImageIcon("src\\image\\loginbj.jpg");ImageIcon bg1 = new ImageIcon("src\\image\\robot.jpg");ImageIcon bg2 = new ImageIcon("src\\image\\dian.jpg");JLabel background = new JLabel;//底色背景图片JLabel background1 = new JLabel;//机器人背景图片JLabel background2 = new JLabel;//让密码明文显示的图片public JTextField passwordclear = new JTextField();JButton bt1 =new JButton;protected void cleartextpre() //这个是鼠标按压后显示明文的方法{ Point mousepoint = MouseInfo.getPointerInfo().getLocation(); if(mousepoint.x>=1163&&mousepoint.x<=1174&&mousepoint.y>=420 &&mousepoint.y<=430) { System.out.println("成功点击密码明文显示!"); char[] passwordclearword = password.getPassword(); String clearword = new String(passwordclearword); password.setVisible; passwordclear.setVisible; passwordclear.setText(clearword); passwordclear.setBounds(335, 99, 110, 25); login1.add(passwordclear); loginin1.repaint(); loginin1.setVisible; };}protected void cleartext() //鼠标松开后还原密文{ passwordclear.setVisible; password.setVisible; loginin1.repaint(); loginin1.setVisible;}public loginFrame() { // TODO Auto-generated method stub VerifyCodeUtils code1 = new VerifyCodeUtils(); JLabel pic = null; try { code2 = code1.run1(); //验证码的String pic= new JLabel(new ImageIcon("src\\verifycode\\"+code2+".jpg")); pic.setBounds(455, 180, 110, 60); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.print("加载验证码出错"); }//验证码的生成,四位验证码 ImageIcon icon = new ImageIcon("src\\image\\loginbutton.jpg"); bt1.setIcon; bt1.setBounds(335, 240, 110, 27); login1.add; bt1.addActionListener; background.setBounds(0,-25,600,300); loginin1.getLayeredPane().add(background,new Integer(Integer.MIN_VALUE)); //最上层面板透明 JPanel jp=loginin1.getContentPane(); jp.setOpaque; //设置透明 background1.setBounds(30,30,200,200); login1.add(background1); login1.add; login1.setSize; login1.setLocation; login1.setOpaque; login1.setLayout; loginin1.add; l1.setBounds(270, 40, 200, 50); l1.setFont(new Font("楷体",Font.BOLD, 20)); l2.setBounds(270, 90, 200, 50); l2.setFont(new Font("楷体",Font.BOLD, 20)); l3.setBounds(270, 140, 200, 50); l3.setFont(new Font("楷体",Font.BOLD, 20)); l4.setBounds(270, 190, 200, 50); l4.setFont(new Font("楷体",Font.BOLD, 20)); user.setBounds(335, 49, 110, 25); user.setBackground(new Color(204,249,236)); user.setForeground(Color.RED); password.setBounds(335, 99, 110, 25); password.setBackground(new Color(204,249,236)); verify.setBounds(335, 199, 110, 25); verify.setBackground(new Color(204,249,236)); background2.setBounds(450, 105, 10, 10);//让密码明文显示的小点 background2.addMouseListener; cb1.addItem; cb1.setSelectedItem; cb1.setBounds(335, 149, 110, 25); login1.add; login1.add; login1.add(background2); login1.add; login1.add; login1.add; login1.add; login1.add; login1.add; loginin1.setBounds(width/2-250,height/2-250,600,300); loginin1.setTitle("无人仓储系统登录"); loginin1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); loginin1.setResizable; loginin1.setIconImage((new ImageIcon("src\\image\\warehouse.png").getImage; loginin1.setVisible;}MouseListener m1 = new MouseListener(){ @Override public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub cleartextpre(); } @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub cleartext(); } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub }}; ActionListener al1 = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub user1=user.getText(); character1=cb1.getSelectedItem().toString(); password1=new String(password.getPassword; verifycode1 = verify.getText(); loginproof lp = new loginproof(); lp.runloginproof(); loginin1.setVisible; } }; public String getcharacter() { return character1; } public String getUser() { if(user.getText()==null&&user.getText()==""&& user.getText().length()==0 &&user1.equals&&user==null) {return "bingo 终于出错了";} else { return user1=user.getText(); } } public String getPassword() { return password1; } public String getVerifycode() { return verifycode1; }

     
 该方法可以把窗体放在组件c的中间位置,如果组件c为null,则把窗体放在屏幕中央。

}

下面是一个测试用例:

子类是这个样子的
package login;

package javatest;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.plaf.ProgressBarUI;

网站地图xml地图