swing:color
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
swing:color [2015/05/05 13:12] – gthanos | swing:color [Unknown date] (current) – external edit (Unknown date) 127.0.0.1 | ||
---|---|---|---|
Line 8: | Line 8: | ||
Για παράδειγμα, | Για παράδειγμα, | ||
<code java> | <code java> | ||
- | this.getContnetPane().setBackground(Color.BLUE); | + | // assuming you are inside the constructor or a non static method of the frame |
+ | this.getContentPane().setBackground(Color.BLUE); | ||
</ | </ | ||
Line 54: | Line 55: | ||
====== Παράδειγμα χρήση της κλάσης Color ====== | ====== Παράδειγμα χρήση της κλάσης Color ====== | ||
- | Παρακάτω δίνουμε ένα παράδειγμα όπου χρησιμοποιούμε | + | Παρακάτω δίνουμε ένα παράδειγμα όπου χρησιμοποιούμε την κλάση [[https:// |
+ | |||
+ | <code java ColorDemo.java> | ||
+ | import javax.swing.*; | ||
+ | import java.awt.*; | ||
+ | import java.awt.event.*; | ||
+ | |||
+ | public class ColorDemo extends JFrame implements ActionListener { | ||
+ | public static final int WIDTH = 300; | ||
+ | public static final int HEIGHT = 200; | ||
+ | |||
+ | private JPanel redPanel; | ||
+ | private JPanel whitePanel; | ||
+ | private JPanel bluePanel; | ||
+ | |||
+ | public static void main(String args[]) { | ||
+ | ColorDemo gui = new ColorDemo(); | ||
+ | gui.setVisible(true); | ||
+ | } | ||
+ | |||
+ | public ColorDemo() { | ||
+ | super(" | ||
+ | setSize(WIDTH, | ||
+ | setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
+ | setLayout(new BorderLayout()); | ||
+ | |||
+ | JPanel biggerPanel = new JPanel(); | ||
+ | biggerPanel.setLayout(new GridLayout(1, | ||
+ | |||
+ | redPanel = new JPanel(); | ||
+ | redPanel.setBackground(Color.LIGHT_GRAY); | ||
+ | biggerPanel.add(redPanel); | ||
+ | |||
+ | whitePanel = new JPanel(); | ||
+ | whitePanel.setBackground(Color.LIGHT_GRAY); | ||
+ | biggerPanel.add(whitePanel); | ||
+ | |||
+ | bluePanel = new JPanel(); | ||
+ | bluePanel.setBackground(Color.LIGHT_GRAY); | ||
+ | biggerPanel.add(bluePanel); | ||
+ | |||
+ | add(biggerPanel, | ||
+ | |||
+ | JPanel buttonPanel = new JPanel(); | ||
+ | buttonPanel.setLayout(new FlowLayout()); | ||
+ | buttonPanel.setBackground(Color.LIGHT_GRAY); | ||
+ | |||
+ | JButton redButton = new JButton(" | ||
+ | redButton.setBackground(Color.RED); | ||
+ | redButton.addActionListener(this); | ||
+ | buttonPanel.add(redButton); | ||
+ | |||
+ | JButton whiteButton = new JButton(" | ||
+ | whiteButton.setBackground(Color.WHITE); | ||
+ | whiteButton.addActionListener(this); | ||
+ | buttonPanel.add(whiteButton); | ||
+ | |||
+ | JButton blueButton = new JButton(" | ||
+ | blueButton.setBackground(Color.BLUE); | ||
+ | blueButton.addActionListener(this); | ||
+ | buttonPanel.add(blueButton); | ||
+ | |||
+ | add(buttonPanel, | ||
+ | } | ||
+ | |||
+ | public void actionPerformed(ActionEvent e) { | ||
+ | String buttonString = e.getActionCommand(); | ||
+ | if(buttonString.equals(" | ||
+ | redPanel.setBackground(Color.RED); | ||
+ | whitePanel.setBackground(Color.LIGHT_GRAY); | ||
+ | bluePanel.setBackground(Color.LIGHT_GRAY); | ||
+ | } | ||
+ | else if(buttonString.equals(" | ||
+ | redPanel.setBackground(Color.LIGHT_GRAY); | ||
+ | whitePanel.setBackground(Color.WHITE); | ||
+ | bluePanel.setBackground(Color.LIGHT_GRAY); | ||
+ | } | ||
+ | else if(buttonString.equals(" | ||
+ | redPanel.setBackground(Color.LIGHT_GRAY); | ||
+ | whitePanel.setBackground(Color.LIGHT_GRAY); | ||
+ | bluePanel.setBackground(Color.BLUE); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | | Προηγούμενο: | ||
swing/color.1430831535.txt.gz · Last modified: 2015/05/05 12:12 (external edit)