java
1package picross; 2 3import static picross.Commonconst.HI; 4import static picross.Commonconst.WI; 5 6import java.awt.BorderLayout; 7 8import javax.swing.JButton; 9import javax.swing.JFrame; 10 11public class Picross extends JFrame { 12 public Picross() { 13 setTitle("ピクロス"); 14 setDefaultCloseOperation(EXIT_ON_CLOSE); 15 Board board = new Board(); 16 board.add(btn); 17 getContentPane().add(board); 18 pack(); 19 } 20 21 public static void main(String[] args) { 22 // TODO 自動生成されたメソッド・スタブ 23 Picross picross = new Picross(); 24 picross.setVisible(true); 25 } 26 27} 28
java
1package picross; 2 3import java.awt.Color; 4import java.awt.Dimension; 5import java.awt.Graphics; 6import java.awt.Panel; 7import java.awt.event.MouseEvent; 8import java.awt.event.MouseListener; 9 10import javax.swing.JButton; 11import javax.swing.JPanel; 12import static picross.Commonconst.*; 13 14public class Board extends JPanel implements ActionListener, MouseListener { 15 int[][] boardarray = new int[MASS][MASS]; 16 JButton btn = new JButton("CSV出力"); 17 18 public Board() { 19 // TODO 自動生成されたコンストラクター・スタブ 20 setPreferredSize(new Dimension(WI+1000, HI+1000)); 21 addMouseListener(this); 22 btn.setBounds(WI, HI, 100, 100); 23 this.add(btn); 24 btn.addActionListener(this); 25 26 27 boardarray[5][5] = 1; 28 } 29 30 public void paint(Graphics graphi) { 31 // graphi.setColor(Color.GRAY); 32 // graphi.fillRect(0, 0, HI, WI); 33 34 for (int x = 0; x < MASS; x++) { 35 for (int y = 0; y < MASS; y++) { 36 if (boardarray[x][y] == 1) { 37 graphi.setColor(Color.BLACK); 38 graphi.fillRect(x * SIZE, y * SIZE, SIZE, SIZE); 39 } else if (boardarray[x][y] == 0) { 40 graphi.setColor(Color.GRAY); 41 graphi.fillRect(x * SIZE, y * SIZE, SIZE, SIZE); 42 } 43 graphi.setColor(Color.ORANGE); 44 for (int i = 0; i < MASS; i++) { 45 graphi.drawLine(0, SIZE * i, WI, i * SIZE); 46 graphi.drawLine(i * SIZE, 0, i * SIZE, HI); 47 } 48 } 49 50 } 51 } 52 53 @Override 54 public void mouseClicked(MouseEvent e) { 55 // TODO 自動生成されたメソッド・スタブ 56 57 } 58 59 @Override 60 public void mousePressed(MouseEvent e) { 61 // TODO 自動生成されたメソッド・スタブ 62 int x = e.getX(); 63 int y = e.getY(); 64 65 x /= SIZE; 66 y /= SIZE; 67 68 if (boardarray[x][y] == 0) { 69 boardarray[x][y] = 1; 70 } else if (boardarray[x][y] == 1) { 71 boardarray[x][y] = 0; 72 } 73 repaint(); 74 System.out.println(x + "," + y); 75 } 76 77 @Override 78 public void mouseReleased(MouseEvent e) { 79 // TODO 自動生成されたメソッド・スタブ 80 81 } 82 83 @Override 84 public void mouseEntered(MouseEvent e) { 85 // TODO 自動生成されたメソッド・スタブ 86 87 } 88 89 @Override 90 public void mouseExited(MouseEvent e) { 91 // TODO 自動生成されたメソッド・スタブ 92 93 } 94 95} 96
上記のコードでフォーム上にボタンを出現させたいのですが、初めて扱うためよくわからず表示させることができません。
どのようにしたら表示可能でしょうか?
できればboardクラスにbuttonイベントが入ってくれると分かりやすくていいのですが可能なのでしょうか?
補足・・・・
ActionListenerとMouseListenerを同時使用ができることはわかったのでそこは解決しましたが、ボタンの表示ができません。色々試しているのですがmainのあるクラスでインターネットのサンプルを真似したらもちろんできるのですが、別クラスで既にPanelを宣言しているので新たにPanelを使用すると消えてしまいます。なので既存のPanelを使用するのだとは思うのですが記述方法がわからないため表示させることができません。どなたかわかる方お願いします。
下記参考にしたHP
http://www.javadrive.jp/tutorial/nulllayout/index1.html
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/04/06 14:13