简体   繁体   English

JWindow的窗口不移动

[英]Window of JWindow doesn't move

I have a frame extends from JWindow (because I want to handle my X and - buttons) so I don't want it decorated. 我有一个从JWindow扩展JWindow的框架(因为我想处理X和-按钮),所以我不想装饰它。 My problem is when I run the application I can't drag my window - it's fixed in a certain location. 我的问题是,当我运行应用程序时,我无法拖动窗口-它固定在某个位置。 My code is as follows (the class i big so i picked the related section): 我的代码如下(该类我很大,所以我选择了相关部分):

public class StatisticsMainFrame extends JWindow{ 公共类StatisticsMainFrame扩展JWindow {

public StatisticsMainFrame()
{
     bodyPane = new JPanel();
    bodyPane.setLayout(new BoxLayout(bodyPane, BoxLayout.X_AXIS));
    sideBannerPane = new JPanel(); // program banner
    buttonsPane = new JPanel(); // contains buttons
    contentPane = new JPanel();
    contentPane.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
    contentPane.setPreferredSize(new Dimension(500,500)); 
    // contentPane contains some panel for display data


    Container container = new Container();
    container.setLayout(new BoxLayout(container, BoxLayout.X_AXIS));
    container.add(Box.createVerticalGlue());
    container.add(sideBannerPane);
    container.add(buttonsPane);
    container.add(contentPane);

    Container cont = new Container();
    cont.setLayout(new BoxLayout(cont, BoxLayout.Y_AXIS));
    //cont.add(Box.createHorizontalGlue());
    //cont.add(custDecorationPane);
    cont.add(container);
    add(cont);
    // Adding Panes

    setAlwaysOnTop(true);
    //setUndecorated(true);
    pack();

}

public static void main(String[] arg)
{
    new StatisticsMainFrame().setVisible(true);
}


private CustomFrameDecorationButtons custDecorationPane;
private JPanel bodyPane;
private JPanel contentPane;
private JPanel sideBannerPane;
private JPanel buttonsPane;

// buttonsPane components
private JLabel general_lbl;
private JLabel Diagnosis_lbl;
private JLabel chemotherapy_lbl;
private JLabel radiotherapy_lbl;
private JLabel surgery_lbl;
private JLabel hermonalTherapy_lbl;

private RootButton generalStatistics_btn;
private RootButton statOfAffectedSys_btn;
private RootButton statOfGrade_btn;
private RootButton statOfCombinations_btn;
private RootButton statOfResponses_btn;
private RootButton statOfRadiotherapy_btn;
private RootButton statOfSurgery_btn;
private RootButton statOfHermonaltherapy_btn;

private GeneralStatisticsContentPanel generalStatPane;
private StatisticsOfAffectedSystemPanel2 statOfAffectedSysPanel2;
private StatisticsOfGradePanel statOFGradingPanel;
private ChemotherapyCombinationStatisticsPanel statChemotherapyCombinationPanel;
private ChemotherapyResponseStatisticsPanel statChemotherapyResponsePanel;
private RadiotherapyStatisticsPanel radiotherapyPanel;
private SurgeryStatisticsPanel surgeryStatPanel;
private HermonaltherapyStatisticsPanel hermonaltherapy;

} }

You can add a custom drag using for example the following code: 您可以使用以下代码添加自定义拖动:

addMouseMotionListener(new MouseMotionListener() {
    private int mx, my;

    @Override
    public void mouseMoved(MouseEvent e) {
        mx = e.getXOnScreen();
        my = e.getYOnScreen();
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        Point p = StatisticsMainFrame.this.getLocation();
        p.x += e.getXOnScreen() - mx;
        p.y += e.getYOnScreen() - my;
        mx = e.getXOnScreen();
        my = e.getYOnScreen();
        StatisticsMainFrame.this.setLocation(p);
    }
});

If you place this snippet directly in the constructor of StatisticsMainFrame you can use the mouse to drag the window by clicking anywhere inside the window. 如果将此代码段直接放置在StatisticsMainFrame的构造函数中,则可以通过单击窗口内的任意位置来使用鼠标拖动窗口。 (You can also add the listener to any other component.) (您也可以将侦听器添加到任何其他组件。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM