It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming Java Forum

need some help with button colors.

need some help with button colors.

Postby clavicles on Tue Jan 30, 2007 12:51 am

hey everyone. im still fairly new to java and im having some trouble making my interface not look bland. im trying to change the button background colors, and maybe the color of the frame(s) as well. its a simple calculator program and all i need is a line or two of code that i cannot figure out. thanks a lot.

Code: Select all
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class Calculator extends JFrame implements ActionListener
{
    JTextField num1 = new JTextField("00.00");
    JTextField num2 = new JTextField("00.00");
   
    JLabel answer = new JLabel("00.00");
    JLabel symbol = new JLabel("...");
    JLabel equals = new JLabel("=");
   
    JButton additionButton = new JButton(" + ");
    JButton subtractionButton = new JButton(" - ");
    JButton divisionButton = new JButton(" / ");
    JButton multiplicationButton = new JButton(" * ");
    JButton clearButton = new JButton(" clr! ");
    JButton aboutButton = new JButton( "about!" );
    JButton exitButton = new JButton(" exit!" );


    JFrame calcFrame = new JFrame("Calculator!");
   
    public Calculator()
    {

        JPanel fields = new JPanel();
        fields.setLayout(new BoxLayout(fields, BoxLayout.X_AXIS));
        fields.add(num1);
        fields.add(symbol);
        fields.add(num2);
        fields.add(equals);
        fields.add(answer);
       
        JPanel buttons = new JPanel();
        buttons.setLayout(new FlowLayout());
        buttons.add(additionButton);
        buttons.add(subtractionButton);
        buttons.add(divisionButton);
        buttons.add(multiplicationButton);



        JPanel moreButtons = new JPanel(new FlowLayout());
        moreButtons.add(clearButton);
        moreButtons.add(aboutButton);
        moreButtons.add(exitButton);
     
        getContentPane().setLayout(new BorderLayout());
        getContentPane().add("North", fields);
        getContentPane().add("Center", buttons);
        getContentPane().add("South", moreButtons);
       
        additionButton.addActionListener(this);
        subtractionButton.addActionListener(this);
        divisionButton.addActionListener(this);
        multiplicationButton.addActionListener(this);
        clearButton.addActionListener(this);
        exitButton.addActionListener(this);
        pack();
        setVisible(true);

        calcFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        aboutButton.addActionListener(new AboutListener());

        clearButton.setMnemonic(KeyEvent.VK_C);
        aboutButton.setMnemonic(KeyEvent.VK_A);
        exitButton.setMnemonic(KeyEvent.VK_X);
    }

    public void actionPerformed(ActionEvent e)
    {
        double x = Double.parseDouble(num1.getText());
        double y = Double.parseDouble(num2.getText());
        if (e.getSource() == additionButton)
        {
            answer.setText(String.valueOf( x + y ));
            symbol.setText("+");
        }
        if (e.getSource() == subtractionButton)
        {
            answer.setText(String.valueOf( x - y ));
            symbol.setText("-");
        }
        if (e.getSource() == divisionButton)
        {
            answer.setText(String.valueOf( x / y ));
            symbol.setText("/");
        }
        if (e.getSource() == multiplicationButton)
        {
            answer.setText(String.valueOf( x * y ));
            symbol.setText("*");
        }
        if (e.getSource() == clearButton)
        {
            answer.setText("00.00");
            symbol.setText("...");
            num1.setText("00.00");
            num2.setText("00.00");
        }
        if (e.getSource() == exitButton)
        {
            System.exit(0);
        }

    }


  public class AboutListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      JFrame popUp = new JFrame("About frame");
      popUp.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      popUp.setSize(250,200);
      MyPanel panel = new MyPanel();
      popUp.getContentPane().add(panel);
      popUp.setVisible(true);
    }
    public class MyPanel extends JPanel {
      public void paintComponent(Graphics g) {
        int width = this.getWidth();
        int height = this.getHeight();
        g.setColor(Color.black);
        g.fillRect(0, 0, width, height);
        g.setColor(Color.green);
        g.drawString("  ^_^!   a ken zemaitis production.  ", width/4, height/2);
      }
    }
  }
   
    public static void main(String args[])
    {
        UIManager.put("Button.background",new javax.swing.plaf.ColorUIResource(Color.YELLOW));
        Calculator f = new Calculator();
    }
  }

-ken zemaitis.
clavicles
 
Posts: 0
Joined: Tue Jan 30, 2007 12:48 am

Who is online

Users browsing this forum: No registered users and 1 guest