| | |
Using user input to change background color of JPanel
Thread Solved |
•
•
Join Date: Apr 2008
Posts: 108
Reputation:
Solved Threads: 1
How do you to take user input for red, green and blue(a number between 0 and 255) and make the background of the center JPanel that color.
Heres the code on what ive done so far:
Heres the code on what ive done so far:
Java Syntax (Toggle Plain Text)
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; /** * Write a description of class ColorTestApplet here. * * @author (your name) * @version (a version number or a date) */ public class ColorTestApplet extends JApplet { public void init() { this.setLayout(new BorderLayout()); //Declaring panels centerPanel = new JPanel(); northPanel = new JPanel(); southPanel = new JPanel(); //centerPanel.setLayout(new GridLayout()); this.add(centerPanel, BorderLayout.CENTER); this.add(northPanel, BorderLayout.NORTH); this.add(southPanel, BorderLayout.SOUTH); JLabel red = new JLabel("Red:"); JLabel green = new JLabel("Green:"); JLabel blue = new JLabel("Blue:"); redText = new JTextField("0", 5); greenText = new JTextField("0", 5); blueText = new JTextField("0", 5); redText.setHorizontalAlignment(redText.RIGHT); greenText.setHorizontalAlignment(redText.RIGHT); blueText.setHorizontalAlignment(redText.RIGHT); JButton show = new JButton("Show"); JLabel name = new JLabel("Kimberlie Davis"); northPanel.add(red); northPanel.add(redText); northPanel.add(green); northPanel.add(greenText); northPanel.add(blue); northPanel.add(blueText); southPanel.add(show); centerPanel.add(name); ShowAction showAction = new ShowAction(); show.addActionListener(showAction); } class ShowAction implements ActionListener { public void actionPerformed(ActionEvent e) { int red, green, blue; String color; try { red = Integer.parseInt(redText.getText()); green = Integer.parseInt(greenText.getText()); blue = Integer.parseInt(blueText.getText()); if(red > 255 || red < 0 || green > 255 || green < 0 || blue > 255 || blue < 0) { JOptionPane.showMessageDialog(null, "Please enter a number between 0 and 255!"); redText.setText("0"); blueText.setText("0"); greenText.setText("0"); } else { color = "" + red + green + blue; JOptionPane.showMessageDialog(null, color); //centerPanel.setBackground(red, blue, green); } //centerpanel.setBackground(Color.color); } catch(NumberFormatException exception) { JOptionPane.showMessageDialog(null, "Please enter a number!"); redText.setText("0"); blueText.setText("0"); greenText.setText("0"); } } } JPanel centerPanel; JPanel northPanel; JPanel southPanel; JTextField redText; JTextField greenText; JTextField blueText; }
Do you have to do it that way? I think a much more interesting way would be to use ColorChooser
Last edited by jasimp; Oct 29th, 2008 at 9:21 pm.
"Argyou not with the hand you are dealt in cards or life." ---- Wizard and Glass
Just create a new color using your values. I added the line of code into your try statement.
java Syntax (Toggle Plain Text)
try { red = Integer.parseInt(redText.getText()); green = Integer.parseInt(greenText.getText()); blue = Integer.parseInt(blueText.getText()); if(red > 255 || red < 0 || green > 255 || green < 0 || blue > 255 || blue < 0) { JOptionPane.showMessageDialog(null, "Please enter a number between 0 and 255!"); redText.setText("0"); blueText.setText("0"); greenText.setText("0"); } else { //creates a new color using the values inputed by the user Color colorTest = new Color(red,green,blue); color = "" + red + green + blue; JOptionPane.showMessageDialog(null, color); //sets the color to the one created from user inputted values centerPanel.setBackground(colorTest); } }
Last edited by jasimp; Oct 29th, 2008 at 9:24 pm.
"Argyou not with the hand you are dealt in cards or life." ---- Wizard and Glass
![]() |
Similar Threads
Other Threads in the Java Forum
- Previous Thread: AWT or Swing?
- Next Thread: Run Time Stack
| Thread Tools | Search this Thread |
911 actionlistener addressbook android api append applet application array arrays automation binary block bluetooth character chat class client code component consumer csv database desktop developmenthelp eclipse error fractal ftp game givemetehcodez graphics gui html ide image input integer j2me j2seprojects japplet java javaarraylist javac javaee javaprojects jni jpanel julia lego linked linux list loop loops mac map method methods mobile netbeans newbie number objects online oriented panel printf problem program programming project projects properties recursion replaydirector reporting researchinmotion rotatetext rsa scanner se server set sms sort sql string swing test textfields threads time title tree tutorial-sample ubuntu update windows working






