Jump to content
  • Revista PROGRAMAR: Já está disponível a edição #60 da revista programar. Faz já o download aqui!

Sign in to follow this  
Crocks23

Gradiente em botão Ajuda pls

Recommended Posts

Crocks23

Olá pessoal ;)

Tenho umas duvidas, como sou iniciante em java, e para mim um pouco dificil.

Estou a fazer uma calculadora, e nao consigo mudar a cor dos botões para gradient nem colocar uma imagem de fundo.

Já testei varios codigos de gradient mas nao consigo implementar no botão e o background fica a tapar todos os botões.

Queria colocar a calculadora mais elegante

Codigo da calculadora:


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.UIManager;
import javax.swing.plaf.ColorUIResource;
import java.awt.*;
import java.awt.event.*;
import java.math.*;
import javax.swing.*;



class CalculatorPanel extends JPanel implements ActionListener
{
public CalculatorPanel()
{

setLayout(null);
setBackground(Color.white);

//------------ Display -----------------------
display = new JTextField("0");
display.setEditable(false);
add(display);
display.setBounds(50,15,215,40);

//------------ Botões ------------------------
b1 = new Button("0");
b1.setForeground(Color.black);
add(b1);
b1.setBounds(50,235,50,50);
b1.addActionListener(this);



b2 = new Button("1");
b2.setBackground(Color.white);
b2.setForeground(Color.black);
add(b2);
b2.addActionListener(this);
b2.setBounds(50,180,50,50);

b3 = new Button("4");
b3.setBackground(Color.white);
b3.setForeground(Color.black);
add(b3);
b3.addActionListener(this);
b3.setBounds(50,125,50,50);

b4 = new Button("7");
b4.setBackground(Color.white);
b4.setForeground(Color.black);
add(b4);
b4.addActionListener(this);
b4.setBounds(50,70,50,50);


b6 = new Button("2");
b6.setBackground(Color.white);
b6.setForeground(Color.black);
add(b6);
b6.addActionListener(this);
b6.setBounds(105,180,50,50);

b7 = new Button("5");
b7.setBackground(Color.white);
b7.setForeground(Color.black);
add(b7);
b7.addActionListener(this);
b7.setBounds(105,125,50,50);

b8 = new Button("8");
b8.setBackground(Color.white);
b8.setForeground(Color.black);
add(b8);
b8.addActionListener(this);
b8.setBounds(105,70,50,50);

b9 = new Button(".");
b9.setBackground(Color.white);
b9.setForeground(Color.black);
add(b9);
b9.addActionListener(this);
b9.setBounds(105,235,50,50);

b10 = new Button("3");
b10.setBackground(Color.white);
b10.setForeground(Color.black);
add(b10);
b10.addActionListener(this);
b10.setBounds(160,180,50,50);

b11 = new Button("6");
b11.setBackground(Color.white);
b11.setForeground(Color.black);
add(b11);
b11.addActionListener(this);
b11.setBounds(160,125,50,50);

b12 = new Button("9");
b12.setBackground(Color.white);
b12.setForeground(Color.black);
add(b12);
b12.addActionListener(this);
b12.setBounds(160,70,50,50);

b13= new Button("+");
b13.setBackground(Color.white);
b13.setForeground(Color.gray);
add(b13);
b13.addActionListener(this);
b13.setBounds(215,125,50,50);

b14 = new Button("-");
b14.setBackground(Color.white);
b14.setForeground(Color.gray);
add(b14);
b14.addActionListener(this);
b14.setBounds(215,180,50,50);

b15 = new Button("\u00d7");
b15.setBackground(Color.white);
b15.setForeground(Color.gray);
add(b15);
b15.addActionListener(this);
b15.setBounds(215,235,50,50);

b16 = new Button("\u00f7");
b16.setBackground(Color.white);
b16.setForeground(Color.gray);
add(b16);
b16.addActionListener(this);
b16.setBounds(215,70,50,50);

b17= new Button("=");
b17.setBackground(Color.green);
b17.setForeground(Color.black);
add(b17);
b17.addActionListener(this);
b17.setBounds(270,70,50,215);


b18 = new Button("%");
b18.setBackground(Color.white);
b18.setForeground(Color.red);
add(b18);
b18.addActionListener(this);
b18.setBounds(160,235,50,50);



b21 = new Button("PI");
b21.setBackground(Color.gray);
b21.setForeground(Color.orange);
add(b21);
b21.addActionListener(this);
b21.setBounds(335,70,50,50);

b24= new Button("sin");
b24.setBackground(Color.gray);
b24.setForeground(Color.orange);
add(b24);
b24.addActionListener(this);
b24.setBounds(335,125,50,50);

b25 = new Button("cos");
b25.setBackground(Color.gray);
b25.setForeground(Color.orange);
add(b25);
b25.addActionListener(this);
b25.setBounds(335,180,50,50);

b26 = new Button("tan");
b26.setBackground(Color.gray);
b26.setForeground(Color.orange);
add(b26);
b26.addActionListener(this);
b26.setBounds(335,235,50,50);

 b40= new Button("C");
 b40.setBackground(Color.red);
 b40.setForeground(Color.white);
 add(b40);
 b40.addActionListener(this);
 b40.setBounds(270,15,55,40);

 b41 = new Button("CE");
 b41.setBackground(Color.red);
 b41.setForeground(Color.white);
 add(b41);
 b41.addActionListener(this);
 b41.setBounds(330,15,55,40);				

}

public void actionPerformed(ActionEvent evt)
{
 String s = evt.getActionCommand();

 if (s.equals("C"))
 {
	 operando1 = 0;
		 op = "=";
	 display.setText("0");		
		 start = true;
	 }	
 else if('0' <= s.charAt(0) && s.charAt(0) <= '9'|| s.equals("."))
 {
	 if (start)
	 {
	 display.setText(s);
	 start = false;
	 }
	 else
	 {
	 display.setText(display.getText() + s);
	 }
 }
 else
 {

	 if (s.equals("PI"))
	 {
		 double aux = 1;
		 aux = Math.PI;
		 display.setText("" + aux);

	 }
	 if(s.equals("E"))
	 {
		 double aux = 1;
		 aux = Math.E;
		 display.setText("" + aux);
	 }
	 if(s.equals("%"))
	 {
		 double x = Double.parseDouble(display.getText());
		 double total = (operando1*x)/100;
		 display.setText("" + total);
	 }
		 if(s.equals("+/-"))
	 {
		 double total = 0;
		 double x = Double.parseDouble(display.getText());
		 if (x != 0)
			 total = (x * -1);
		 display.setText("" + total);
	 }

	 String memoria = "M RM M2 M3";

	 if(memoria.indexOf(s)!= -1)
	 {
		 JOptionPane.showMessageDialog(
			 null, "Este recursos será implementado na Calculadora verão 2. Aguarde !!!");
	 }


	 String comp = "sqrt In n! PI log tan cos sin x^2"
				 + "x^3 asin acos atan e^x" ;

	 if (comp.indexOf(s) != -1)
	 {
		 calculate1(Double.parseDouble
			 (display.getText()),s);
		 start = true;
	 }	

	 String comum = "= + - * / \u00d7 \u00f7 x^y mod";

	 if (s.equals("CE"))
	 {
		 display.setText("" + operando1);
		 op = s;
		 start = true;
	 }else if (comum.indexOf(s) != -1)
	 {
		 double x = Double.parseDouble(display.getText());
		 calculate(x);
		 op = s;
		 start = true;
	 }

 }		
}

public void calculate1(double opr,String op)
{
 if (op.equals("sqrt")) opr = Math.sqrt(opr);
 else if(op.equals("In")) opr = Math.log(opr);
 else if(op.equals("n!")) opr = fatorial(opr);
 else if(op.equals("log")) opr = Math.log(opr);
 else if(op.equals("tan")) opr = Math.tan((opr*Math.PI) / 180);
 else if(op.equals("cos")) opr = Math.cos((opr*Math.PI) / 180);
 else if(op.equals("sin")) opr = Math.sin((opr*Math.PI) / 180) ;
 else if(op.equals("asin")) opr = Math.asin(opr);
 else if(op.equals("acos")) opr = Math.acos(opr);
 else if(op.equals("atan")) opr = Math.atan(opr);
 else if(op.equals("x^2")) opr = Math.pow(opr,2);
 else if(op.equals("x^3")) opr = Math.pow(opr,3);
 else if(op.equals("e^x")) opr = Math.exp(opr);			

 display.setText("" + opr);
}

public double fatorial(double nro)
{
 if(nro <=1)
	 return 1;
 else
	 return nro*fatorial(nro-1);
}

public double umSobre(double nro)
{
 if (nro != 0)
	 return ((1)/(nro));
 else
	 return 0;
}					

public void calculate(double n)
{

 if (op.equals("+")) operando1 += n;
 else if (op.equals("-")) operando1 -= n;
 else if (op.equals("\u00d7")) operando1 *= n;
 else if (op.equals("\u00f7")) operando1 /= n;
 else if (op.equals("=")) operando1 = n;
 else if (op.equals("x^y")) operando1 = Math.pow(operando1,n);
 else if (op.equals("mod")) operando1 %= n;

 display.setText("" + operando1);
}

private JTextField display;
private double operando1 = 0;
private BigDecimal memory[];
private boolean start = true;
private String op = "=";
private Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,
		 b15,b16,b17,b18,b21,b22,b23,b24,b25,b26,b40,b41;		
}

