Академический Документы
Профессиональный Документы
Культура Документы
}
Here is a program, SSLClient,that puts all the pieces together:
import
import
import
import
import
import
import
import
import
java.io.BufferedReader;
java.io.InputStream;
java.io.InputStreamReader;
java.io.IOException;
java.io.OutputStream;
java.io.PrintWriter;
java.net.Socket;
javax.net.SocketFactory;
javax.net.ssl.SSLSocketFactory;
// Close everything
out.close();
in.close();
socket.close();
} catch (IOException e) {
System.err.println("Problems talking to " + hostname);
e.printStackTrace();
}
}
}
When you run the program, pass in the name of the host you want
to communicate with. Consider running Tomcat locally, or connect
to sun.com. Note that the port constant in SSLClient is 443. You
can alter the HTTPS port constant in the program if you need to,
or you can change the program so that the constant is specified
on the command line.
The results of communicating over the HTTPS port might be
different than the results you get when communicating over the
regular HTTP port. Consider saving the output and view it in
your browser. You should trim off the standard header values,
such as the date and time and server. Of course, you should
leave in the server value if you're interested in what server
a site is using. For instance, connecting to www.sun.com
through the SSLClient program:
java SSLClient www.sun.com
reveals the use of the SunONE WebServer 6.0 in the various
header fields:
HTTP/1.1 200 OK
Server: SunONE WebServer 6.0
Date: Tue, 03 Aug 2004 22:30:32 GMT
P3p: policyref="http://www.sun.com/p3p/Sun_P3P_Policy.xml",
...
Clients should examine the credentials received to make sure that
they are talking to the host they think they are. Use
socket.getSession().getPeerCertificates() or getPeerPrincipal().
It's non-trivial for someone to reroute the connection to a
malicious host, and this is one way to check against that. You
can use the HostnameVerifier interface to verify the host name.
Security-conscious developers (and users) should consider
downloading the unlimited Java Cryptography Extension (JCE)
policy files from the J2SE 1.4.2 downloads page
(http://java.sun.com/j2se/1.4.2/download.html). The version of
the JCE that comes with J2SE 1.4 is strong, but limited.
Provided that you live in an eligible country, the added keysize
support offers a much stronger level of cryptography capabilities
than JSSE alone.
To learn more about the JCE, see the JCE Reference Guide
(http://java.sun.com/j2se/1.4.2/docs/guide/security/jce/JCERefGuide.html).
To learn more about JSSE, see the JSSE Reference Guide
(http://java.sun.com/j2se/1.4.2/docs/guide/security/jsse/JSSERefGuide.html).
frame.setVisible(true);
}
};
EventQueue.invokeLater(runner);
}
}
Using JColorChooser in Your Own Window
Although the Color1 example illustrates the way you will likely
use the JColorCooser most of the time, it certainly isn't the
only way to use it. The code in Color1 calls the showDialog
method of JColorChooser to place the component in a newly
created, modal JDialog box. Instead, you can simply add the
component to your own window -- the control is itself just a
JComponent. Here's an example, Color2, that adds the
JColorChooser to a window:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Color2 {
public static void main(String args[]) {
Runnable runner = new Runnable() {
public void run() {
JFrame frame = new JFrame("Second Sample");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = frame.getContentPane();
final JLabel label =
new JLabel("Hello, World", JLabel.CENTER);
label.setFont(
new Font("Serif", Font.BOLD | Font.ITALIC, 48));
contentPane.add(label, BorderLayout.SOUTH);
Color initialBackground = label.getBackground();
final JColorChooser colorChooser =
new JColorChooser(initialBackground);
contentPane.add(colorChooser, BorderLayout.CENTER);
// Missing source here...
frame.pack();
frame.setVisible(true);
}
};
EventQueue.invokeLater(runner);
}
}
Although adding the component to the window works, it doesn't
display an OK or Cancel button. So you don't know when the user
is done selecting a color. The Reset button isn't displayed
either, but that never notified the caller of selection. If you
use the JColorChooser in this second way, it is the caller's
java.awt.*;
java.awt.event.*;
javax.swing.*;
javax.swing.colorchooser.*;
javax.swing.event.*;
label.setSize(label.getPreferredSize());
final JColorChooser colorChooser =
new JColorChooser(Color.RED);
colorChooser.setPreviewPanel(label);
// For no preview panel, add a JComponent with no size
// colorChooser.setPreviewPanel(new JPanel());
contentPane.add(colorChooser, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
};
EventQueue.invokeLater(runner);
}
}
As the comments in the program indicate, if you want a chooser
with no preview panel, add a JComponent subclass such as a JPanel
with no size.
Passing in null to the setPreviewPanel method does not remove
the panel. Instead, it tells the ColorChooserComponentFactory to
use the default panel.
Working with Chooser Panels
You might have noticed that the JColorChooser comes with three
different ways of selecting colors: the Swatches tab, the HSB tab
(for selecting color from Hue, Saturation, and Brightness
levels), and the RGB tab (for selecting color from Red, Green,
and Blue levels). If you don't like the default set of panels
provided by these tabs, you can add your own. Each panel extends
from the AbstractColorChooserPanel class.
There are various support methods in JColorChooser that you use
to create your own panels:
o getChooserPanels() returns the current set of panels.
o addChooserPanel(AbstractColorChooserPanel panel) adds a
specific panel.
o removeChooserPanel(AbstractColorChooserPanel panel) removes a
specific panel.
o setChooserPanels(AbstractColorChooserPanel[] panels) changes
all the available panels at once, instead of adding and
removing individual panels.
The AbstractColorChooserPanel class consists of five abstract
methods that you must implement to provide your own chooser
panel:
o
o
o
o
o
buildChooser()
getDisplayName()
getLargeDisplayIcon()
getSmallDisplayIcon()
updateChooser() [protected]
javax.swing.*;
javax.swing.colorchooser.*;
java.awt.*;
java.awt.event.*;
Color.GREEN,
Color.LIGHT_GRAY,
Color.MAGENTA,
Color.ORANGE,
Color.PINK,
Color.RED,
Color.WHITE,
Color.YELLOW,
SystemColor.activeCaption,
SystemColor.activeCaptionBorder,
SystemColor.activeCaptionText,
SystemColor.control,
SystemColor.controlDkShadow,
SystemColor.controlHighlight,
SystemColor.controlLtHighlight,
SystemColor.controlShadow,
SystemColor.controlText,
SystemColor.desktop,
SystemColor.inactiveCaption,
SystemColor.inactiveCaptionBorder,
SystemColor.inactiveCaptionText,
SystemColor.info,
SystemColor.infoText,
SystemColor.menu,
SystemColor.menuText,
SystemColor.scrollbar,
SystemColor.text,
SystemColor.textHighlight,
SystemColor.textHighlightText,
SystemColor.textInactiveText,
SystemColor.textText,
SystemColor.window,
SystemColor.windowBorder,
SystemColor.windowText,
null};
// Change combo box to match color, if possible
private void setColor(Color newColor) {
int position = findColorPosition(newColor);
comboBox.setSelectedIndex(position);
}
// Given a label, find the position of the label in the list
private int findColorLabel(Object label) {
String stringLabel = label.toString();
int position = NOT_FOUND;
for (int i=0,n=labels.length; i<n; i++) {
if (stringLabel.equals(labels[i])) {
position=i;
break;
}
}
return position;
}
// Given a color, find the position whose color matches
// This could result in a position different from original
// if two are equal. Since color is same, this is
//considered to be okay
private int findColorPosition(Color color) {
import javax.swing.colorchooser.*;
import javax.swing.event.*;
public class Color4 {
public static void main(String args[]) {
Runnable runner = new Runnable() {
public void run() {
JFrame frame = new JFrame("Last Sample");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = frame.getContentPane();
final JLabel label = new JLabel
("Hello, World", JLabel.CENTER);
label.setFont(
new Font("Serif", Font.BOLD | Font.ITALIC, 48));
label.setSize(label.getPreferredSize());
final JColorChooser colorChooser =
new JColorChooser(Color.RED);
colorChooser.setPreviewPanel(label);
AbstractColorChooserPanel myPanel =
new MyColorChooserPanel();
AbstractColorChooserPanel panels[] = {myPanel};
colorChooser.setChooserPanels(panels);
contentPane.add(colorChooser, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
};
EventQueue.invokeLater(runner);
}
}
For more information on working with JColorChooser, see the "How
to use Color Choosers" trail
(http://java.sun.com/tutorial/uiswing/components/colorchooser.html)
in The Java Tutorial.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - OTHER RESOURCES
Got a question about Java technologies or tools? Then join
these upcoming chats and webinars:
Chats (http://java.sun.com/developer/community/chat/):
August 17. 11:00 A.M. PDT/6:00 P.M. GMT.
What's Happening in the JCP?
August 24. 11:00 A.M. PDT/6:00 P.M. GMT.
J2ME Wireless Toolkit 2.2
Webinar (https://see.sun.com/Apps/DCS/mcp?q=STa8XTFt0953y)