-
2021-03-08 16:07:24
import java.io.ByteArrayOutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
class Test {
public static void main(String[] args) {
System.out.println("Default Charset=" + Charset.defaultCharset());
System.out.println("file.encoding=" + System.getProperty("file.encoding"));
System.out.println("Default Charset=" + Charset.defaultCharset());
System.out.println("Default Charset in Use=" + getDefaultCharSet());
}
private static String getDefaultCharSet() {
OutputStreamWriter writer = new OutputStreamWriter(new ByteArrayOutputStream());
String enc = writer.getEncoding();
return enc;
}
}
编译运行这段代码。
更多相关内容 -
如何更改JTextArea内容的字体大小?
2021-07-16 22:05:14I'm trying to write a method that changes a text size inside of the JtextArea.JTextArea editorPanel;Font editorFont;public void setSize( int size ) {editorPanel.setFont( new Font( editorFont.getName()...I'm trying to write a method that changes a text size inside of the JtextArea.
JTextArea editorPanel;
Font editorFont;
public void setSize( int size ) {
editorPanel.setFont( new Font( editorFont.getName(), editorFont.getStyle(), size ) );
}
I have an inner-class ActionListener on another class which look like;
class SizeListener implements ActionListener {
String size;
public void actionPerformed(ActionEvent e) {
JComboBox cb = (JComboBox) e.getSource();
size = (String) cb.getSelectedItem();
int i = Integer.parseInt( size );
displayFont = display.getEditorFont();
display.setSize( i );
}
}
I have implemented this actionlistener to my JComboBox, so that when I choose a new "size" from my ComboBox the text size of the JTextArea should be increased or decreased depending on the choice. Which method or implementation I can use to solve this problem?
解决方案
The easiest way is to derive a new Font based on the existing Font:
JComboBox cb = (JComboBox) e.getSource();
Integer itemSize = (Integer) cb.getSelectedItem();
float fontSize = itemSize.IntValue();
Font font = editorPanel.getFont();
editorPanel.setFont( font.deriveFont( fontSize ) );
Note in this case you would add Integer values to the combo box, not String values so there is no need to parse the selected item.
-
java – 使JTextArea显示固定宽度的字体而不...
2021-03-08 16:07:37虽然它保证所有字符的字体大小相同,但在所有平台上都不一样.import java.awt.Font;import javax.swing.JFrame;import javax.swing.JScrollPane;import javax.swing.JTextArea;import javax.swing.SwingUtilities;...您可以使用逻辑字体“monospaced”.虽然它保证所有字符的字体大小相同,但在所有平台上都不一样.
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
public class TestTextArea {
private void initUI() {
JFrame frame = new JFrame("test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextArea textArea = new JTextArea(24, 80);
textArea.setFont(new Font("monospaced", Font.PLAIN, 12));
frame.add(new JScrollPane(textArea));
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new TestTextArea().initUI();
}
});
}
}
或者,您可以查找满足您需求的“免费”字体,将该字体嵌入代码中并使用java.awt.Font.createFont(int,InputStream)加载它.
-
Jtextarea如何设置不同字体、颜色
2021-03-08 16:07:30// 设置字体大小 fontSizes = new String[63]; for (int i = 0; i ; i++) { fontSizes[i] = Integer.toString((i + 10)); } fontSize = new JComboBox(fontSizes); fontSize.setEditable(false); fontSize....展开全部
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ArtFont extends JFrame implements ActionListener {
JComboBox fontType, fontSize;
JCheckBox boldBx;// 粗体按钮32313133353236313431303231363533e4b893e5b19e31333234333364
JCheckBox italicBx;// 斜体按钮
JButton colorBtn;// 颜色按钮;
String[] fontNames;// 字体名称;
String[] fontSizes;// 字体尺寸;
JLabel label;// 输入提示标签;
JTextField inputText;// 文字输入框;
JTextArea txtArea;// 文字显示区;
JPanel fontPanel;// 字体设置;
JPanel showPanel;// 显示效果区
Font font;
int boldStyle, italicStyle, underlineStyle;
int fontSizeStyle;
String fontNameStyle;
Color colorStyle = Color.black;// 设置字体的默认颜色为黑色;
public ArtFont() {
super("字体设置");
// 设置默认字体
boldStyle = 0;
italicStyle = 0;
underlineStyle = 0;
fontSizeStyle = 10;
fontNameStyle = "宋体";
font = new Font(fontNameStyle, boldStyle + italicStyle, fontSizeStyle);
fontPanel = new JPanel();
fontPanel.setLayout(new FlowLayout());
// 设置字体名字
GraphicsEnvironment ge = GraphicsEnvironment
.getLocalGraphicsEnvironment();
fontNames = ge.getAvailableFontFamilyNames();// 获得系统中所有字体的名字;
fontType = new JComboBox(fontNames);
fontType.setEditable(false);
fontType.setMaximumRowCount(10);
fontType.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
// 实现监听字体名字改变的事件
fontNameStyle = (String) e.getItem();// 程序段1
font = new Font(fontNameStyle, boldStyle + italicStyle,
fontSizeStyle);
txtArea.setFont(font);
}
});
// 设置字体大小
fontSizes = new String[63];
for (int i = 0; i < 63; i++) {
fontSizes[i] = Integer.toString((i + 10));
}
fontSize = new JComboBox(fontSizes);
fontSize.setEditable(false);
fontSize.setMaximumRowCount(10);
fontSize.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
// 实现监听字体大小改变的方法
// 程序段2
fontSizeStyle =Integer.parseInt(e.getItem().toString());
font = new Font(fontNameStyle, boldStyle + italicStyle,
fontSizeStyle);
txtArea.setFont(font);
}
});
// 设置粗体选择按钮;
boldBx = new JCheckBox("粗体");
boldBx.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
// 实现监听选择粗体状态改变的方法
if(e.getStateChange()==1) boldStyle=1;
else boldStyle=0;
font = new Font(fontNameStyle, boldStyle + italicStyle,
fontSizeStyle);
txtArea.setFont(font);// 程序段3
}
});
// 设置斜体选择按钮;
italicBx = new JCheckBox("斜体");
italicBx.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
// 实现监听选择斜体状态改变的方法
if(e.getStateChange()==1) italicStyle=2;
else italicStyle=0;
font = new Font(fontNameStyle, boldStyle + italicStyle,
fontSizeStyle);
txtArea.setFont(font);// 程序段4
}
});
// 设置颜色选择;
colorBtn = new JButton("颜色");
colorBtn.addActionListener(this);
// 设置字体面板;
fontPanel.add(fontType);
fontPanel.add(fontSize);
fontPanel.add(boldBx);
fontPanel.add(italicBx);
fontPanel.add(colorBtn);
// 设置输入提示标签
label = new JLabel("输入");
// 设置文本输入框;
inputText = new JTextField(30);
inputText.addActionListener(this);
// 设置文本显示区;
txtArea = new JTextArea(10, 80);// 20行80列;
txtArea.setFont(font);
// 设置文本面板;
showPanel = new JPanel();
showPanel.add(label);
showPanel.add(inputText);
showPanel.setLayout(new FlowLayout());
showPanel.add(new JScrollPane(txtArea));
// 设置容器;
Container container = getContentPane();
container.setLayout(new BorderLayout());
container.add(fontPanel, BorderLayout.NORTH);
container.add(showPanel, BorderLayout.CENTER);
setSize(500, 300);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == colorBtn) {// 改变颜色
colorStyle = JColorChooser.showDialog(this, "选择字体颜色", colorStyle);
colorBtn.setForeground(colorStyle);
txtArea.setForeground(colorStyle);
} else if (e.getSource() == inputText) {// 将输入文字在文字显示区表示;
txtArea.setText(inputText.getText());
}
}
public static void main(String args[]) {
ArtFont artFont = new ArtFont();
artFont.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
已赞过
已踩过<
你对这个回答的评价是?
评论
收起
-
Java Swing - 如何更改JTextPane的字体大小...
2021-02-28 09:53:50import java.awt.Dimension;import java.awt.FontMetrics;import javax.swing.JFrame;import javax.swing.JScrollPane;import javax.swing.JTextPane;import javax.swing.text.SimpleAttributeSet;... -
java的JTextArea中怎么改变字体颜色
2021-03-09 17:44:27展开全部java swing 中JTEXTAREA不能改变字体颜色,它是纯文本组件,可以使用JTEXTPANE,通过操作DOCUMENT文档来控制JTEXTPANE显示的内容,下面的代码在一个32313133353236313431303231363533e58685e5aeb... -
我该如何调整Java Swing JTextArea的大小
2021-03-06 23:42:10问题是,BoxLayout尊重组件的最大尺寸,这对于滚动窗格来说设置得非常大。不是使用BoxLayout,而是使用带有FlowLayout的面板。运行下面的示例以查看您当前正在执行的操作。然后注释掉setLayout... -
Font 设置字体大小
2021-06-03 19:36:29JTextArea Myarea = new JTextArea(); Myarea.setText("静夜思\n" +"床前明月光,\n" +"疑是地上霜。\n" +"举头望明月,\n" +"低头思故乡。\n" ); Myarea.setBounds(50, 50, 250, 250); Font ... -
java字体设置,包括大小,颜色,加粗,下划线,对齐,斜体的设置
2021-03-08 08:25:31下面的代码告诉我们该怎么在文本编辑器中设置字体大小,颜色,加粗,下划线等许多便捷操作~花了很长的时间找了这么一个资料,真是累煞我了~~!!差点都要放弃了,最后终于在网络中搜索到了这么一段十分有用、十分有... -
Java实训——编写一个窗体程序,能够对文本区中的文字设置字体和大小。
2021-03-15 20:46:49实训要求:编写一个窗体程序,能够对文本区中的文字设置字体和大小。代码:import java.awt.*;import java.awt.event.*;import javax.swing.*;public class FontChange extends JFrame implements ItemListener {... -
Java实训——编写一个窗体程序,可以对文本区中的文字设置字体和大小。
2021-03-08 16:11:29实训要求:编写一个窗体程序,可以对文本区中的文字设置字体和大小。java代码:jspimport java.awt.*;import java.awt.event.*;import javax.swing.*;public class FontChange extends JFrame implements ... -
Swing中字体大小的设置,局部字体大小设置
2012-12-03 17:57:13在Swing中设置字体大小的方法主要分为2种。以JLabel jl=new JLabel();组件为例 第一、根据setFont()属性 Font font=new Font("宋体",Font.BOLD,36); jl.setFont(font); 第二、用兼容的标签 str1="品名:哇... -
java字体设置,包括大小,颜色,加粗,下划线,对齐,斜体的设置 | 学步园
2021-03-05 16:38:53family为字体2、对字体大小的操作MutableAttributeSet attr = new SimpleAttributeSet();StyleConstants.setFontSize(attr, size);setCharacterAttributes(editor, attr, false);size为字号3、是否是粗体的操作... -
java中 JTextArea类的属性和方法
2021-02-26 09:19:55java中JTextArea类的属性和方法(2012-12-11 13:48:13)标签:字体属性方法指示组件JTextArea:void append(String str)将给定文本追加到文档结尾。protected Document createDefaultModel()如果没有显式给出构造时要... -
java swing中的JTextArea边框
2021-02-13 00:15:54有一对夫妇的方式,你可能能够做到这一点,你可以只需将边界后的边界应用于框架或JTextArea或者您可以支持根据您的需求根据您的需要将Border的值转换为任意方法我的选择是考虑使用构建器模式,这将允许您提供您感... -
如何在JEditorPane中设置标签大小?
2021-07-17 01:00:49A JTextArea's tab size can easily be set using setTabSize(int).Is there a similar way to do it with a JEditorPane?Right now, text with tabs in my pane looks like:if (stuff){more stuff;}And, I'd prefer... -
java - JTextArea在java swing中的边框 - 堆栈内存溢出
2021-02-13 00:15:50有几种方法可以实现这一点,你可以将事后的边框应用到框架或JTextArea或者你可以根据你的需要为这两种方法提供Border值我倾向于考虑使用构建器模式,这将允许您提供您感兴趣的属性并生成最终结果。因为许多属性是在... -
文字字体设置窗口
2021-05-15 21:47:481:效果和要求 * 在当在文本框中输入文字后回车,在文本域中... * 当选择字体大小下拉框中的某一字体大小时,文本域中的文字设置为指定的字体大小。 * 当选择窗体样式下拉框中的某一窗体效果时,窗体外观改变为指定 -
java Swing设置文本框文字颜色和字体
2021-03-01 08:28:03在java中文本编辑器单独设置文字的控件有个JTextPane,而通常使用的JTextArea似乎不能设置所选文字的颜色和字体。//实例化一个文本编辑的控件JTextPane editorPane=new JTextPane();//根据所选颜色进行设置... -
SWING开发之JTextArea的的中文显示问题
2013-07-31 21:27:48今天在测试JTextArea的时候发现中文显示的不太正确,后来设置成了宋体,显示是可以显示了,但是有乱码: 在网上找了好久也没有找到解决办法,后来我修改myeclipse的字符编码等等操作均无法解决该问题;这个是... -
Java Swing JTextArea文本区域的实现示例
2021-02-27 22:32:53JTextArea 用来编辑多行的文本。JTextArea 除了允许多行编辑外,其他基本用法和 JTextField 基本一致。JTextArea 常用构造方法:/*** 参数说明:* text: 默认显示的文本* rows: 默认可见的行数* columns: 默认可见的列... -
Java GUI编程(4)---组件中设置字体Font
2020-02-20 14:34:15Font类设置GUI界面的字体样式, 包括字体类型(宋体、仿宋)、字体风格(斜体字、加粗)、 字号大小(默认单位pt)。 构造函数 Font(String familyName,int style,int size) Font f = new Font("仿宋", Font.BOLD+... -
Java Swing图形化编程之JTextArea
2021-12-27 00:08:43概述 JTextArea,文本区域。JTextArea 用来编辑多行的文本。JTextArea 除了允许多行编辑外,其他基本用法和 JTextField 基本一致... * 默认由 rows 和 columns 决定首选大小 */ JTextArea() JTextArea(String text) -
Swing JTextArea类
2021-03-17 11:17:41JTextArea类是一个显示纯文本的多行区域。类声明以下是javax.swing.JTextArea类的声明 -public class JTextAreaextends JTextComponent类构造函数编号构造函数描述1JTextArea()构造一个新的TextArea。2JTextArea... -
swing jtextArea滚动条和文字缩放效果
2021-02-27 16:59:19本文实例为大家分享了swing jtextArea滚动条和文字缩放的具体代码,供大家参考,具体内容如下当加了滚动条的jtextArea添加滚动事件比如缩放ctrl+wheel时,添加的滚动事件和滚动缩放事件会重合,如何让这两个事件同时... -
JAVA程序中有很多JTextField和JTextArea,能否一次性设置全部字体大小和颜色?
2016-11-12 22:40:04程序中有很多个JTextField和JTextArea,能否一次性设置全部字体大小和颜色? 还是只能每个JTextField和JTextArea单独设置?