Toki hai ragione srebbe egoista da parte mia non farlo.
//ho usato i layout
import java.awt.*;
import javax.swing.*;
import javax.swing.colorchooser.*;
import java.awt.event.*;
import java.io.*;
import java.nio.*;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Lorenzo
*/
public class Editor extends JFrame{
JMenuBar barra = new JMenuBar();
JScrollBar barraS = new JScrollBar();
JMenu file = new JMenu();
JMenu Modifica = new JMenu();
JMenuItem item =new JMenuItem();
JMenuItem Salva =new JMenuItem();
JMenuItem Colore =new JMenuItem();
JMenuItem GrandezzaF =new JMenuItem();
JMenuItem Esci =new JMenuItem();
String y;
JScrollPane Scroll = new JScrollPane();
JPanel Pannello = new JPanel();
JTextArea Edit = new JTextArea();
JFileChooser scegli = new JFileChooser();
JFileChooser fileChooser;
JLabel parole = new JLabel();
JButton e = new JButton();
File d;
FileReader v ;
JButton ColoreT = new JButton();
JButton ColoreE = new JButton();
JColorChooser b = new JColorChooser();
JButton accetta = new JButton();
Font fontAttuale =Edit.getFont();
String carattere =fontAttuale.getFamily();
int dimensione=fontAttuale.getSize();
int stile=fontAttuale.getStyle();
// JComboBox box = new JComboBox();
DefaultListModel listaFont=new DefaultListModel();
DefaultComboBoxModel Grandezza=new DefaultComboBoxModel();
JComboBox f = new JComboBox(Grandezza);
DefaultComboBoxModel box=new DefaultComboBoxModel();
JComboBox a = new JComboBox(box);
JList c = new JList(listaFont);
Object ListaT[]={"Normale","Grassetto","Corsivo","Corsivo e Grassetto"};
Object Lista[]={"Arial","Times New Roman","Terminal",
"Comic Sans MS","Calibri","Segoe Print","Consolas","Courier New",
"Fixedsys","Gabriola","Impact","Lucida Sans Unicode","Script",
"Small Fonts"};
boolean t =false;
JMenu Minfo = new JMenu();
JMenuItem info = new JMenuItem();
public void scegliFont(ActionEvent even){
JFrame font = new JFrame();
JList LFont = new JList();
if(t==false){
for(int i=0;i<=13;i++){
listaFont.addElement(Lista[i]);
}
for(int i=0;i<=3;i++){
box.addElement(ListaT[i]);
}
int v = 0;
for(int i=0;i<72;i++){
v++;
Grandezza.addElement(v);
}
}
JPanel r = new JPanel();
JPanel g = new JPanel();
JPanel PannelloC = new JPanel();
JPanel j = new JPanel();
//r.setLayout(new FlowLayout(FlowLayout.TRAILING,50,100));
PannelloC.setLayout(new FlowLayout(FlowLayout.RIGHT));
PannelloC.add(accetta);
j.setBounds(150,30, 300, 80);
g.setBounds(150, 0, 200, 30);
r.setBounds(0, 0, 150, 300);
f.getFont().getSize();
j.add(f);
g.add(a);
r.add(c);
accetta.setText("Accetta");
font.setTitle("Scegli Font");
font.add(g);
font.add(r);
font.add(j);
font.add(PannelloC);
font.setSize(450, 400);
font.setVisible(true);
}
public void scegliColore(ActionEvent even){
JFrame a = new JFrame();
a.setTitle("Tabella Colori");
a.setBounds(0, 15, 450,450);
//a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
a.setVisible(true);
a.setLocationRelativeTo(null);
ColoreT.setSize(20,50);
b.setVisible(true);
JPanel m = new JPanel();
ColoreT.setText("Cambia Colore Testo");
ColoreE.setText("Cambia Colore Sfondo");
m.setLayout(new FlowLayout(FlowLayout.RIGHT,10,10));
m.add(ColoreT);
m.add(ColoreE);
b.add(m,BorderLayout.SOUTH);
a.add(b,BorderLayout.NORTH);
// a.show();
//a.setVisible(true);
// Edit.setBackground(x);
}
public void Leggi(ActionEvent even) throws FileNotFoundException, IOException{
scegli.showOpenDialog(this);
scegli.getSelectedFile();
d = scegli.getSelectedFile();
d.getPath();
v = new FileReader(new File(d.getPath()));
Edit.read(v, null);
}
public void Salva(ActionEvent even) throws IOException{
scegli.showSaveDialog(this);
File f = scegli.getSelectedFile();
f.getName();
try (BufferedWriter write = new BufferedWriter(new FileWriter(f))) {
write.append(Edit.getText());
write.flush();
}
}
public void Mostrarighe(ActionEvent even){
parole.setText(Edit.getText().length()+"");
}
Editor(){
barra.setBounds(0, 0, 1280, 30);
file.setText("File");
item.setText("Apri File");
Salva.setText("Salva File");
Esci.setText("Esci");
file.add(item);
file.add(Salva);
file.add(Esci);
Minfo.setText("Help");
info.setText("Informazioni");
Minfo.add(info);
Colore.setText("Seleziona Colore");
GrandezzaF.setText("Seleziona Font");
Modifica.add(Colore);
Modifica.add(GrandezzaF);
Modifica.setText("Modifica");
barra.add(file);
barra.add(Modifica);
e.setText("Mostra numero lettere");
barra.add(Minfo);
barra.add(e);
barra.add(parole);
Edit.setSize(700,550);
Edit.setLineWrap(true);
Edit.setFont(new Font ("Italic",20,16));
Edit.setVisible(true);
add(Edit,BorderLayout.CENTER);
add(barra,BorderLayout.NORTH);
item.addActionListener(new ActionListener (){
@Override
public void actionPerformed(ActionEvent e) {
try {
Leggi(e);
} catch (FileNotFoundException ex) {
Logger.getLogger(Editor.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Editor.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
Salva.addActionListener(new ActionListener (){
@Override
public void actionPerformed(ActionEvent e) {
try {
Salva(e);
} catch (FileNotFoundException ex) {
Logger.getLogger(Editor.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Editor.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
Esci.addActionListener(new ActionListener (){
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
Colore.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
scegliColore(e);
}
});
ColoreT.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
Edit.setForeground(b.getColor());
}
});
ColoreE.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
Edit.setBackground(b.getColor());
}
});
GrandezzaF.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
scegliFont(e);
t=true;
}
});
e.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
Mostrarighe(e);
}
});
accetta.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
String d = (String) c.getSelectedValue();
int y = a.getSelectedIndex();
int b = f.getSelectedIndex();
Edit.setFont(new Font(d,y,b));
}
});
info.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane f = new JOptionPane();
String i="Ciao,sono Lorenzo e questo è il mio editor "
+ "\nvi saranno sicuramente dei bug"
+ "\nquindi ti invito a segnalarmeli"
+ "\nqui: lorenzollc09@gmail.com ";
JOptionPane.showMessageDialog(null, i);
}
});
}
public static void main(String args[]){
Editor window = new Editor();
window.setTitle("Editor++ by l.l.");
window.setVisible(true);
window.setLocationRelativeTo(null);
window.setSize(new Dimension(700,550));
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// window.setMaximumSize(null);
}
}