İletişim kutusunu kullanıcıya görüntülemek için PrinterJob.printDialog(attributes)
'u kullanan yeni Java yazdırma API'sini kullanıyoruz. , Ne yaparak bulundu bazen (ı çözemedim nasıl henüz) olmasıdır AncakBir Swing uygulamasında yazıcı ayarlarını sürdürmenin iyi bir yolu var mı?
PrintRequestAttributeSet attributes = loadAttributesFromPreferences();
if (printJob.printDialog(attributes)) {
// print, and then...
saveAttributesToPreferences(attributes);
}
: sonraki sefer için kullanıcı ayarları kaydetmek isteyen
, bunu yapmak istedim öznitelikler içeride bazı kötü veriler alır ve yazdırdığınızda, hiçbir şeyden beyaz bir sayfa alırsınız. Ardından kod zehirli ayarları tercihlere kaydeder ve tüm sonraki yazdırma işlemleri de zehirli ayarlara neden olur. Ek olarak, alıştırmanın tüm noktasını, yeni çalışma için ayarları önceki çalıştırma için kullanıcı ile aynı olan, yeniliyor, çünkü yeni iletişim kutusu eski ayarları kullanmak için görünmüyor.
Bunu yapmak için uygun bir yol olup olmadığını bilmek istiyorum. Surely Sun, uygulamanın her başlatılışında kullanıcıların yazıcıyı, sayfa boyutunu, yönünü ve kenar boşluğu ayarlarını seçmesi gerektiğini düşünmüyordu.
Düzenleme depolama yöntemlerinin uygulanmasını göstermek için:
private PrintRequestAttributeSet loadAttributesFromPreferences()
{
PrintRequestAttributeSet attributes = null;
byte[] marshaledAttributes = preferences.getByteArray(PRINT_REQUEST_ATTRIBUTES_KEY, null);
if (marshaledAttributes != null)
{
try
{
@SuppressWarnings({"IOResourceOpenedButNotSafelyClosed"})
ObjectInput objectInput = new ObjectInputStream(new ByteArrayInputStream(marshaledAttributes));
attributes = (PrintRequestAttributeSet) objectInput.readObject();
}
catch (IOException e)
{
// Can occur due to invalid object data e.g. InvalidClassException, StreamCorruptedException
Logger.getLogger(getClass()).warn("Error trying to read print attributes from preferences", e);
}
catch (ClassNotFoundException e)
{
Logger.getLogger(getClass()).warn("Class not found trying to read print attributes from preferences", e);
}
}
if (attributes == null)
{
attributes = new HashPrintRequestAttributeSet();
}
return attributes;
}
private void saveAttributesToPreferences(PrintRequestAttributeSet attributes)
{
ByteArrayOutputStream storage = new ByteArrayOutputStream();
try
{
ObjectOutput objectOutput = new ObjectOutputStream(storage);
try
{
objectOutput.writeObject(attributes);
}
finally
{
objectOutput.close(); // side-effect of flushing the underlying stream
}
}
catch (IOException e)
{
throw new IllegalStateException("I/O error writing to a stream going to a byte array", e);
}
preferences.putByteArray(PRINT_REQUEST_ATTRIBUTES_KEY, storage.toByteArray());
}
Düzenleme: Tamam, o yazıcıyı hatırlayarak değil sebep gibi görünüyor o PrintRequestAttributeSet içinde olmadığıdır hiç Gerçekten de, en azından ayarlar rastgele zehirlenene kadar kenar boşlukları ve sayfa boyutları hatırlanır. Ama kullanıcı tarafından seçilen yazıcı burada değil: ne arıyorsanız yerine PrintRequestAttributeSet
daha PrintServiceAttributeSet olduğu görülüyor
[0] = {[email protected]} class javax.print.attribute.standard.Media -> na-letter
[1] = {[email protected]} class javax.print.attribute.standard.Copies -> 1
[2] = {[email protected]} class javax.print.attribute.standard.MediaPrintableArea -> (10.0,10.0)->(195.9,259.4)mm
[3] = {[email protected]} class javax.print.attribute.standard.OrientationRequested -> portrait
'saveAttributesToPreferences()' işlevinin doğru çalıştığını doğruladınız mı? – trashgod
'Doğru' tanımlayın. Bu bayt dizisine serileştirir ve bunları tercihlere göre saklar, ancak bunun doğru olup olmadığı bu sorunun cevabına bağlıdır. – Trejkaz