diff --git a/src/FirstGUI.java b/src/FirstGUI.java index 03cd114..4561dfe 100644 --- a/src/FirstGUI.java +++ b/src/FirstGUI.java @@ -6,17 +6,20 @@ import javax.swing.JFrame; import javax.swing.JOptionPane; -class MyJButton extends JButton implements ActionListener , Runnable { +class MyJButton extends JButton implements ActionListener, Runnable { Thread thread; - public MyJButton(String label){ + + public MyJButton(String label) { super(label); } - public Dimension getPreferredSize(){ + + public Dimension getPreferredSize() { return new Dimension(300, 100); } + @Override public void actionPerformed(ActionEvent e) { - if (thread == null){ + if (thread == null) { thread = new Thread(this); thread.start(); } else { @@ -25,12 +28,14 @@ FirstGUI.jf.pack(); System.out.println(e.getActionCommand()); } + int num = 0; + @Override public void run() { - while(thread != null){ + while (thread != null) { num++; - setText("Second: "+num); + setText("Second: " + num); try { Thread.sleep(1000); } catch (InterruptedException e) { @@ -39,30 +44,42 @@ } } } + class MyThread extends Thread { } public class FirstGUI { static JFrame jf; - public static void main(String[] args){ + + public static void main(String[] args) { System.out.println("FirstGUI"); jf = new JFrame("FirstGUI"); MyJButton jb = new MyJButton("Label Label Label"); - jb.addActionListener(jb); - jb.addActionListener( e -> System.out.println("Button was pressed") ); + + // 古い書き方 jb.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - JOptionPane.showMessageDialog(jb,"Button was pressed."); + JOptionPane.showMessageDialog(jb, "Button was pressed."); } }); + jb.addActionListener(jb); + + // ラムダ式 (Java 8以降) 引数の型は自動的に推論される + jb.addActionListener(e -> System.out.println(e.getActionCommand() + " was pressed")); + + // メソッド参照(ラムダ式をさらに簡潔にしたもの) + // 引数が1つで、その引数をそのままメソッドに渡す場合に使える(今回はActionEventをprintlnに送ってしまうので、すこし意味が異なる) + jb.addActionListener(System.out::println); + jf.getContentPane().add(jb); -// jf.setSize(300,200); + // jf.setSize(300,200); jf.pack(); jf.setVisible(true); - jf.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); + jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + // jf.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); } } diff --git a/src/mypackage/TransparentWindowTest.java b/src/mypackage/TransparentWindowTest.java new file mode 100644 index 0000000..38867e6 --- /dev/null +++ b/src/mypackage/TransparentWindowTest.java @@ -0,0 +1,91 @@ +package mypackage; + +import java.awt.Color; +import java.awt.Dimension; +import java.awt.Graphics; +import java.awt.Image; +import java.awt.Point; +import java.awt.event.MouseEvent; +import java.awt.event.MouseListener; +import java.awt.event.MouseMotionListener; + +import javax.swing.JWindow; + +public class TransparentWindowTest extends JWindow implements MouseListener, MouseMotionListener { + Image buf; + Graphics gra; + public TransparentWindowTest(){ + addMouseListener(this); + addMouseMotionListener(this); + } + // public void init(){ + // } + public Dimension getPreferredSize(){ + return new Dimension(300,200); + } + public void paint(Graphics g){ + if (buf == null) { + Dimension d = getSize(); + buf = createImage(d.width, d.height); + } + Dimension r = getSize(); + gra = buf.getGraphics(); + gra.setColor(new Color(0,0,0,0)); + gra.fillRect(0,0,r.width,r.height); + // super.paint(g); + gra.setColor(Color.black); + gra.drawString(getLocation().toString(), 100, 100); + gra.drawRect(0,0,r.width-1,r.height-1); + g.drawImage(buf,0,0,this); + } + @Override + public void mouseClicked(MouseEvent e) { + System.exit(0); + + } + Point pressP, pressLoc; + int diffx, diffy; + @Override + public void mousePressed(MouseEvent e) { + pressP = e.getLocationOnScreen(); + pressLoc = getLocationOnScreen(); + diffx = pressP.x - pressLoc.x; + diffy = pressP.y - pressLoc.y; + } + @Override + public void mouseReleased(MouseEvent e) { + // TODO Auto-generated method stub + + } + @Override + public void mouseEntered(MouseEvent e) { + // TODO Auto-generated method stub + + } + @Override + public void mouseExited(MouseEvent e) { + // TODO Auto-generated method stub + + } + @Override + public void mouseDragged(MouseEvent e) { + Point nowP = e.getLocationOnScreen(); + System.out.println(nowP); + setLocation(nowP.x - diffx, nowP.y - diffy); +repaint(); + + } + @Override + public void mouseMoved(MouseEvent e) { + // TODO Auto-generated method stub + + } + public static void main(String[] args) { + + TransparentWindowTest tw = new TransparentWindowTest(); + tw.setSize(300,200); + tw.setBackground(new Color(100,0,0,0)); + tw.setVisible(true); + // tw.init(); + } +}