使用取决于鼠标所在位置的按钮调整窗口大小

时间:2015-06-30 13:17:54

标签: java swing resize

我想知道当用户点击并在屏幕上拖动按钮时,我将如何调整窗口大小。我环顾四周,找不到任何答案。

我的窗口代码:

package Dashboard;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JTextArea;
import javax.swing.SpringLayout;

public class DashboardWindow extends JFrame {

    JMenuBar menuBar;
    JMenuItem menuItem;
    static JMenu menu;
    static JPanel Desc = new JPanel();
    static JMenu dm = new JMenu("Dashboard");
    static JLabel slabel = new JLabel();
    static JPanel status = new JPanel();
    static JPanel frame = new JPanel();
    static JPanel games = new JPanel();
    static JPanel apps = new JPanel();
    static JMenuItem Exit = new JMenuItem("Exit");
    static JMenuBar menubar = new JMenuBar();
    static JTextArea descText = new JTextArea();
    static JButton run = new JButton("run");

    public static JProgressBar pbar;
    /**
     *
     */
    private static final long serialVersionUID = 1L;

    public DashboardWindow(String title, int operations) {
        super(title);

        pbar = new JProgressBar(0, operations);

        Color gray = new Color(41, 39, 39);
        Color white = new Color(199, 197, 197);
        Color ngray = new Color(32, 32, 32);

        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int width = (int) screenSize.getWidth();
        int height = (int) screenSize.getHeight();
        System.out.println("screen width : " + width + ", screen height : " + height);

        SpringLayout springLayout = new SpringLayout();
        Desc.setLayout(springLayout);

        menubar.setBackground(gray);
        menubar.setForeground(gray);
        menubar.setBorderPainted(false);
        menubar.setBorder(BorderFactory.createEmptyBorder());
        pbar.setBackground(gray);

        slabel.setText("  :   No Tasks Right Now");
        slabel.setSize(278, 15);
        slabel.setBackground(gray);
        slabel.setLocation(0, 253);
        slabel.setForeground(white);

        dm.setBackground(gray);
        dm.setForeground(white);
        dm.setBorderPainted(false);
        dm.setBorder(BorderFactory.createEmptyBorder());

        Exit.setBackground(gray);
        Exit.setForeground(white);
        Exit.setBorderPainted(false);
        Exit.setBorder(BorderFactory.createEmptyBorder());

        Exit.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });

        dm.add(Exit);

        menubar.add(dm);

        setJMenuBar(menubar);

        Dimension prefSize = pbar.getPreferredSize();
        prefSize.height = 10;
        pbar.setPreferredSize(prefSize);

        frame.setBackground(gray);
        this.add(frame);
        status.setBackground(gray);
        status.setBorder(BorderFactory.createDashedBorder(null));
        status.setPreferredSize(new Dimension(200, 80));
        status.add(pbar);
        status.add(slabel, BorderLayout.PAGE_END);
        add(status, BorderLayout.PAGE_END);

        games.setBackground(white);
        games.setBorder(BorderFactory.createEmptyBorder());
        games.setPreferredSize(new Dimension((int) (width / 6.5), (int) (height / 9.5)));

        run.setBorderPainted(false);
        run.setBackground(gray);
        run.setFont(new Font("Arial", Font.PLAIN, 18));
        run.setForeground(white);
        //run.setAlignmentX(Component.CENTER_ALIGNMENT);
        run.setVisible(false);

        springLayout.putConstraint(SpringLayout.WEST, run, (int) (width / 3), SpringLayout.WEST, this);
        springLayout.putConstraint(SpringLayout.NORTH, run, (int) (height / 15), SpringLayout.NORTH, this);
        Desc.add(run);

        apps.setLayout(new BoxLayout(apps, BoxLayout.Y_AXIS));
        apps.setBackground(ngray);
        apps.setBorder(BorderFactory.createEtchedBorder());
        apps.setPreferredSize(new Dimension(300, 0));

        Desc.setBackground(ngray);
        Desc.setBorder(BorderFactory.createEtchedBorder());
        Desc.setPreferredSize(new Dimension((int) (width / 1.65), 0));

        //apps.setAlignmentX(CENTER_ALIGNMENT);
        //descText.setAlignmentX(Component.CENTER_ALIGNMENT);
        descText.setBackground(ngray);
        springLayout.putConstraint(SpringLayout.WEST, descText, (int) (width / 20), SpringLayout.WEST, this);
        springLayout.putConstraint(SpringLayout.NORTH, descText, (int) (height / 20), SpringLayout.NORTH, this);
        Desc.add(descText);

        add(Desc, BorderLayout.LINE_END);
        add(apps, BorderLayout.LINE_START);
        add(games, BorderLayout.CENTER);

        this.setUndecorated(true);
        this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        this.setSize(new Dimension((int) (width / 1.35), (int) (height / 1.3)));
        this.setLocationRelativeTo(null);
        this.setVisible(true);
        Dashboard.initializerTasks();
    }

    public static void PBarValue(int Value) {
        pbar.setValue(Value);
    }

    public static void ChangeText(String text) {
        slabel.setText(text);
    }

    public static void PWaitTrue() {
        pbar.setIndeterminate(true);
    }

    public static void PWaitFalse() {
        pbar.setIndeterminate(false);
    }

    public static void PSetMax(int max) {
        pbar.setMaximum(max);
    }

}

