Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.BorderLayout;
- import java.awt.Color;
- import java.awt.Dimension;
- import javax.swing.BorderFactory;
- import javax.swing.JButton;
- import javax.swing.JComponent;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.JMenuBar;
- import javax.swing.JMenu;
- import javax.swing.JMenuItem;
- import javax.swing.JColorChooser;
- public class prog2 extends JPanel
- {
- int i = 0;
- Color[] buttonColor = {Color.RED, Color.ORANGE, Color.YELLOW, Color.GREEN, Color.BLUE, Color.MAGENTA};
- //Array to hold colors
- JButton colbutton = new JButton("Click Here");
- //Button that will change colors
- JButton bahbutton = new JButton("Don't Click");
- //the second placeholderbutton
- Dimension size1 = new Dimension(100, 50);
- //Dimension for size setting
- private JMenuItem colorRed, colorOrange, colorYellow, colorGreen, colorBlue, colorViolet;
- public prog2()
- {
- super(new BorderLayout());
- //setting up for color changing
- this.add(colbutton, BorderLayout.CENTER);
- //places the main button in the center of the layout
- colbutton.setMaximumSize(size1);
- colbutton.setMinimumSize(size1);
- //attempts to set min and max, but at least defines the starting size of the buttons
- this.add(bahbutton, BorderLayout.SOUTH);
- //places the secondary button below the first
- bahbutton.setMinimumSize(size1);
- bahbutton.setMaximumSize(size1);
- //attempts to set min and max, but at least defines the starting size of the buttons
- this.setBorder(BorderFactory.createEmptyBorder(50,50,50,50));
- //places empty space around the buttons
- JMenuBar mainMenu = new JMenuBar();
- this.setJMenuBar(mainMenu);
- JMenu colorMenuMain = new JMenu ("Colors");
- JMenu colorChoose = new JMenu ("Chooser");
- mainMenu.add(colorMenuMain);
- colorMenuMain.add(colorRed = new JMenuItem("Red"));
- colorMenuMain.add(colorOrange = new JMenuItem("Orange"));
- colorMenuMain.add(colorYellow = new JMenuItem("Yellow"));
- colorMenuMain.add(colorGreen = new JMenuItem("Green"));
- colorMenuMain.add(colorBlue = new JMenuItem("Blue"));
- colorMenuMain.add(colorViolet = new JMenuItem("Violet"));
- JColorChooser colchoose = new JColorChooser();
- colchoose.setBorder(BorderFactory.createTitledBorder("Choose Text Color"));
- colchoose.setPreviewPanel(new JPanel());
- colchoose.setColor(this.getForeground());
- colbutton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e)
- {
- i = ++i < buttonColor.length ? i : 0;
- colbutton.setBackground(buttonColor[i]);
- //increases I as the action is detected
- }
- });
- bahbutton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e)
- {
- bahbutton.setBackground(buttonColor[i]);
- //Turn button color back to default? idk
- }
- });
- colorRed.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e)
- {
- bahbutton.setBackground(buttonColor[0]);
- }
- });
- colorOrange.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e)
- {
- bahbutton.setBackground(buttonColor[1]);
- }
- });
- colorYellow.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e)
- {
- bahbutton.setBackground(buttonColor[2]);
- }
- });
- colorGreen.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e)
- {
- bahbutton.setBackground(buttonColor[3]);
- }
- });
- colorBlue.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e)
- {
- bahbutton.setBackground(buttonColor[4]);
- }
- });
- colorViolet.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e)
- {
- bahbutton.setBackground(buttonColor[5]);
- }
- });
- colorChoose.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e)
- {
- Color newColor = JColorChooser.showDialog(
- prog2.this,
- "Choose Background Color",
- this.getBackground());
- if (newColor != null)
- {
- this.setForeground(newColor);
- }
- }
- });
- colchoose.getSelectionModel().addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e)
- {
- //Do I need a new listener to change the color, or?
- }
- });
- }
- private static void run() {
- JFrame frame = new JFrame("Color Changing");
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- //standard exiting the program when it's closed
- JComponent contentPane = new prog2();
- //creates content based on the constructor
- contentPane.setOpaque(true);
- frame.setTitle("Color Changing");
- //makes a title! doesn't do much, though.
- frame.setContentPane(contentPane);
- frame.pack();
- frame.setVisible(true);
- //standard pane initialization
- }
- public static void main(String[] args)
- {
- run();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement