Swing应用程序中的Jni字体错误

时间:2011-08-11 07:54:43

标签: swing java-native-interface jtextpane java

我收到此随机Jni错误,有时代码有效,有时它不会

    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.Font;
    import java.awt.GraphicsEnvironment;

    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTextPane;
    import javax.swing.text.BadLocationException;
    import javax.swing.text.MutableAttributeSet;
    import javax.swing.text.StyleConstants;
    import javax.swing.text.StyledDocument;

    public class Fonts {
        public static void main(String[] args) {
            Fonts fs = new Fonts();
            try {
                fs.initialize();
            } catch (Exception e) {
                e.printStackTrace();
            }
            fs.frm.setVisible(true);
        }

        private String[] fnt;
        private JFrame frm;
        private JScrollPane jsp;
        private JTextPane jta;
        private int width = 450;
        private int height = 300;
        private GraphicsEnvironment ge = GraphicsEnvironment
                .getLocalGraphicsEnvironment();
        private Font[] fnts;
        private StyledDocument doc;
        private MutableAttributeSet mas;
        // private String[] fams;

        private int cp = 0;

        public Fonts() {
        }

        public void dis(String s) {
            try {
                doc.insertString(cp, s, mas);
                doc.insertString(cp, "\n", mas);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        public void initialize() throws BadLocationException {
            frm = new JFrame("awesome");
            frm.setMinimumSize(new Dimension(width, height));
            frm.setBounds(100, 100, width, height);
            frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frm.getContentPane().setLayout(new BorderLayout());

            fnts = ge.getAllFonts();
            jta = new JTextPane();
            doc = jta.getStyledDocument();

            jsp = new JScrollPane(jta);
            frm.getContentPane().add(jsp, BorderLayout.CENTER);

            frm.pack();

            fnt = ge.getAvailableFontFamilyNames();

            mas = jta.getInputAttributes();
            for (int i = 0; i < fnt.length; i++) {
                StyleConstants.setBold(mas, false);
                StyleConstants.setItalic(mas, false);
                StyleConstants.setFontFamily(mas, fnt[i]);
                StyleConstants.setFontSize(mas, 16);
                dis(fnt[i]);
                StyleConstants.setBold(mas, true);
                dis(fnt[i] + " Bold");
                StyleConstants.setItalic(mas, true);
                dis(fnt[i] + " Bold & Italic");
                StyleConstants.setBold(mas, false);
                dis(fnt[i] + " Italic");
            }
        }

    }

这是我得到的错误。

    #
    # A fatal error has been detected by the Java Runtime Environment:
    #
    #  SIGSEGV (0xb) at pc=0xb3fdad10, pid=20482, tid=3066784624
    #
    # JRE version: 6.0_26-b03
    # Java VM: Java HotSpot(TM) Client VM (20.1-b02 mixed mode, sharing linux-x86 )
    # Problematic frame:
    # C  [libfontmanager.so+0x2ed10]  float+0x40
    #
    # An error report file with more information is saved as:
    # /home/alex/repos/java-alex.fonts/bin/hs_err_pid20482.log
    #
    # If you would like to submit a bug report, please visit:
    #   http://java.sun.com/webapps/bugreport/crash.jsp
    # The crash happened outside the Java Virtual Machine in native code.
    # See problematic frame for where to report the bug.
    #
    Aborted

1 个答案:

答案 0 :(得分:2)

添加了importans /标准Swing rulles

  • 更改了主要方法

  • 将JFrame的所有方法从toop移到方法的末尾,

  • PrefferedSize设置为JScrollPane

然后例如

import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;

public class Fonts {

    private String[] fnt;
    private JFrame frm;
    private JScrollPane jsp;
    private JTextPane jta;
    private int width = 450;
    private int height = 300;
    private GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    private Font[] fnts;
    private StyledDocument doc;
    private MutableAttributeSet mas;
    // private String[] fams;
    private int cp = 0;

    public Fonts() {
        jta = new JTextPane();
        doc = jta.getStyledDocument();
        jsp = new JScrollPane(jta);
        jsp.setPreferredSize(new Dimension(height, width));
        fnt = ge.getAvailableFontFamilyNames();
        mas = jta.getInputAttributes();
        for (int i = 0; i < fnt.length; i++) {
            StyleConstants.setBold(mas, false);
            StyleConstants.setItalic(mas, false);
            StyleConstants.setFontFamily(mas, fnt[i]);
            StyleConstants.setFontSize(mas, 16);
            dis(fnt[i]);
            StyleConstants.setBold(mas, true);
            dis(fnt[i] + " Bold");
            StyleConstants.setItalic(mas, true);
            dis(fnt[i] + " Bold & Italic");
            StyleConstants.setBold(mas, false);
            dis(fnt[i] + " Italic");
        }
        frm = new JFrame("awesome");
        frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frm.setLayout(new BorderLayout());
        frm.add(jsp, BorderLayout.CENTER);
        frm.setLocation(100, 100);
        frm.pack();
        frm.setVisible(true);
    }

    private void dis(String s) {
        try {
            doc.insertString(cp, s, mas);
            doc.insertString(cp, "\n", mas);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                Fonts fs = new Fonts();
            }
        });
    }
}
  • 您必须更改将Font添加到JTextPane的逻辑,Z - > A的订单错误(只是我的直升机视图)

编辑:并改变了访问/可见性(@ by attn trashgod)

public void dis(String s) {...

private void dis(String s) {...