感谢任何帮助:D

1 个答案:

答案 0 :(得分:0)

以下示例可能会对您有所帮助。

如何使用:

拖动方法:按住按钮并将鼠标移动到屏幕中的某个位置并释放。窗口(jframe)将调整大小。此示例仅处理窗口起始点(x,y)的右侧和底侧。您可以根据需要修改代码,以便在任何方向和方向上进行修改。调整大小。

Clcik方法:点击按钮并将鼠标移动到任何位置,然后重新点击即可发布。这样就有了#&39;调整大小

import java.awt.MouseInfo;
import javax.swing.JButton;
import javax.swing.JFrame;

public class WindowResize {
    boolean isMoving = false;
    JFrame objFrm = new JFrame("Resize Demo");
    JButton ctrlButton = new JButton("Press and hold");
    public void fnShow() {
        objFrm.setSize(500, 500);
        objFrm.add(ctrlButton, java.awt.BorderLayout.PAGE_END);
        ctrlButton.setLocation(9, 0);
        objFrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        objFrm.setVisible(true);
        ctrlButton.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                fnSetScreenSize(evt, true);
            }
        });
        ctrlButton.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                isMoving = !isMoving;
                if (isMoving) {
                    ctrlButton.setText("Resizing now");
                } else {
                    ctrlButton.setText("Window size just fine");
                }
            }
            @Override
            public void mouseExited(java.awt.event.MouseEvent evt) {
                fnSetScreenSize(evt, isMoving);
            }
        });
        ctrlButton.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            @Override
            public void mouseMoved(java.awt.event.MouseEvent evt) {
                fnSetScreenSize(evt, isMoving);
            }
        });
        objFrm.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            @Override
            public void mouseMoved(java.awt.event.MouseEvent evt) {
                fnSetScreenSize(evt, isMoving);
            }
        });
    }
    private void fnSetScreenSize(java.awt.event.MouseEvent evt, boolean _isMoving) {
        if (_isMoving) {
            int intWidth = MouseInfo.getPointerInfo().getLocation().x - objFrm.getLocationOnScreen().x + ((ctrlButton.getHeight() / 2) * 2);
            int intHeight = MouseInfo.getPointerInfo().getLocation().y - objFrm.getLocationOnScreen().y + ((ctrlButton.getHeight() / 2) * 2);
            objFrm.setSize(intWidth, intHeight);
        }
    }
    public static void main(String[] args) {
        new WindowResize().fnShow();
    }
}