下面列出了怎么用java.awt.HeadlessException的API类实例代码及写法,或者点击链接到github查看源代码。
public static void main(final String[] args) throws HeadlessException {
final Frame frame = new Frame("Test Frame");
final List list = new List();
frame.setSize(300, 200);
list.add(ITEM_NAME);
list.select(0);
frame.add(list);
frame.validate();
frame.setVisible(true);
sleep();
if (!ITEM_NAME.equals(list.getSelectedItem())) {
throw new RuntimeException("List item not selected item.");
}
list.removeAll();
frame.dispose();
}
@Test
public void testStuff()
throws ClassNotFoundException, UnsupportedLookAndFeelException, InstantiationException, IllegalAccessException {
try {
if (ImprovedFileChooser.isMacOs) {
System.setProperty("apple.laf.useScreenMenuBar", "true");
} else {
for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
}
ImprovedFileChooser fileChooser = new ImprovedFileChooser();
fileChooser.setPreferredSize(new Dimension(1000, 750));
fileChooser.showDialog(null, "Choose");
} catch (HeadlessException e) {
// ok to fail if there is no display
}
}
/**
* Test the processing of modFeat. Checks that when in select single and
* close mode, only one instance of a feat with a sub-choice is added.
*/
@Test
public void testModFeat()
{
readyToRun();
final PlayerCharacter character = new PlayerCharacter();
character.setRace(human);
character.incrementClassLevel(1, pcClass, true);
UIPropertyContext.setSingleChoiceAction(Constants.CHOOSER_SINGLE_CHOICE_METHOD_SELECT_EXIT);
ChooserFactory.useRandomChooser();
assertEquals(
2, (int)character.getRemainingFeatPoints(true),
"Start with 2 feats"
);
try
{
AbstractCharacterTestCase.applyAbility(character, BuildUtilities.getFeatCat(), toughness, "");
}
catch (HeadlessException e)
{
Logging.debugPrint("Ignoring Headless exception.");
}
assertEquals(1, (int)character.getRemainingFeatPoints(true), "Only 1 feat used");
}
public static void main(final String[] args) throws HeadlessException {
final Frame frame = new Frame("Test Frame");
final List list = new List();
frame.setSize(300, 200);
list.add(ITEM_NAME);
list.select(0);
frame.add(list);
frame.validate();
frame.setVisible(true);
sleep();
if (!ITEM_NAME.equals(list.getSelectedItem())) {
throw new RuntimeException("List item not selected item.");
}
list.removeAll();
frame.dispose();
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
try {
doTheChecks(chain, authType);
} catch (CertPathValidatorException e) {
try {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
certControll.addChain(chain);
}
});
} catch (HeadlessException e1) {
Log.error("Couldn't add certificate from presented chain");
}
throw new CertificateException(e);
}
}
private void createSelfSignedCertificate() {
idControll.setUpData(commonNameField.getText(), organizationUnitField.getText(), organizationField.getText(), countryField.getText(),
cityField.getText());
try {
KeyPair keyPair = idControll.createKeyPair();
X509Certificate cert = idControll.createSelfSignedCertificate(keyPair);
if (saveCertToFile.isSelected()) {
PemBuilder pemBuilder = new PemHelper().new PemBuilder();
pemBuilder.add(keyPair.getPrivate());
pemBuilder.add(cert);
pemBuilder.saveToPemFile(IdentityController.CERT_FILE);
JOptionPane.showMessageDialog(null,
Res.getString("dialog.self.signed.certificate.has.been.created") + IdentityController.SECURITY_DIRECTORY.toString());
} else {
try {
idControll.addEntryToKeyStore(cert, keyPair.getPrivate());
} catch (HeadlessException | InvalidNameException | KeyStoreException e) {
Log.error("Couldn't save entry to IdentityStore", e);
}
}
} catch (NoSuchAlgorithmException | NoSuchProviderException | IOException | OperatorCreationException | CertificateException e1) {
Log.error("Couldn't create Self Signed Certificate", e1);
}
}
@Test
public void testPanel() {
try {
PrefPanel pp = new PrefPanel("title", (PreferencesExt) store.node("test"));
pp.addTextField("name", "name", "defValue");
Field.Text longF = pp.addTextField("Longname", "Longname",
"defValue really long name for to be with starting value gotta adjust the thing");
Field.Int iu = pp.addIntField("testIU", "number of times to barf", 2);
pp.finish();
} catch (HeadlessException e) {
// ok to fail if there is no display
}
}
/**
* Creates a new DropTarget given the <code>Component</code>
* to associate itself with, an <code>int</code> representing
* the default acceptable action(s) to
* support, a <code>DropTargetListener</code>
* to handle event processing, a <code>boolean</code> indicating
* if the <code>DropTarget</code> is currently accepting drops, and
* a <code>FlavorMap</code> to use (or null for the default <CODE>FlavorMap</CODE>).
* <P>
* The Component will receive drops only if it is enabled.
* @param c The <code>Component</code> with which this <code>DropTarget</code> is associated
* @param ops The default acceptable actions for this <code>DropTarget</code>
* @param dtl The <code>DropTargetListener</code> for this <code>DropTarget</code>
* @param act Is the <code>DropTarget</code> accepting drops.
* @param fm The <code>FlavorMap</code> to use, or null for the default <CODE>FlavorMap</CODE>
* @exception HeadlessException if GraphicsEnvironment.isHeadless()
* returns true
* @see java.awt.GraphicsEnvironment#isHeadless
*/
public DropTarget(Component c, int ops, DropTargetListener dtl,
boolean act, FlavorMap fm)
throws HeadlessException
{
if (GraphicsEnvironment.isHeadless()) {
throw new HeadlessException();
}
component = c;
setDefaultActions(ops);
if (dtl != null) try {
addDropTargetListener(dtl);
} catch (TooManyListenersException tmle) {
// do nothing!
}
if (c != null) {
c.setDropTarget(this);
setActive(act);
}
if (fm != null) {
flavorMap = fm;
} else {
flavorMap = SystemFlavorMap.getDefaultFlavorMap();
}
}
/**
* Creates a new DropTarget given the <code>Component</code>
* to associate itself with, an <code>int</code> representing
* the default acceptable action(s) to
* support, a <code>DropTargetListener</code>
* to handle event processing, a <code>boolean</code> indicating
* if the <code>DropTarget</code> is currently accepting drops, and
* a <code>FlavorMap</code> to use (or null for the default <CODE>FlavorMap</CODE>).
* <P>
* The Component will receive drops only if it is enabled.
* @param c The <code>Component</code> with which this <code>DropTarget</code> is associated
* @param ops The default acceptable actions for this <code>DropTarget</code>
* @param dtl The <code>DropTargetListener</code> for this <code>DropTarget</code>
* @param act Is the <code>DropTarget</code> accepting drops.
* @param fm The <code>FlavorMap</code> to use, or null for the default <CODE>FlavorMap</CODE>
* @exception HeadlessException if GraphicsEnvironment.isHeadless()
* returns true
* @see java.awt.GraphicsEnvironment#isHeadless
*/
public DropTarget(Component c, int ops, DropTargetListener dtl,
boolean act, FlavorMap fm)
throws HeadlessException
{
if (GraphicsEnvironment.isHeadless()) {
throw new HeadlessException();
}
component = c;
setDefaultActions(ops);
if (dtl != null) try {
addDropTargetListener(dtl);
} catch (TooManyListenersException tmle) {
// do nothing!
}
if (c != null) {
c.setDropTarget(this);
setActive(act);
}
if (fm != null) {
flavorMap = fm;
} else {
flavorMap = SystemFlavorMap.getDefaultFlavorMap();
}
}
static Window createInputMethodWindow(String title, InputContext context, boolean isSwing) {
if (GraphicsEnvironment.isHeadless()) {
throw new HeadlessException();
}
if (isSwing) {
return new InputMethodJFrame(title, context);
} else {
Toolkit toolkit = Toolkit.getDefaultToolkit();
if (toolkit instanceof InputMethodSupport) {
return ((InputMethodSupport)toolkit).createInputMethodWindow(
title, context);
}
}
throw new InternalError("Input methods must be supported");
}
static Window createInputMethodWindow(String title, InputContext context, boolean isSwing) {
if (GraphicsEnvironment.isHeadless()) {
throw new HeadlessException();
}
if (isSwing) {
return new InputMethodJFrame(title, context);
} else {
Toolkit toolkit = Toolkit.getDefaultToolkit();
if (toolkit instanceof InputMethodSupport) {
return ((InputMethodSupport)toolkit).createInputMethodWindow(
title, context);
}
}
throw new InternalError("Input methods must be supported");
}
static Window createInputMethodWindow(String title, InputContext context, boolean isSwing) {
if (GraphicsEnvironment.isHeadless()) {
throw new HeadlessException();
}
if (isSwing) {
return new InputMethodJFrame(title, context);
} else {
Toolkit toolkit = Toolkit.getDefaultToolkit();
if (toolkit instanceof InputMethodSupport) {
return ((InputMethodSupport)toolkit).createInputMethodWindow(
title, context);
}
}
throw new InternalError("Input methods must be supported");
}
static public float convertUnitsToPixels(final int unitType, final float value) {
if (unitType == NumberWithUnits.UT_UNITLESS || unitType == NumberWithUnits.UT_PERCENT) { return value; }
float pixPerInch;
try {
pixPerInch = Toolkit.getDefaultToolkit().getScreenResolution();
} catch (final HeadlessException ex) {
// default to 72 dpi
pixPerInch = 72;
}
final float inchesPerCm = .3936f;
switch (unitType) {
case NumberWithUnits.UT_IN:
return value * pixPerInch;
case NumberWithUnits.UT_CM:
return value * inchesPerCm * pixPerInch;
case NumberWithUnits.UT_MM:
return value * .1f * inchesPerCm * pixPerInch;
case NumberWithUnits.UT_PT:
return value * (1f / 72f) * pixPerInch;
case NumberWithUnits.UT_PC:
return value * (1f / 6f) * pixPerInch;
}
return value;
}
private void pasteFromClipboard() {
try {
String data = (String) Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null).getTransferData(DataFlavor.stringFlavor);
List<Village> villages = PluginManager.getSingleton().executeVillageParser(data);
if (!villages.isEmpty()) {
addVillages(villages.toArray(new Village[villages.size()]));
}
} catch (HeadlessException | IOException | UnsupportedFlavorException ignored) {
}
}
static Window createInputMethodWindow(String title, InputContext context, boolean isSwing) {
if (GraphicsEnvironment.isHeadless()) {
throw new HeadlessException();
}
if (isSwing) {
return new InputMethodJFrame(title, context);
} else {
Toolkit toolkit = Toolkit.getDefaultToolkit();
if (toolkit instanceof InputMethodSupport) {
return ((InputMethodSupport)toolkit).createInputMethodWindow(
title, context);
}
}
throw new InternalError("Input methods must be supported");
}
protected SpriteStore() {
try {
gc = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
} catch (HeadlessException e) {
logger.warn("Cannot initialize graphic user interface: " + e);
}
}
/**
* Creates a new DropTarget given the <code>Component</code>
* to associate itself with, an <code>int</code> representing
* the default acceptable action(s) to
* support, a <code>DropTargetListener</code>
* to handle event processing, a <code>boolean</code> indicating
* if the <code>DropTarget</code> is currently accepting drops, and
* a <code>FlavorMap</code> to use (or null for the default <CODE>FlavorMap</CODE>).
* <P>
* The Component will receive drops only if it is enabled.
* @param c The <code>Component</code> with which this <code>DropTarget</code> is associated
* @param ops The default acceptable actions for this <code>DropTarget</code>
* @param dtl The <code>DropTargetListener</code> for this <code>DropTarget</code>
* @param act Is the <code>DropTarget</code> accepting drops.
* @param fm The <code>FlavorMap</code> to use, or null for the default <CODE>FlavorMap</CODE>
* @exception HeadlessException if GraphicsEnvironment.isHeadless()
* returns true
* @see java.awt.GraphicsEnvironment#isHeadless
*/
public DropTarget(Component c, int ops, DropTargetListener dtl,
boolean act, FlavorMap fm)
throws HeadlessException
{
if (GraphicsEnvironment.isHeadless()) {
throw new HeadlessException();
}
component = c;
setDefaultActions(ops);
if (dtl != null) try {
addDropTargetListener(dtl);
} catch (TooManyListenersException tmle) {
// do nothing!
}
if (c != null) {
c.setDropTarget(this);
setActive(act);
}
if (fm != null) {
flavorMap = fm;
} else {
flavorMap = SystemFlavorMap.getDefaultFlavorMap();
}
}
static Window createInputMethodWindow(String title, InputContext context, boolean isSwing) {
if (GraphicsEnvironment.isHeadless()) {
throw new HeadlessException();
}
if (isSwing) {
return new InputMethodJFrame(title, context);
} else {
Toolkit toolkit = Toolkit.getDefaultToolkit();
if (toolkit instanceof InputMethodSupport) {
return ((InputMethodSupport)toolkit).createInputMethodWindow(
title, context);
}
}
throw new InternalError("Input methods must be supported");
}
/**
* Creates a new DropTarget given the <code>Component</code>
* to associate itself with, an <code>int</code> representing
* the default acceptable action(s) to
* support, a <code>DropTargetListener</code>
* to handle event processing, a <code>boolean</code> indicating
* if the <code>DropTarget</code> is currently accepting drops, and
* a <code>FlavorMap</code> to use (or null for the default <CODE>FlavorMap</CODE>).
* <P>
* The Component will receive drops only if it is enabled.
* @param c The <code>Component</code> with which this <code>DropTarget</code> is associated
* @param ops The default acceptable actions for this <code>DropTarget</code>
* @param dtl The <code>DropTargetListener</code> for this <code>DropTarget</code>
* @param act Is the <code>DropTarget</code> accepting drops.
* @param fm The <code>FlavorMap</code> to use, or null for the default <CODE>FlavorMap</CODE>
* @exception HeadlessException if GraphicsEnvironment.isHeadless()
* returns true
* @see java.awt.GraphicsEnvironment#isHeadless
*/
public DropTarget(Component c, int ops, DropTargetListener dtl,
boolean act, FlavorMap fm)
throws HeadlessException
{
if (GraphicsEnvironment.isHeadless()) {
throw new HeadlessException();
}
component = c;
setDefaultActions(ops);
if (dtl != null) try {
addDropTargetListener(dtl);
} catch (TooManyListenersException tmle) {
// do nothing!
}
if (c != null) {
c.setDropTarget(this);
setActive(act);
}
if (fm != null) {
flavorMap = fm;
} else {
flavorMap = SystemFlavorMap.getDefaultFlavorMap();
}
}
@Test
public void whenHeadlessmode_thenFrameThrowsHeadlessException() {
assertThatExceptionOfType(HeadlessException.class).isThrownBy(() -> {
Frame frame = new Frame();
frame.setVisible(true);
frame.setSize(120, 120);
});
}
private void pasteFromClipboard() {
String data = "";
try {
data = (String) Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null).getTransferData(DataFlavor.stringFlavor);
List<Village> villages = PluginManager.getSingleton().executeVillageParser(data);
if (!villages.isEmpty()) {
addVillages(villages.toArray(new Village[villages.size()]));
}
} catch (HeadlessException | IOException | UnsupportedFlavorException ignored) {
}
}
public int getScreenResolution() throws HeadlessException {
return Toolkit.getDefaultToolkit().getScreenResolution();
}
public Clipboard getSystemClipboard() throws HeadlessException {
return Toolkit.getDefaultToolkit().getSystemClipboard();
}
public GraphicsDevice[] getScreenDevices()
throws HeadlessException {
throw new HeadlessException();
}
protected ListPeer createList(java.awt.List target) throws HeadlessException {
throw new IllegalStateException("Method not implemented");
}
public PropertySheetDialog(Frame owner, boolean modal)
throws HeadlessException {
super(owner, modal);
}
public BaseDialog(Frame owner) throws HeadlessException {
super(owner);
buildUI();
}
private static void checkDragEnabled(boolean b) {
if (b && GraphicsEnvironment.isHeadless()) {
throw new HeadlessException();
}
}
public Rectangle getMaximumWindowBounds() throws HeadlessException {
throw new HeadlessException();
}
public void addCertificateAsExempted(CertificateModel certModel) throws HeadlessException, InvalidNameException, KeyStoreException {
addEntryToKeyStoreImpl(certModel, true);
}