質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Swing

SwingはJavaに標準で付属するグラフィック関連のクラスライブラリを指します。

Q&A

0回答

1537閲覧

SwingのmouseDraggedについて

Launcher

総合スコア19

Swing

SwingはJavaに標準で付属するグラフィック関連のクラスライブラリを指します。

0グッド

0クリップ

投稿2017/01/20 02:43

図のようなlauncherを制作しています。

フレームの上にパネルをのせ、そのうえにボタンを置いています。

ソースは以下の通りです
public class JFrameSample2 extends JFrame implements MouseListener, MouseMotionListener{
private final Point startPt = new Point();

private JPanel panel; //private ArrayList<RoundButton> ButtonList; private RoundButton[] button; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { JFrameSample2 frame = new JFrameSample2(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public JFrameSample2() { //this.setContentPane(new JPanel()); this.setPreferredSize(new Dimension(400, 400)); this.setLocation(750, 150); //常に最前面に表示 this.setAlwaysOnTop(true); //背景を透明あるいは半透明にする場合、 //FrameはundecoratedにしないとIllegalComponentStateExceptionがスローされる this.setUndecorated(true); this.setBackground(new Color(50,50,50,50)); //ウィンドウ範囲を示す枠をつけておく this.getRootPane().setBorder(new LineBorder(new Color(50,50,50,50))); this.pack(); //this.setVisible(true); //パネルの作成 panel = new JPanel(); //パネル背景の透明化 panel.setBackground(new Color(50,50,50,50)); //layoutの無効 panel.setLayout(null); //フレームにパネルを乗っける this.getContentPane().add(panel); //CreateButtonオブジェクトの生成 CreateButton crtButton = new CreateButton(); button = crtButton.createButton(8); for(int i=0;i<button.length;i++){ button[i].addActionListener(new ClickListener()); panel.add(button[i]); }

//ここがわかりません。
//ここでボタンをさくせいしています。そのボタンを押したときに
//フレームの場所の移動ができるようにしたいです。
JButton moveButton = new JButton("Click Me!");
moveButton.addMouseListener(this);
moveButton.addMouseMotionListener(this);
moveButton.setLocation(200,200);
addMouseListener(this);
addMouseMotionListener(this);
panel.add(moveButton);

// パネルがキーボードを受け付けるようにする(必須) setFocusable(true); addKeyListener(new ClosekeyListener()); requestFocus(); } public void mousePressed(MouseEvent me) { int x = me.getX(); int y = me.getY(); System.out.println(x); System.out.println(y); startPt.setLocation(me.getPoint()); } public void mouseDragged(MouseEvent me) { System.out.println("ここに入った"); Point eventLocationOnScreen = me.getLocationOnScreen(); setLocation(eventLocationOnScreen.x - startPt.x, eventLocationOnScreen.y - startPt.y); } @Override public void mouseMoved(MouseEvent e) { // TODO 自動生成されたメソッド・スタブ } @Override public void mouseClicked(MouseEvent e) { // TODO 自動生成されたメソッド・スタブ } @Override public void mouseEntered(MouseEvent e) { // TODO 自動生成されたメソッド・スタブ } @Override public void mouseExited(MouseEvent e) { // TODO 自動生成されたメソッド・スタブ } @Override public void mouseReleased(MouseEvent e) { // TODO 自動生成されたメソッド・スタブ } //ボタンクリック時の操作 class ClickListener implements ActionListener{ public void actionPerformed(ActionEvent e){ File file = null; //ファイルのパスを入れる変数 if(e.getSource() == button[0]){ //押されたボタンがbutton1だった時の処理 file = new File("c:"); }else if(e.getSource() == button[1]){ //押されたボタンがbutton4だった時の処理 file = new File("C:/Program Files (x86)/Google/Chrome/Application/chrome.exe"); }else if(e.getSource() == button[2]){ //押されたボタンがbutton2だった時の処理 file = new File("C:/Program Files (x86)/Google/Chrome/Application/chrome.exe"); }else if(e.getSource() == button[3]){ //押されたボタンがbutton3だった時の処理 file = new File("C:/Program Files (x86)/Google/Chrome/Application/chrome.exe"); }else if(e.getSource() == button[4]){ //押されたボタンがbutton4だった時の処理 file = new File("C:/Program Files (x86)/Google/Chrome/Application/chrome.exe"); }else if(e.getSource() == button[5]){ //押されたボタンがbutton4だった時の処理 file = new File("C:/Program Files (x86)/Google/Chrome/Application/chrome.exe"); }else if(e.getSource() == button[6]){ //押されたボタンがbutton4だった時の処理 file = new File("C:/Program Files (x86)/Google/Chrome/Application/chrome.exe"); }else if(e.getSource() == button[7]){ //押されたボタンがbutton4だった時の処理 file = new File("C:/Program Files (x86)/Google/Chrome/Application/chrome.exe"); }else if(e.getSource() == button[8]){ //押されたボタンがbutton4だった時の処理 file = new File("C:/Program Files (x86)/Google/Chrome/Application/chrome.exe"); } Desktop desktop = Desktop.getDesktop(); try { desktop.open(file); } catch (IOException e1) { // TODO 自動生成された catch ブロック e1.printStackTrace(); } } } class ClosekeyListener extends KeyAdapter { @Override public void keyPressed(KeyEvent e) { int keycode = e.getKeyCode(); if (keycode == KeyEvent.VK_ALT){ System.exit(0); } } }

}

なぜかボタンが表示されません。
(200,200)がフレームの真ん中の座標になります。
ボタンを表示させたいです。
イメージ説明

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問