import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyButton{
public static void main(String[] args){
JFrame w = new JFrame();
w.setSize(300, 400);
MyPanel mp = new MyPanel();
w.add(mp);
w.addMouseListener(mp);
mp.addMouseListener(mp);
w.setVisible(true);
}
}
class MyPanel extends JPanel implements MouseListener{
boolean b = true;
public void paint(Graphics g){
super.paint(g);
if(b){
g.setColor(Color.WHITE);
g.drawLine(30, 30, 80, 30);
g.drawLine(30, 30, 30, 50);
g.setColor(Color.BLACK);
g.drawLine(30, 50, 80, 50);
g.drawLine(80, 30, 80, 50);
}else{
g.setColor(Color.BLACK);
g.drawLine(30, 30, 80, 30);
g.drawLine(30, 30, 30, 50);
g.setColor(Color.WHITE);
g.drawLine(30, 50, 80, 50);
g.drawLine(80, 30, 80, 50);
}
}
@Override
public void mouseClicked(MouseEvent arg0){
}
@Override
public void mouseEntered(MouseEvent arg0){
}
@Override
public void mouseExited(MouseEvent arg0){
}
@Override
public void mousePressed(MouseEvent arg0){
if(arg0.getX()>30 && arg0.getX()<80&&
arg0.getY()>30 && arg0.getY()<50)
{
b = false;
repaint();
}
}
@Override
public void mouseReleased(MouseEvent arg0){
b = true;
repaint();
}
}
import java.awt.*;
import javax.swing.*;
public class MyButton{
public static void main(String[] args){
JFrame w = new JFrame();
w.setSize(300, 400);
JButton b1 = new JButton("OK1");
JButton b2 = new JButton("OK2");
JButton b3 = new JButton("OK3");
JButton b4 = new JButton("OK4");
JButton b5 = new JButton("OK5");
//设置布局
w.setLayout(new BorderLayout());
w.add(b1, BorderLayout.NORTH);
w.add(b2, BorderLayout.SOUTH);
w.add(b3, BorderLayout.WEST);
w.add(b4, BorderLayout.EAST);
w.add(b5, BorderLayout.CENTER);
w.setVisible(true);
}
}
//设置布局
w.setLayout(new GridLayout(3, 2));
//设置布局
w.setLayout(new FlowLayout());