פעולות על JBUTTON בJAVA - תכנות - HWzone פורומים
עבור לתוכן
  • צור חשבון

פעולות על JBUTTON בJAVA


SweeT_EviL

Recommended Posts

יש לך את המחלקה MouseListener שהיא יותר משוכללת מ Action Listener

public class Mouse implements MouseListener
{
public void mousePressed(MouseEvent e)
{

}
public void mouseReleased(MouseEvent e)
{

}
public void mouseClicked(MouseEvent e)
{

}
public void mouseEntered (MouseEvent e)
{

}
public void mouseExited (MouseEvent e)
{

}

קישור לתוכן
שתף באתרים אחרים

package game;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import java.util.StringTokenizer;

import javax.swing.*;


public class DrawingColor extends JPanel implements MouseListener, ActionListener {
public final static int BOARD_W=20,BOARD_H=20;
public final static int LABEL_W=25,LABEL_H=25;
private static Board board;
private static JButton[][] lbl=null;
private static JPanel panBoard=null;

public DrawingColor(){
lbl=new JButton[BOARD_W][BOARD_H];
panBoard = new JPanel();
panBoard.setLayout(new GridLayout(BOARD_W, BOARD_H));
panBoard.setBorder(BorderFactory.createLineBorder(Color.BLUE, 1));

JPanel panStock = new JPanel();
panStock.setLayout(new GridLayout(BOARD_W, BOARD_H));
panStock.setBorder(BorderFactory.createLineBorder(Color.RED, 1));



for(int row=BOARD_W-1; row>=0; --row)
{
for(int col=0; col<BOARD_H; col++) {
lbl[row][col]=new JButton();
lbl[row][col].setPreferredSize(new Dimension(LABEL_W,LABEL_H));
lbl[row][col].setOpaque(true);
lbl[row][col].setForeground(Color.RED);
lbl[row][col].setBorder(BorderFactory.createLineBorder(Color.BLUE, 1));

if(board.getValue(row+1,col+1) == 1)
lbl[row][col].setBackground(Color.RED);
if(board.getValue(row+1,col+1) == 2)
lbl[row][col].setBackground(Color.GREEN);
if(board.getValue(row+1,col+1) == 3)
lbl[row][col].setBackground(Color.YELLOW);
if(board.getValue(row+1,col+1) == 4)
lbl[row][col].setBackground(Color.BLUE);

lbl[row][col].addActionListener(this);
lbl[row][col].setActionCommand(row+","+col);
panBoard.add(lbl[row][col], row,col);
}
}
add(panBoard);
}

public static void updateBoard(Board b) {
board=b;
for(int row=0; row<BOARD_W; row++)
{
for(int col=0; col<BOARD_H; col++) {
if(board.getValue(row+1,col+1) == 1)
lbl[row][col].setBackground(Color.RED);
if(board.getValue(row+1,col+1) == 2)
lbl[row][col].setBackground(Color.GREEN);
if(board.getValue(row+1,col+1) == 3)
lbl[row][col].setBackground(Color.YELLOW);
if(board.getValue(row+1,col+1) == 4)
lbl[row][col].setBackground(Color.BLUE);
}
}
}

public void mouseClicked(MouseEvent e) {
int row=e.getY()/LABEL_H;
int col=e.getX()/LABEL_W;

JOptionPane.showMessageDialog(null,"Row=" + row + " Col="+col);
System.out.print("Row=" + row + " Col="+col);
}

public void mousePressed(MouseEvent e) {
int row=e.getY()/LABEL_H;
int col=e.getX()/LABEL_W;

JOptionPane.showMessageDialog(null,"Row=" + row + " Col="+col);
System.out.print("Row=" + row + " Col="+col);
}

public void mouseReleased(MouseEvent e) {
int row=e.getY()/LABEL_H;
int col=e.getX()/LABEL_W;

JOptionPane.showMessageDialog(null,"Row=" + row + " Col="+col);
System.out.print("Row=" + row + " Col="+col);
}

public void mouseEntered(MouseEvent e) {
int row=e.getY()/LABEL_H;
int col=e.getX()/LABEL_W;

JOptionPane.showMessageDialog(null,"Row=" + row + " Col="+col);
System.out.print("Row=" + row + " Col="+col);
}

public void mouseExited(MouseEvent e) {
int row=e.getY()/LABEL_H;
int col=e.getX()/LABEL_W;

JOptionPane.showMessageDialog(null,"Row=" + row + " Col="+col);
System.out.print("Row=" + row + " Col="+col);
}

public void actionPerformed(ActionEvent e){
StringTokenizer st=new StringTokenizer(e.getActionCommand(),",");
String[] s=new String[2];

s[0]=st.nextToken();
s[1]=st.nextToken();

int row=Integer.parseInt(s[0])+1;
int col=Integer.parseInt(s[1])+1;

JOptionPane.showMessageDialog(null,"Row=" + row + " Col="+col);
System.out.print("Row=" + row + " Col="+col);
}

class BlokusLabel extends JLabel {
public void paint(Graphics g) {
g.setColor(Color.BLUE);
g.fillRect(0,0,getWidth(), getHeight());
}
}

public static void main(Board b) {
board=b;
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}

private static void createAndShowGUI() {
JFrame frame = new JFrame("1");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JComponent newContentPane = new DrawingColor();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);

//Display the window.
frame.setSize(600,600);
frame.pack();
frame.setVisible(true);

}
}

קישור לתוכן
שתף באתרים אחרים

ארכיון

דיון זה הועבר לארכיון ולא ניתן להוסיף בו תגובות חדשות.

×
  • צור חדש...