class CalculatorFrame extends JFrame
{

JTextArea t;

public CalculatorFrame()
{
 setTitle("Calculadora");
 setSize(435,440);
 addWindowListener(new WindowAdapter()
 {
	 public void windowClosing(WindowEvent e)
	 {
	 System.exit(0);
	 }
 });

 Container contentPane = getContentPane();
 contentPane.add(new CalculatorPanel());

 // -------------- Menu -----------------------

 MenuBar bar = new MenuBar();

 Menu calc = new Menu("Calculadora");
 MenuItem exit = new MenuItem("	 Sair ");
 calc.add(exit);
 exit.addActionListener(new ActionListener()
 {
	 public void actionPerformed(ActionEvent e)
	 {
	 System.exit(0);
	 }
 });
 bar.add(calc);


 Menu about = new Menu("Sobre");
 bar.add(about);

 MenuItem about1 = new MenuItem(" Calculadora");
 about.add(about1);
 about1.addActionListener(new ActionListener()
 {
	 public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(null, "Calculadora		 "
+ "\n "
+"\n ");
}
});
bar.add(about);

setMenuBar(bar);
//--------------------------------------------
}
}





public class Calculadora
{
public static void main(String[] args)
{
 JFrame frame = new CalculatorFrame();	
 frame.show();


}
}


Cumprimentos :)

Edited by Crocks23
geshi

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.