Czcionki w Swingu – problem
Chyba nie do obejścia.
Listing 1. Stacktrace
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 4
at sun.font.FontDesignMetrics.charsWidth(Unknown Source)
at javax.swing.text.Utilities.getTabbedTextOffset(Unknown Source)
at javax.swing.text.Utilities.getTabbedTextOffset(Unknown Source)
at javax.swing.text.Utilities.getTabbedTextOffset(Unknown Source)
at javax.swing.text.PlainView.viewToModel(Unknown Source)
at javax.swing.text.FieldView.viewToModel(Unknown Source)
at javax.swing.plaf.basic.BasicTextUI$RootView.viewToModel(Unknown Source)
at javax.swing.plaf.basic.BasicTextUI.viewToModel(Unknown Source)
at javax.swing.text.DefaultCaret.moveCaret(Unknown Source)
at javax.swing.text.DefaultCaret.mouseDragged(Unknown Source)
at java.awt.AWTEventMulticaster.mouseDragged(Unknown Source)
at java.awt.Component.processMouseMotionEvent(Unknown Source)
at javax.swing.JComponent.processMouseMotionEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Kod:
Listing 2. Kod
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class Test extends JPanel {
private static final long serialVersionUID = 1L;
public Test() {
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
JTextField tf = new JTextField();
tf.setBounds(100, 100, 100, 30);
add(tf);
}
});
} catch (Exception exc) {
System.out.println("Can't create because of " + exc);
}
}
public static void main(String[] args) {
JFrame frame = new JFrame();
Test panel = new Test();
frame.getContentPane().add(panel);
panel.setLayout(new GridLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(600, 300, 200, 200);
frame.setVisible(true);
}
}
Przepis na:
Skompiluj, uruchom, wpisz 12345, zaznacz mychą 34 i naciśnij spację. Masz 12 5. Zaznacz mychą od lewej strony tekst.
WTF?
Kod jest dostępny na licencji MIT. Jednak niektóre rozwiązania mogą być objęte inną licencją. W takim przypadku jest, to zaznaczone. Artykuły są dostępne na licencji CC-BY.
Jeżeli spodobał ci się ten wpis, to podziel się nim z innymi lub wesprzyj autora.