Java 如何以编写JTextArea的确切方式将JTextArea发送给另一个JTextArea
输出 我试图通过对我在第一个Jtextarea上写的某个特定值保持相同的样式(粗体、斜体…),将Jtextarea的相同值发送给另一个Jtextarea。但每当我发送到另一个JtextArea时,所有的值都具有相同的样式Java 如何以编写JTextArea的确切方式将JTextArea发送给另一个JTextArea,java,swing,Java,Swing,输出 我试图通过对我在第一个Jtextarea上写的某个特定值保持相同的样式(粗体、斜体…),将Jtextarea的相同值发送给另一个Jtextarea。但每当我发送到另一个JtextArea时,所有的值都具有相同的样式 /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templat
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication13;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
/**
*
* @author abdoulhalimabdallah
*/
public class NewJFrame extends javax.swing.JFrame {
/**
* Creates new form NewJFrame
*/
public NewJFrame() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
lol = new javax.swing.JTextPane();
jButton1 = new javax.swing.JButton();
jScrollPane2 = new javax.swing.JScrollPane();
lol1 = new javax.swing.JTextPane();
jButton2 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
lol.setFont(new java.awt.Font("Avenir Next", 0, 24)); // NOI18N
lol.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyTyped(java.awt.event.KeyEvent evt) {
lolKeyTyped(evt);
}
});
jScrollPane1.setViewportView(lol);
jButton1.setText("Bold");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
lol1.setFont(new java.awt.Font("Avenir Next", 0, 24)); // NOI18N
jScrollPane2.setViewportView(lol1);
jButton2.setText("Send");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(211, 211, 211)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 420, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 420, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(layout.createSequentialGroup()
.addGap(368, 368, 368)
.addComponent(jButton2)))
.addContainerGap(217, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(37, 37, 37)
.addComponent(jButton1)
.addGap(18, 18, 18)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 192, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 13, Short.MAX_VALUE)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 192, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18))
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
SimpleAttributeSet attributeSet = new SimpleAttributeSet();
StyleConstants.setBold(attributeSet, true);
lol.setCharacterAttributes(attributeSet, true);
lol.requestFocusInWindow();
}
private void lolKeyTyped(java.awt.event.KeyEvent evt) {
// TODO add your handling code here:
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
lol1.setText(lol.getText());
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTextPane lol;
private javax.swing.JTextPane lol1;
// End of variables declaration
}
/*
*要更改此许可证标题,请在“项目属性”中选择“许可证标题”。
*要更改此模板文件,请选择工具|模板
*然后在编辑器中打开模板。
*/
包javaapplication13;
导入javax.swing.text.SimpleAttributeSet;
导入javax.swing.text.StyleConstants;
/**
*
*@作者Abdoulhalimabdalah
*/
公共类NewJFrame扩展了javax.swing.JFrame{
/**
*创建新表单NewJFrame
*/
公共NewJFrame(){
初始化组件();
}
/**
*从构造函数中调用此方法来初始化表单。
*警告:不要修改此代码。此方法的内容始终为
*由表单编辑器重新生成。
*/
@抑制警告(“未选中”)
//
私有组件(){
jScrollPane1=newjavax.swing.JScrollPane();
lol=newjavax.swing.JTextPane();
jButton1=newjavax.swing.JButton();
jScrollPane2=newjavax.swing.JScrollPane();
lol1=newjavax.swing.JTextPane();
jButton2=newjavax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
lol.setFont(新的java.awt.Font(“Avenir Next”,0,24));//NOI18N
lol.addKeyListener(新的java.awt.event.KeyAdapter(){
public void keyTyped(java.awt.event.KeyEvent evt){
lolKeyTyped(evt);
}
});
jScrollPane1.setViewportView(lol);
jButton1.setText(“粗体”);
jButton1.addActionListener(新java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent evt){
jButton1ActionPerformed(evt);
}
});
lol1.setFont(新的java.awt.Font(“Avenir Next”,0,24));//NOI18N
jScrollPane2.setViewportView(lol1);
jButton2.setText(“发送”);
jButton2.addActionListener(新java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent evt){
jButton2ActionPerformed(evt);
}
});
javax.swing.GroupLayout=newjavax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(布局);
layout.setHorizontalGroup(
createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(211211211211)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton1,javax.swing.GroupLayout.PREFERRED_SIZE,89,javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jScrollPane2,javax.swing.GroupLayout.PREFERRED_SIZE,420,javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jScrollPane1,javax.swing.GroupLayout.PREFERRED_SIZE,420,javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(layout.createSequentialGroup()
.addGap(368368368)
.addComponent(jButton2)))
.addContainerGap(217,简称最大值))
);
layout.setVerticalGroup(
createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,layout.createSequentialGroup()
.addGap(37,37,37)
.addComponent(jButton1)
.addGap(18,18,18)
.addComponent(jScrollPane1,javax.swing.GroupLayout.PREFERRED\u SIZE,192,javax.swing.GroupLayout.PREFERRED\u SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,13,Short.MAX_值)
.addComponent(jScrollPane2,javax.swing.GroupLayout.PREFERRED\u SIZE,192,javax.swing.GroupLayout.PREFERRED\u SIZE)
.addGap(18、18、18))
);
包装();
}//
私有void jButton1ActionPerformed(java.awt.event.ActionEvent evt){
//TODO在此处添加您的处理代码:
SimpleAttributeSet attributeSet=新的SimpleAttributeSet();
StyleConstants.setBold(attributeSet,true);
lol.setCharacterAttributes(attributeSet,true);
lol.requestFocusInWindow();
}
私有void lolKeyTyped(java.awt.event.KeyEvent evt){
//TODO在此处添加您的处理代码:
}
私有void jButton2ActionPerformed(java.awt.event.ActionEvent evt){
//TODO在此处添加您的处理代码:
lol1.setText(lol.getText());
}
/**
*@param指定命令行参数
*/
公共静态void main(字符串参数[]){
/*设置Nimbus的外观和感觉*/
//
/*如果Nimbus(在JavaSE6中引入)不可用,请使用默认的外观。
*详情请参阅http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
试一试{
for(javax.swing.UIManager.LookAndFeelInfo:javax.swing.UIManager.getInstalledLookAndFeels()){
if(“Nimbus”.equals(info.getName())){