【问题标题】:Window of JWindow doesn't moveJWindow的窗口不动
【发布时间】:2012-06-16 16:54:37
【问题描述】:

我有一个从JWindow 延伸的框架(因为我想处理我的 X 和 - 按钮)所以我不希望它被装饰。我的问题是当我运行应用程序时,我无法拖动我的窗口 - 它固定在某个位置。我的代码如下(我的类很大所以我选择了相关部分):

公共类 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;

}

【问题讨论】:

  • 我在您的代码中看不到 JWindow。你介意发SSCCE吗?
  • thnx asif...实际上我做这件事但有时我得到不合适的答案..但无论如何thnx..

标签: java swing jwindow


【解决方案1】:

您可以使用例如以下代码添加自定义拖动:

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);
    }
});

如果你把这个sn-p直接放在StatisticsMainFrame的构造函数中,你可以用鼠标在窗口内任意位置点击拖动窗口。 (您也可以将侦听器添加到任何其他组件。)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-02
    • 2014-08-12
    • 1970-01-01
    • 1970-01-01
    • 2015-10-08
    • 1970-01-01
    • 2013-10-27
    • 2020-12-03
    相关资源
    最近更新 更多