Java graphical interface Computer Manager interface

After learning JavaSwing building program interface, small partners biggest confusion might be "Why do we do it so ugly interface, not the same as market popular program interface it?"

 

 

 

Like this interface, we found that the title bar with the default state is not the same, the Close button style has changed. The method of implementation is very simple to use setUndecorated (true); cancel decorative form, other effects can be completed through the pictures. The following code implements a form without a title bar, using technical drawing background set background image as being part of the panel; achieved through the close button of the mouse and mouse-click events; drag the mouse to achieve a new title bar to move the entire form of the function. Other features and special effects you can achieve on their own.

Constants.java

/**

 * @Description: to save constants

 * @Author: old nine school-pit head  

 * @Date: 2017 Nian 12 Yue 25 Ri 2:47:31 PM  

 * @version V1.0

 * @Copyright: 2017 http://www.xuetang9.com Inc. All rights reserved.

 */

public class Constants {

    / ** * Global Font Name /

    public static String SysFontName = "宋体";

    / ** Login Form W * /

    public static int Width_LoginFrame = 387;

    / ** * Login Form High /

    public static int Height_LoginFrame = 266;

}

LoginFrame.java

/**

 * @Description: login screen

 * @Author: old nine school-pit head  

 * @Date: 2017 Nian 12 Yue 25 Ri 2:40:07 PM  

 * @version V1.0

 * @Copyright: 2017 http://www.xuetang9.com Inc. All rights reserved.

 */

public class LoginFrame extends JFrame{

    private JPanel pnlTop = new TopPanel("images/sknin1.jpg");

    private JPanel pnlMiddle = new JPanel();

    private JPanel pnlBottom = new JPanel();

    private JPanel contentPane = null;

    private BorderLayout contentPaneLayout = null; // Border layout of content panel

    private Point mousePressedPoint; record when // click the mouse coordinates pnlTop panel

    public LoginFrame(){

        setDefaultCloseOperation (JFrame.DO_NOTHING_ON_CLOSE); // close the form when doing nothing

        setTitle ( "Login Computer Manager"); // set form title

        setSize (Constants.Width_LoginFrame, Constants.Height_LoginFrame); // form size here can refer to the size of the picture material (interface material to be copied into the images folder)

        initComponents (); // call to initialization method on the custom form components

        setLocationRelativeTo (null); // set the form centered

        setUndecorated (true); // set true form undecorated

    }

    private void initComponents() {

        contentPane = new JPanel();

        contentPaneLayout = new BorderLayout();

        contentPane.setLayout(contentPaneLayout);

        / ********************** start of setting pnlTop related controls ********************* **** /

        JLabel lblTitle = new JLabel ( "Login Computer Manager");

        lblTitle.setFont(new Font(Constants.SysFontName, Font.PLAIN, 14));

        lblTitle.setForeground(Color.WHITE);

        // grid bag layout

        GridBagLayout pnlTopLayout = new GridBagLayout();

        pnlTop.setLayout (pnlTopLayout);

        pnlTop.add(lblTitle);

        JLabel lblClose = new JLabel();

        lblClose.setIcon(new ImageIcon("images/close.png"));

        pnlTop.add(lblClose);      

        // Set a regular grid bag layout

        GridBagConstraints grConstraints = new GridBagConstraints();

        grConstraints.insets = new Insets (0, 0, 0,245); // Set the four-directional margin   

        pnlTopLayout.setConstraints (lblTitle, grConstraints); // set new rules for the control

        // set the control over the layout width and height

        pnlTop.setPreferredSize(new Dimension(Constants.Width_LoginFrame, 30));

        contentPane.add(pnlTop, BorderLayout.NORTH);

        lblClose.addMouseListener (new MouseAdapter () {// Close button image replacement

            ImageIcon icon = new ImageIcon("images/close.png");

            @Override

            public void mouseEntered(MouseEvent e) {

                lblClose.setIcon(null);

                lblClose.setForeground(Color.RED);

                lblClose.setText ( "X"); // No other picture material, the use of the letter X analog implementation transitions

                lblClose.setPreferredSize(new Dimension(icon.getIconWidth(), icon.getIconHeight()));

            }

            @Override

            public void mouseExited(MouseEvent e) {

                lblClose.setIcon(icon);

            }

            @Override

            public void mouseClicked(MouseEvent e) {

                int result = JOptionPane.showConfirmDialog (null, "confirmed to close it?", "window closed", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);

                if(result == 0) System.exit(0);

            }

        });

        pnlTop.addMouseListener(new MouseAdapter() {

            @Override

            public void mousePressed(MouseEvent e) {

                First record mouse clicks of the mouse coordinates when //

                mousePressedPoint = e.getPoint();

            }

        });

        pnlTop.addMouseMotionListener(new MouseAdapter() {         

            @Override

            public void mouseDragged(MouseEvent e) {

                // get the current coordinate form

                Point p = getLocation();

                // set the form coordinates: current coordinates of the current mouse coordinates + movement - mouse original coordinate == current coordinates + movement distance of the mouse

                setLocation ((int) (p.getX () + e.getX () - mousePressedPoint.getX ()), (int) (p.getY () + e.getY () - mousePressedPoint.getY ()));             

            }

        });

        / ********************** end of set pnlTop related controls ********************* **** /

        this.setContentPane(contentPane);

    }

 

 

    class TopPanel extends JPanel {// override the upper panel (the background image to achieve the effect of stretching)

        private ImageIcon background;

        public TopPanel(String backImagePath) {

            if(null == backImagePath) return;

            background = new ImageIcon(backImagePath);

        }

        @Override

        protected void paintComponent (Graphics g) {// redraw assembly

            if(background == null) return;

            // drawing pictures

            g.drawImage(background.getImage(), 0, 0, Constants.Width_LoginFrame, background.getIconHeight(), null);

        }

    }

    public static void main(String[] args) {

        new LoginFrame().setVisible(true);

    }

}

Dry notes more attention to micro-channel public number: the old nine school

Guess you like

Origin www.cnblogs.com/ljxt/p/11612598.html