点解按钮时,其他一些按钮会闪动一下,但把按钮的
setBorder(BorderFactory.createRaisedBevelBorder());
把这句去掉却不会这样,到底是什么原因呢?怎么修改,希望高手能指点一下,谢谢!
package ccc;
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.text.*;
import java.awt.event.*;
public class test extends JFrame implements Runnable{
int main_x=15;
int main_y=12;
int toppanel_height=40;
int height=main_y*20+toppanel_height+68;
int width=main_x*20+15;
public static void main(String[] args){
EventQueue.invokeLater(new Runnable(){
public void run(){
try{
test frame=new test();
frame.setVisible(true);
new Thread(frame).start();
}catch(Exception e){
e.printStackTrace();
}
}
});
}
public test(){
super();
setBounds(0,0,width,height);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(new BorderLayout());
DrawPanel mainpanel=new DrawPanel();
getContentPane().add(mainpanel,BorderLayout.CENTER); }
class DrawPanel extends JPanel implements MouseListener,ActionListener{//
final JButton aaa[][]=new JButton[100][100];
public DrawPanel(){
super();
setLayout(null);
setBorder(BorderFactory.createLoweredBevelBorder());
for(int xx=0;xx<main_x;xx++){
for(int yy=0;yy<main_y;yy++){
aaa[xx][yy]=new JButton();
aaa[xx][yy].setBounds(xx*20+5,yy*20+5,20,20);
add(aaa[xx][yy]);
aaa[xx][yy].setBackground(Color.DARK_GRAY);
aaa[xx][yy].setBorder(BorderFactory.createRaisedBevelBorder());//把这句去掉却不会这样
aaa[xx][yy].addActionListener(this);
aaa[xx][yy].addMouseListener(this);
}
}
}
public void paint(Graphics g){
super.paint(g);
Graphics2D g2=(Graphics2D)g;
g2.setColor(Color.GRAY);
g2.setStroke(new BasicStroke(2));
for(int xx=0;xx<main_x;xx++){
for(int yy=0;yy<main_y;yy++){
g2.drawLine(xx*20+5,5,xx*20+5,width-10);
g2.drawLine(5,yy*20+5,height-10,yy*20+5);
}
}
}
public void actionPerformed(ActionEvent e){
remove((JButton)e.getSource());
this.repaint();
}
public void mouseClicked(MouseEvent e){
}
public void mouseEntered(MouseEvent e){
repaint();
}
public void mouseExited(MouseEvent e){
}
public void mousePressed(MouseEvent e){
}
public void mouseReleased(MouseEvent e){
}
}
public void run(){
}
}
setBorder(BorderFactory.createRaisedBevelBorder());
把这句去掉却不会这样,到底是什么原因呢?怎么修改,希望高手能指点一下,谢谢!
package ccc;
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.text.*;
import java.awt.event.*;
public class test extends JFrame implements Runnable{
int main_x=15;
int main_y=12;
int toppanel_height=40;
int height=main_y*20+toppanel_height+68;
int width=main_x*20+15;
public static void main(String[] args){
EventQueue.invokeLater(new Runnable(){
public void run(){
try{
test frame=new test();
frame.setVisible(true);
new Thread(frame).start();
}catch(Exception e){
e.printStackTrace();
}
}
});
}
public test(){
super();
setBounds(0,0,width,height);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(new BorderLayout());
DrawPanel mainpanel=new DrawPanel();
getContentPane().add(mainpanel,BorderLayout.CENTER); }
class DrawPanel extends JPanel implements MouseListener,ActionListener{//
final JButton aaa[][]=new JButton[100][100];
public DrawPanel(){
super();
setLayout(null);
setBorder(BorderFactory.createLoweredBevelBorder());
for(int xx=0;xx<main_x;xx++){
for(int yy=0;yy<main_y;yy++){
aaa[xx][yy]=new JButton();
aaa[xx][yy].setBounds(xx*20+5,yy*20+5,20,20);
add(aaa[xx][yy]);
aaa[xx][yy].setBackground(Color.DARK_GRAY);
aaa[xx][yy].setBorder(BorderFactory.createRaisedBevelBorder());//把这句去掉却不会这样
aaa[xx][yy].addActionListener(this);
aaa[xx][yy].addMouseListener(this);
}
}
}
public void paint(Graphics g){
super.paint(g);
Graphics2D g2=(Graphics2D)g;
g2.setColor(Color.GRAY);
g2.setStroke(new BasicStroke(2));
for(int xx=0;xx<main_x;xx++){
for(int yy=0;yy<main_y;yy++){
g2.drawLine(xx*20+5,5,xx*20+5,width-10);
g2.drawLine(5,yy*20+5,height-10,yy*20+5);
}
}
}
public void actionPerformed(ActionEvent e){
remove((JButton)e.getSource());
this.repaint();
}
public void mouseClicked(MouseEvent e){
}
public void mouseEntered(MouseEvent e){
repaint();
}
public void mouseExited(MouseEvent e){
}
public void mousePressed(MouseEvent e){
}
public void mouseReleased(MouseEvent e){
}
}
public void run(){
}
}
for (int xx = 0; xx < main_x; xx++) {
for (int yy = 0; yy < main_y; yy++) {
aaa[xx][yy] = new JButton();
aaa[xx][yy].setBounds(xx * 20 + 5, yy * 20 + 5, 20, 20);
add(aaa[xx][yy]);
aaa[xx][yy].setBackground(Color.DARK_GRAY);
aaa[xx][yy].setBorder(BorderFactory.createRaisedBevelBorder());// 把这句去掉却不会这样
aaa[xx][yy].addActionListener(this);
aaa[xx][yy].addMouseListener(this);
aaa[xx][yy].setFocusable(false); // 让按钮得不到焦点
}
}