Problemas com o JButton[RESOLVIDO]

2 respostas
DANIEL35
Olá, amigos, estou precisando deixar este botão "fechar" da mesma cor do frame. Fiz diversas tentativas mas não consegui. Alguém de boa vontade poderia me ajudar? Obrigado. Vejam o código.
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

class Teste extends JFrame {
	private JButton b1;
	private TratarBotao tb;
    FontMetrics fm;
    String s = "Teste, testes, testes";

   public Teste() {   	
    setTitle("PPI-V");
    b1 = new JButton("fechar");    
    tb = new TratarBotao(b1);
    getContentPane().setLayout(new FlowLayout());  
    getContentPane().add(b1);
    b1.setForeground(Color.red);
    b1.addActionListener(tb);    
    
    Font font = new Font("Jokerman", Font.ITALIC, 36);
    setFont(font);
    fm = getFontMetrics(font);
    setSize(fm.stringWidth(s)+30, fm.getHeight()+60);
    
    setVisible(true);
	setSize(550, 550);
	setResizable(false);
    setLocationRelativeTo(null);	

   }
    public class TratarBotao implements ActionListener{
   	private JButton b1;
   	public TratarBotao(JButton ba){
   		b1 = ba;
   	    }
   	public void actionPerformed(ActionEvent ae){
   		if(ae.getSource()== b1){
   			//new TelaLogar();
   	     	setVisible(false);     
   	    	}
    	}
   	
   }
   public void paint(Graphics g) {

      Insets ins = getInsets();
      int w = getSize().width-ins.left-ins.right;
      int h = getSize().height-ins.top-ins.bottom;

      int centerX = w/2 + ins.left;
      int centerY = h/2 + ins.top;

      g.setColor(Color.red);
      g.fillRect(ins.left, ins.top, w, h);

      g.setColor(Color.yellow);
      g.drawString(
         s, 
         centerX-fm.stringWidth(s)/2, 
         centerY + (fm.getAscent()-fm.getDescent())/2
      );

   }
   static public void main(String[] args) {
      new Teste();
   }
}

2 Respostas

I

Olá… :smiley:b1.setBackground(Color.RED);Ajudei :?: Até +…

DANIEL35

Sim, ajudou. Muito obrigado.

Criado 6 de junho de 2010
Ultima resposta 6 de jun. de 2010
Respostas 2
Participantes 2