是的,语言规范定义结果为“ 2”。如果VM采取不同的方式,则不符合规范。
大多数编译器都会对此抱怨。以Eclipse为例,它将声称永远不会执行return块,但这是错误的。
编写这样的代码我需要知道如何执行此操作:
假设:我有这样的代码JTextArea:
JTextArea
LOAD R1, 1 DEC R1 STORE M, R1 ADD R4, R1,8
我想改变的颜色LOAD,DEC,STORE并ADD以颜色为蓝色R1,R4颜色绿色 M到红色的数字橙色
LOAD,DEC,STORE
ADD
R1,R4
如何更改此文字的颜色?这些文本来自记事本,也可以直接在文本区域中键入。是非常糟糕的做法,永远不要这样做:)
JTextArea是为了娱乐Plain Text。应用于单个字符的设置适用于中的整个文档JTextArea。但随着JTextPane或JEditorPane你有选择,而影响了你String Literals按照自己的喜好。在JTextPane的帮助下,您可以这样操作:
Plain Text
JTextPane
JEditorPane
String Literals
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; import javax.swing.text.AttributeSet; import javax.swing.text.SimpleAttributeSet; import javax.swing.text.StyleConstants; import javax.swing.text.StyleContext; public class TextPaneTest extends JFrame { private JPanel topPanel; private JTextPane tPane; public TextPaneTest() { topPanel = new JPanel(); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); EmptyBorder eb = new EmptyBorder(new Insets(10, 10, 10, 10)); tPane = new JTextPane(); tPane.setBorder(eb); //tPane.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY)); tPane.setMargin(new Insets(5, 5, 5, 5)); topPanel.add(tPane); appendToPane(tPane, "My Name is Too Good.\n", Color.RED); appendToPane(tPane, "I wish I could be ONE of THE BEST on ", Color.BLUE); appendToPane(tPane, "Stack", Color.DARK_GRAY); appendToPane(tPane, "Over", Color.MAGENTA); appendToPane(tPane, "flow", Color.ORANGE); getContentPane().add(topPanel); pack(); setVisible(true); } private void appendToPane(JTextPane tp, String msg, Color c) { StyleContext sc = StyleContext.getDefaultStyleContext(); AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, c); aset = sc.addAttribute(aset, StyleConstants.FontFamily, "Lucida Console"); aset = sc.addAttribute(aset, StyleConstants.Alignment, StyleConstants.ALIGN_JUSTIFIED); int len = tp.getDocument().getLength(); tp.setCaretPosition(len); tp.setCharacterAttributes(aset, false); tp.replaceSelection(msg); } public static void main(String... args) { SwingUtilities.invokeLater(new Runnable() { public void run() { new TextPaneTest(); } }); } }