User Tools

Site Tools


swing:color

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
swing:color [2015/05/05 13:03] – created gthanosswing:color [Unknown date] (current) – external edit (Unknown date) 127.0.0.1
Line 8: Line 8:
 Για παράδειγμα, αν θέλετε να αλλάξετε το background color σε μπλε σε ένα παράθυρο, αυτό μπορείτε να το κάνετε με μία δήλωση της μορφής (υποθέτωντας ότι είστε σε μία μέθοδο ή στον κατασκευαστή της κλάσης που κληρονομεί την κλάση JFrame): Για παράδειγμα, αν θέλετε να αλλάξετε το background color σε μπλε σε ένα παράθυρο, αυτό μπορείτε να το κάνετε με μία δήλωση της μορφής (υποθέτωντας ότι είστε σε μία μέθοδο ή στον κατασκευαστή της κλάσης που κληρονομεί την κλάση JFrame):
 <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);
 </code> </code>
  
Line 28: Line 29:
 </code> </code>
  
-Αν θέλετε να ορίσετε τα δικά σας αντικείμενα τύπου Color μπορείτε να το κάνετε με ένα από τους  κατασκευαστές.+Αν θέλετε να ορίσετε τα δικά σας αντικείμενα τύπου Color μπορείτε να το κάνετε με ένα από τους  παρακάτω [[https://docs.oracle.com/javase/7/docs/api/java/awt/Color.html#constructor_summary|κατασκευαστές]].
 <code java> <code java>
 Color(ColorSpace cspace, float[] components, float alpha) Color(ColorSpace cspace, float[] components, float alpha)
Line 50: Line 51:
 Color(int r, int g, int b, int a) Color(int r, int g, int b, int a)
 Creates an sRGB color with the specified red, green, blue, and alpha values in the range (0 - 255). Creates an sRGB color with the specified red, green, blue, and alpha values in the range (0 - 255).
 +</code>
  
 +====== Παράδειγμα χρήση της κλάσης Color ======
 +
 +Παρακάτω δίνουμε ένα παράδειγμα όπου χρησιμοποιούμε την κλάση [[https://docs.oracle.com/javase/8/docs/api/java/awt/Color.html|java.awt.Color]]. Δημιουργούμε ένα παράθυρο και ορίζουμε ως Manager για το content pane του παραθύρου τον [[swing:layout#Border Layout Manager|BorderLayout Manager]]. Στις περιοχές //WEST//, //CENTER//, //EAST// τοποθετούμε απλά JPanel αντικείμενα τα οποία τους αλλάζουμε το χρώμα. Στην περιοχή //SOUTH// τοποθετούμε ένα νέο JPanel (buttonPanel) με FlowLayout Manager και 3 κουμπιά, ένα κόκκινο, ένα άσπρο και ένα μπλε. Κάθε φορά που πατάμε ένα κουμπί τα χρώματα στο τρία παραπάνω Panels εναλλάσσονται. Δείτε τον κώδικα και κατεβάστε και τρέξτε το παράδειγμα.
 +
 +<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("Color Demonstration");
 +    setSize(WIDTH, HEIGHT);
 +    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 +    setLayout(new BorderLayout());
 +    
 +    JPanel biggerPanel = new JPanel();
 +    biggerPanel.setLayout(new GridLayout(1,3));
 +    
 +    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, BorderLayout.CENTER);
 +    
 +    JPanel buttonPanel = new JPanel();
 +    buttonPanel.setLayout(new FlowLayout());    
 +    buttonPanel.setBackground(Color.LIGHT_GRAY);
 +    
 +    JButton redButton = new JButton("Red");
 +    redButton.setBackground(Color.RED);
 +    redButton.addActionListener(this);
 +    buttonPanel.add(redButton);
 +    
 +    JButton whiteButton = new JButton("White");
 +    whiteButton.setBackground(Color.WHITE);
 +    whiteButton.addActionListener(this);
 +    buttonPanel.add(whiteButton);
 +    
 +    JButton blueButton = new JButton("Blue");
 +    blueButton.setBackground(Color.BLUE);
 +    blueButton.addActionListener(this);
 +    buttonPanel.add(blueButton);
 +    
 +    add(buttonPanel, BorderLayout.SOUTH);
 +  }
 +  
 +  public void actionPerformed(ActionEvent e) {
 +    String buttonString = e.getActionCommand();
 +    if(buttonString.equals("Red") ) {
 +      redPanel.setBackground(Color.RED);
 +      whitePanel.setBackground(Color.LIGHT_GRAY);
 +      bluePanel.setBackground(Color.LIGHT_GRAY);
 +    }
 +    else if(buttonString.equals("White") ) {
 +      redPanel.setBackground(Color.LIGHT_GRAY);
 +      whitePanel.setBackground(Color.WHITE);
 +      bluePanel.setBackground(Color.LIGHT_GRAY);
 +    }
 +    else if(buttonString.equals("Blue") ) {
 +      redPanel.setBackground(Color.LIGHT_GRAY);
 +      whitePanel.setBackground(Color.LIGHT_GRAY);
 +      bluePanel.setBackground(Color.BLUE);
 +    }
 +  }
 +}
 </code> </code>
 +
 +| Προηγούμενο: [[:swing:jframe | Η κλάση JFrame ]] | [[:toc | Περιεχόμενα ]] | Επόμενο: [[:swing:icons | Η κλάση ImageIcon ]] |
 +
swing/color.1430830986.txt.gz · Last modified: 2015/05/05 12:03 (external edit)