salve,
stavo provando a catturare lo stato di una Jcheckbox alla premuta di un bottone, provvisoriamente stampando sulla console un messaggio di test, ma non lo cattura.
una volta premuto il pulsante Registra dovrebbe catturarmi lo stato della Jcheckbox cass2 e controllare se il valore di ritorno è TRUE o FALSE.
codice:package Xpar.gui; import javax.swing.*; import java.awt.*; import java.awt.event.*; import net.java.dev.designgridlayout.*; import java.io.*; import java.text.SimpleDateFormat; import javax.swing.filechooser.FileNameExtensionFilter; import java.util.Date; import java.util.Locale; /** * * @author andy */ public class XPAR { JFrame frame=new JFrame(); JPanel panelUno=new JPanel(); DesignGridLayout layout=new DesignGridLayout(panelUno); JLabel nomeFile=new JLabel("Nome File:"); JTextField campoNomeFile=new JTextField(); JButton sfogliaFile=new JButton("..."); JLabel dimCarLabel=new JLabel("Dimensione Caratteri:"); JTextField dimCaratteri=new JTextField(); JLabel altRigheLabel=new JLabel("Altezza Righe:"); JTextField altRighe=new JTextField(); JButton altIncremento=new JButton("+"); JButton altDecremento=new JButton("-"); JButton dimDecremento=new JButton("-"); JButton dimIncremento=new JButton("+"); JLabel cassettoLabel=new JLabel("Cassetto:"); JCheckBox cass2=new JCheckBox("2"); JCheckBox cass3=new JCheckBox("3"); JLabel tipoCartaLabel=new JLabel("Tipo Carta:"); JCheckBox formatoA3=new JCheckBox("A3"); JCheckBox formatoA4=new JCheckBox("A4"); JLabel orientamentoLabel=new JLabel("Orientamento:"); JCheckBox orizz=new JCheckBox("Orizzontale(Landscape)"); JCheckBox vert=new JCheckBox("Verticale(Portrait)"); JLabel grassettoLabel=new JLabel("Grassetto:"); JCheckBox grassetto_ChkBx=new JCheckBox("Grassetto"); JButton registra_btn=new JButton("Registra"); JButton cambiaFile_btn=new JButton("Cambia File"); JButton test_btn=new JButton("Test"); JButton esci_btn=new JButton("Esci"); int flagCassetto; private int flagFormatoCarta; private int flagOrientamento; SimpleDateFormat formatter = new SimpleDateFormat("dd/MMMM/yyyy - hh:mm aaa",Locale.ITALY); Date today = new Date(); String result=formatter.format(today); public XPAR() { frame.getContentPane(); frame.add(panelUno); frame.setTitle("XPAR"); panelUno.setPreferredSize(new Dimension(600,600)); frame.setVisible(true); frame.pack(); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE); layout.row().grid().add(nomeFile).add(campoNomeFile).add(sfogliaFile); layout.row().grid().add(dimCarLabel).add(dimCaratteri).add(dimIncremento).add(dimDecremento); layout.row().grid().add(altRigheLabel).add(altRighe).add(altIncremento).add(altDecremento); layout.row().grid().add(cassettoLabel).add(cass2).add(cass3); layout.row().grid().add(tipoCartaLabel).add(formatoA3).add(formatoA4); layout.row().grid().add(orientamentoLabel).add(orizz).add(vert); layout.row().grid().add(grassettoLabel).add(grassetto_ChkBx); layout.row().grid().add(registra_btn,cambiaFile_btn,test_btn,esci_btn); esci_btn.addActionListener(new ExitHandler()); sfogliaFile.addActionListener(new FileBrowseHandler()); registra_btn.addActionListener(new RegistroDati()); cambiaFile_btn.addActionListener(new CambioFile()); } public static void main(String arg[]) { XPAR x=new XPAR(); } public class RegistroDati implements ActionListener,ItemListener { public void actionPerformed(ActionEvent e) { try { String cattura= "cassetto selezionato:"+flagCassetto+"\n"+ "Tipo Carta: "+flagFormatoCarta+"\n" +"Orientamento: "+flagOrientamento+"\n"+ "Data: "+result; String xPath="C:"+File.separator+"gpi"+File.separator+"par"+File.separator+"current.psx"; File f=new File(xPath); FileOutputStream fos=new FileOutputStream(xPath); byte buf[]=cattura.getBytes(); fos.write(buf); fos.close(); } catch(Exception ex) { ex.printStackTrace(); } } public void itemStateChanged(ItemEvent ie) { Object source=ie.getItem(); if (source==cass2){ if(cass2.isSelected()){ System.out.print("ciao"); } } } } class CambioFile implements ActionListener { public void actionPerformed(ActionEvent e) { JOptionPane jop=new JOptionPane(); //Object [] option={"Salvare","Non Salvare"}; int n = JOptionPane.showConfirmDialog(frame, "Vuoi salvare il file aperto precedentemente?", "Salvataggio", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); if (n==0) { JFileChooser c=new JFileChooser(); FileNameExtensionFilter filter=new FileNameExtensionFilter("psx Files", "*.psx"); c.setFileFilter(filter); int rVal=c.showSaveDialog(panelUno); if (rVal==JFileChooser.APPROVE_OPTION) { } } } } public class ExitHandler implements ActionListener { public void actionPerformed(ActionEvent e) { JOptionPane jop=new JOptionPane(); int n = JOptionPane.showConfirmDialog(frame, "Vuoi salvare il file aperto precedentemente?", "Salvataggio", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); if (n==0) { // n=0 equivale al tasto Si JFileChooser c=new JFileChooser(); FileNameExtensionFilter filter=new FileNameExtensionFilter("psx Files", "*.psx"); c.setFileFilter(filter); int rVal=c.showSaveDialog(panelUno); if (rVal==JFileChooser.APPROVE_OPTION) { new RegistroDati(); } } else { System.exit(0); } } } public class FileBrowseHandler implements ActionListener { public void actionPerformed(ActionEvent e) { File f=new File("C:"+File.separator+"gpi"+File.separator+"par"); String file=f.getAbsolutePath(); /* JFileChooser jfc=new JFileChooser(f); JDialog jd=new JDialog(); */ FileDialog fd=new FileDialog(frame,"Apri", FileDialog.LOAD); //FileNameExtensionFilter filter=new FileNameExtensionFilter("psx files", "psx"); fd.setDirectory(file); fd.setLocation(100, 100); fd.setVisible(true); /* jfc.setFileFilter(filter); if(f.exists()) jfc.showSaveDialog(null); //jfc.showOpenDialog(null); * */ } } }

Rispondi quotando

