図のような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); } } }
}
あなたの回答
tips
プレビュー