Witam, w wolnych chwilach, ćwiczę sobie pisanie i pracę z NetBeans7(świetna rzecz ) wyprodukowałem kod nawet działa, chodzi o to że wszystkie zmiany czcionki, koloru itd zachodzą dla całego EditorPane a nie powinno tak być, jak zmodyfikować kod by to zmienić? Z góry dziękuje wszystkim sympatykom Javy za pomoc.Sam zabieram się za poszukiwania.
package edytor;
import java.awt.;// nie potrzba definiowac szczegolow
import java.awt.event.;
//import java.awt.Font;
import javax.swing.;
import javax.swing.JEditorPane;
//import java.awt.Color ;
//import java.awt.Dimension ;
import java.io.*;
//import javax.swing.text.StyleConstants;
//import java.io.FileReader;
//import java.io.OutputStream;
//import java.lang.Object;
// javax.swing.text.EditorKit;
//import javax.swing.JFileChooser;
//import javax.swing.JOptionPane;
//import javax.swing.text.StyledEditorKit;
/**
*
-
@Author Dawid
*/
public class NewJFrame extends javax.swing.JFrame {
int rozmiar;
String czcionka;
String s;/** 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() {
bindingGroup = new org.jdesktop.beansbinding.BindingGroup();jLabel2 = new javax.swing.JLabel();
jComboBox2 = new javax.swing.JComboBox();
jLabel1 = new javax.swing.JLabel();
jComboBox1 = new javax.swing.JComboBox();
jLabel3 = new javax.swing.JLabel();
jComboBox3 = new javax.swing.JComboBox();
jFileChooser1 = new javax.swing.JFileChooser();
jCheckBox3 = new javax.swing.JCheckBox();
jCheckBox2 = new javax.swing.JCheckBox();
jCheckBox1 = new javax.swing.JCheckBox();
jScrollPane1 = new javax.swing.JScrollPane();
jEditorPane1 = new javax.swing.JEditorPane();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
jMenuItem1 = new javax.swing.JMenuItem();
jMenuItem6 = new javax.swing.JMenuItem();
jMenuItem2 = new javax.swing.JMenuItem();
jMenuItem5 = new javax.swing.JMenuItem();
jMenu2 = new javax.swing.JMenu();
jMenuItem4 = new javax.swing.JMenuItem();
jMenuItem3 = new javax.swing.JMenuItem();
jMenuItem7 = new javax.swing.JMenuItem();setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel2.setText("Czcionka");
org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, jMenu1, org.jdesktop.beansbinding.ELProperty.create("${icon.iconWidth}"), jLabel2, org.jdesktop.beansbinding.BeanProperty.create("icon"));
bindingGroup.addBinding(binding);jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Serif", "SansSerif", "Monospaced", "Symbol", "Thoma" }));
jComboBox2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jComboBox2ActionPerformed(evt);
}
});jLabel1.setText("Wielkość");
jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" }));
jComboBox1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jComboBox1ActionPerformed(evt);
}
});jLabel3.setText("Kolor");
jComboBox3.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "czerwony", "zielony", "niebieski", "czarny" }));
jComboBox3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jComboBox3ActionPerformed(evt);
}
});jFileChooser1.setControlButtonsAreShown(false);
jFileChooser1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jFileChooser1ActionPerformed(evt);
}
});jCheckBox3.setText("Kursywa");
jCheckBox3.addMouseListener(new java.awt.event.MouseAdapter() {
public void mousePressed(java.awt.event.MouseEvent evt) {
jCheckBox3MousePressed(evt);
}
public void mouseReleased(java.awt.event.MouseEvent evt) {
jCheckBox3MouseReleased(evt);
}
});
jCheckBox3.addChangeListener(new javax.swing.event.ChangeListener() {
public void stateChanged(javax.swing.event.ChangeEvent evt) {
jCheckBox3StateChanged(evt);
}
});
jCheckBox3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jCheckBox3ActionPerformed(evt);
}
});
jCheckBox3.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
public void propertyChange(java.beans.PropertyChangeEvent evt) {
jCheckBox3PropertyChange(evt);
}
});jCheckBox2.setText("Zwykła");
jCheckBox2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jCheckBox2ActionPerformed(evt);
}
});jCheckBox1.setText("Pogrubiona");
jCheckBox1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jCheckBox1ActionPerformed(evt);
}
});jScrollPane1.setViewportView(jEditorPane1);
jMenuBar1.setMaximumSize(new java.awt.Dimension(60, 319));
jMenu1.setText("Pliki");
jMenuItem1.setText("Nowy");
jMenuItem1.setPreferredSize(new java.awt.Dimension(107, 20));
jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem1ActionPerformed(evt);
}
});
jMenu1.add(jMenuItem1);jMenuItem6.setText("Otwórz");
jMenuItem6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem6ActionPerformed(evt);
}
});
jMenu1.add(jMenuItem6);jMenuItem2.setText("Zapisz");
jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem2ActionPerformed(evt);
}
});
jMenu1.add(jMenuItem2);jMenuItem5.setText("Wyjście");
jMenuItem5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem5ActionPerformed(evt);
}
});
jMenu1.add(jMenuItem5);jMenuBar1.add(jMenu1);
jMenu2.setText("Edycja");
jMenuItem4.setText("Kopiuj text");
jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem4ActionPerformed(evt);
}
});
jMenu2.add(jMenuItem4);jMenuItem3.setText("Wytnij");
jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem3ActionPerformed(evt);
}
});
jMenu2.add(jMenuItem3);jMenuItem7.setText("Wklej");
jMenuItem7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem7ActionPerformed(evt);
}
});
jMenu2.add(jMenuItem7);jMenuBar1.add(jMenu2);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel2)
.addGap(18, 18, 18)
.addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jCheckBox1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jCheckBox3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jCheckBox2)
.addGap(12, 12, 12)
.addComponent(jLabel3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jComboBox3, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(104, 104, 104)
.addComponent(jFileChooser1, javax.swing.GroupLayout.PREFERRED_SIZE, 6, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 706, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jFileChooser1, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jCheckBox2)
.addComponent(jCheckBox1)
.addComponent(jCheckBox3)
.addComponent(jComboBox3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3))))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 358, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(53, Short.MAX_VALUE))
);bindingGroup.bind();
pack();
}// </editor-fold>
private void jComboBox2ActionPerformed(java.awt.event.ActionEvent evt) {
// String czcionka;//int getSize(); // java.awt.Dimension jEitorPane1 = null;
-
//int tryb = 0;
// faceCombo.addItem("Serif");
// faceCombo.addItem("SansSerif");
// faceCombo.addItem("Monospaced");
// faceCombo.addItem("Dialog");
// faceCombo.addItem("DialogInput");
// int nrIndCombo2=jComboBox2.getSelectedIndex();
// if(nrIndCombo2==0 ){ jEditorPane1.setFont(new Font("Serif", Font.PLAIN, 18)); }
// if(nrIndCombo2==1 ){ jEditorPane1.setFont(new Font("SansSerif", Font.PLAIN, 18)); }
// if(nrIndCombo2==2 ){ jEditorPane1.setFont(new Font("Monospaced",Font.PLAIN, 18)); }
// if(nrIndCombo2==3 ){ jEditorPane1.setFont(new Font("Dialog", Font.PLAIN, 18)); }
//// switch (jComboBox1.getSelectedIndex())
rozmiar = 14 ;
switch (jComboBox2.getSelectedIndex()) {
case 0: czcionka="Serif"; break;
case 1: czcionka="SansSerif"; break;
case 2: czcionka="Monospaced"; break;
case 3: czcionka="Dialog"; break;
case 4: czcionka="Arial"; break;
} // jEditorPane1.setFont(new Font(czcionka,Font.PLAIN,rozmiar));
// jEditorPane1.setFont(new Font("czcionka",Font.BOLD,14));
jEditorPane1.setFont(new java.awt.Font(czcionka,java.awt.Font.PLAIN,rozmiar));
}
private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {
switch (jComboBox1.getSelectedIndex()){
case 0: rozmiar=14; break;
case 1: rozmiar=18; break;
case 2: rozmiar=22; break;
case 3: rozmiar=26; break;
case 4: rozmiar=30; break;}
jEditorPane1.setFont(new java.awt.Font(czcionka,java.awt.Font.PLAIN,rozmiar));
//String spole=jEditorPane1.getText();
// public Color getForeground() ;
// EditorPane1 textPane = new EditorPane1();
// StyledDocument doc = textPane.getStyledDocument();
// int tryb = 0;
// int nrIndCombo1=jComboBox1.getSelectedIndex();
// if(nrIndCombo1==0 ){ jEditorPane1.setFont(new java.awt.Font(" ", java.awt.Font.PLAIN, 8)); }
// if(nrIndCombo1==1 ){ jEditorPane1.setFont(new java.awt.Font(" ", java.awt.Font.PLAIN, 10)); }
// if(nrIndCombo1==2 ){ jEditorPane1.setFont(new java.awt.Font(" ", java.awt.Font.PLAIN, 12)); }
// if(nrIndCombo1==3 ){ jEditorPane1.setFont(new java.awt.Font(" ", java.awt.Font.PLAIN, 16)); }
// if(nrIndCombo1==4 ){ jEditorPane1.setFont(new java.awt.Font(" ", java.awt.Font.PLAIN, 20)); }
// if(nrIndCombo1==5 ){ jEditorPane1.setFont(new java.awt.Font(" ", java.awt.Font.PLAIN, 24)); }
-
*if(nrIndCombo1==3 ){ jEditorPane1.StyleConstants.setFontSize(attrs, font.getSize(18)); } //if(nrIndCombo1== 4 ){ jEditorPane1.AttributeSet(11); } //if(nrIndCombo1==3){jEditorPane1.setSize(11);}
}
private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {
jEditorPane1.setText("");
}
private void jMenuItem6ActionPerformed(java.awt.event.ActionEvent evt) {
int returnVal = jFileChooser1.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = jFileChooser1.getSelectedFile();
try {
jEditorPane1.read( new FileReader( file.getAbsolutePath() ), null );
} catch (Exception ex) {
System.out.println("problem accessing file"+file.getAbsolutePath());
}
} else {
System.out.println("File access cancelled by user.");
}
}
private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {
// try {
// int returnVal = jFileChooser1.showOpenDialog(this);
-
* if (returnVal == JFileChooser.APPROVE_OPTION) {
// File file = jFileChooser1.getSelectedFile();
//
//}
//catch(Exception e) { System.err.println("Could not save page: "); }
int returnVal = jFileChooser1.showSaveDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = jFileChooser1.getSelectedFile();
try {
FileWriter fw = new FileWriter(file);
jEditorPane1.write(fw);
fw.close();
} catch (Exception ex) {
System.out.println("problem accessing file"+file.getAbsolutePath());
}
} else {
System.out.println("File access cancelled by user.");
}
// int returnVal = jFileChooser1.showSaveDialog(this);
//if (returnVal == JFileChooser.APPROVE_OPTION) {
//File file = jFileChooser1.getSelectedFile();
*FileOutputStream.write(file); * save the file.
}
private void jMenuItem5ActionPerformed(java.awt.event.ActionEvent evt) {
int wynik;
wynik = JOptionPane.showConfirmDialog(this,
"Czy na pewno zakończyć działanie programu?",
"Pytanie",
JOptionPane.YES_NO_OPTION);
if (wynik == JOptionPane.YES_OPTION)
setVisible(false);
dispose();
*System.exit(0); *zamyka aplikacje
}
private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {
cutText(); // wycina text odwolanie do metody
}
private void jComboBox3ActionPerformed(java.awt.event.ActionEvent evt) {
//String spole=jEditorPane1.getText();
// public Color getForeground() ;
int nrIndCombo1=jComboBox3.getSelectedIndex();
if(nrIndCombo1==0 ){ jEditorPane1.setForeground(Color.RED); }
if(nrIndCombo1==1 ){ jEditorPane1.setForeground(Color.green); }
if(nrIndCombo1==2 ){ jEditorPane1.setForeground(Color.blue); }
if(nrIndCombo1==3 ){ jEditorPane1.setForeground(Color.black); }
}
private void jFileChooser1ActionPerformed(java.awt.event.ActionEvent evt) {
// int returnVal = jFileChooser1.showOpenDialog(this);
// if (returnVal == JFileChooser.APPROVE_OPTION) {
// File file = jFileChooser1.getSelectedFile();
// try {
//
// jEditorPane1.read( new FileReader( file.getAbsolutePath() ), null );
//
// } catch (Exception ex) {
// System.out.println("problem accessing file"+file.getAbsolutePath());
// }
// } else {
// System.out.println("File access cancelled by user.");
// }
}
private void jCheckBox3ActionPerformed(java.awt.event.ActionEvent evt) {
jEditorPane1.setFont(new java.awt.Font("Dialog", java.awt.Font.ITALIC, 12));
}
private void jCheckBox2ActionPerformed(java.awt.event.ActionEvent evt) {
jEditorPane1.setFont(new java.awt.Font("Dialog", java.awt.Font.PLAIN, 12));
}
private void jCheckBox1ActionPerformed(java.awt.event.ActionEvent evt) {
// jEditorPane1 poletekstowe;
//
// poletekstowe = new jEditorPane1();
jEditorPane1.setFont(new java.awt.Font("SansSerif", java.awt.Font.BOLD, 12));
// jEditorPane1.setFont("Font.BOLD");
}
public void copyText() { jEditorPane1.copy(); }
public void cutText() { jEditorPane1.cut(); } //metody copiju, wklej, wytnij
public void pasteText() {jEditorPane1.paste(); }
private void jCheckBox3StateChanged(javax.swing.event.ChangeEvent evt) {
}
private void jCheckBox3PropertyChange(java.beans.PropertyChangeEvent evt) {
// TODO add your handling code here:
}
private void jCheckBox3MousePressed(java.awt.event.MouseEvent evt) {
// jEditorPane1.setFont(new java.awt.Font("Dialog", java.awt.Font.ITALIC, 12));
}
private void jCheckBox3MouseReleased(java.awt.event.MouseEvent evt) {
// jEditorPane1.setFont(new java.awt.Font("Dialog", java.awt.Font.PLAIN, 12));
}
private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {
copyText();
}
private void jMenuItem7ActionPerformed(java.awt.event.ActionEvent evt) {
pasteText();
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
JEditorPane poletekstowe;
// JEditorPane doc = poletekstowe.getDocument();
//doc.insertString(doc.getLength(), "Tekst\n", centeredBlackText);
public void run() {
new NewJFrame().setVisible(true);
}
});
}