Agora essa é a classe que vc vai rodar o programa, mas um detalhe, so que essa é um jdialog, e não meu Main. pois essa classe faz parte de um projeto que estou desenvolvendo.
import javax.swing.JPanel;
import java.awt.Frame;
import javax.swing.JDialog;
import java.awt.Dimension;
import javax.swing.JScrollPane;
import java.awt.Rectangle;
import javax.swing.JTextArea;
import javax.swing.BorderFactory;
import javax.swing.border.BevelBorder;
import javax.swing.border.TitledBorder;
import java.awt.SystemColor;
import java.awt.Font;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import java.awt.GridLayout;
/**
- Engenharia da Computação
- Descrição:
- Data: 25/08/2007
*/
/**
*/
public class Serial extends JDialog
{
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JScrollPane jScrollPane = null;
private JScrollPane jScrollPane1 = null;
public static JTextArea textAreaEnviados = null;
public static JTextArea textAreaRecebidos = null;
private JButton btnRecebidos = null;
private JButton btnEnviados = null;
private JPanel panelStatus = null;
private JScrollPane jScrollPane2 = null;
public static JTextArea status = null;
/**
* @param owner
*/
public Serial(Frame owner)
{
super(owner);
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize()
{
this.setSize(442, 378);
this.setTitle("..:: Comunicação Serial ::..");
this.setModal(true);
this.setContentPane(getJContentPane());
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane()
{
if (jContentPane == null)
{
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(getJScrollPane(), null);
jContentPane.add(getJScrollPane1(), null);
jContentPane.add(getBtnRecebidos(), null);
jContentPane.add(getBtnEnviados(), null);
jContentPane.add(getPanelStatus(), null);
}
return jContentPane;
}
/**
* This method initializes jScrollPane
*
* @return javax.swing.JScrollPane
*/
private JScrollPane getJScrollPane()
{
if (jScrollPane == null)
{
jScrollPane = new JScrollPane();
jScrollPane.setBorder(BorderFactory.createTitledBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED), "Dados Recebidos", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, new Font("Dialog", Font.BOLD, 12), SystemColor.activeCaption));
jScrollPane.setLocation(new Point(6, 7));
jScrollPane.setSize(new Dimension(422, 109));
jScrollPane.setViewportView(getTextAreaRecebidos());
}
return jScrollPane;
}
/**
* This method initializes jScrollPane1
*
* @return javax.swing.JScrollPane
*/
private JScrollPane getJScrollPane1()
{
if (jScrollPane1 == null)
{
jScrollPane1 = new JScrollPane();
jScrollPane1.setBorder(BorderFactory.createTitledBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED), "Dados Enviados", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, new Font("Dialog", Font.BOLD, 12), SystemColor.activeCaption));
jScrollPane1.setLocation(new Point(8, 146));
jScrollPane1.setSize(new Dimension(421, 108));
jScrollPane1.setViewportView(getTextAreaEnviados());
}
return jScrollPane1;
}
/**
* This method initializes textAreaEnviados
*
* @return javax.swing.JTextArea
*/
private JTextArea getTextAreaEnviados()
{
if (textAreaEnviados == null)
{
textAreaEnviados = new JTextArea();
textAreaEnviados.setSize(new Dimension(333, 66));
}
return textAreaEnviados;
}
/**
* This method initializes textAreaRecebidos
*
* @return javax.swing.JTextArea
*/
private JTextArea getTextAreaRecebidos()
{
if (textAreaRecebidos == null)
{
textAreaRecebidos = new JTextArea();
textAreaRecebidos.setSize(new Dimension(333, 66));
}
return textAreaRecebidos;
}
/**
* This method initializes btnRecebidos
*
* @return javax.swing.JButton
*/
private JButton getBtnRecebidos()
{
if (btnRecebidos == null)
{
btnRecebidos = new JButton();
btnRecebidos.setBounds(new Rectangle(339, 120, 89, 19));
btnRecebidos.setText("Receber");
btnRecebidos.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
limpa();
SerialCom sm = new SerialCom();
if (sm.PortaExiste("COM1"))
{
textAreaRecebidos.append("Iniciando comunicação"+"\n");
SComm scom = new SComm("COM1",9600,2000);
scom.HabilitarLeitura();
scom.ObterIdDaPorta();
scom.AbrirPorta();
scom.LerDados();
textAreaRecebidos.append(scom.Dadoslidos+"\n");
scom.FecharCom();
}
}
}
);
}
return btnRecebidos;
}
/**
* This method initializes btnEnviados
*
* @return javax.swing.JButton
*/
private JButton getBtnEnviados()
{
if (btnEnviados == null)
{
btnEnviados = new JButton();
btnEnviados.setLocation(new Point(340, 258));
btnEnviados.setText("Enviar");
btnEnviados.setSize(new Dimension(89, 19));
btnEnviados.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
limpa();
SerialCom st = new SerialCom();
if ( st.PortaExiste("COM1") == true)
{
Serial.status.append("Iniciando Comunicação"+"\n");
SComm sc = new SComm("COM1",9600,2000);
sc.HabilitarEscrita();
sc.ObterIdDaPorta();
sc.AbrirPorta();
sc.EnviarUmaString(textAreaEnviados.getText());
sc.FecharCom();
}
}
}
);
}
return btnEnviados;
}
/**
* This method initializes panelStatus
*
* @return javax.swing.JPanel
*/
private JPanel getPanelStatus()
{
if (panelStatus == null)
{
GridLayout gridLayout = new GridLayout();
gridLayout.setRows(1);
panelStatus = new JPanel();
panelStatus.setLayout(gridLayout);
panelStatus.setBounds(new Rectangle(7, 288, 422, 56));
panelStatus.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
panelStatus.add(getJScrollPane2(), null);
}
return panelStatus;
}
/**
* This method initializes jScrollPane2
*
* @return javax.swing.JScrollPane
*/
private JScrollPane getJScrollPane2()
{
if (jScrollPane2 == null)
{
jScrollPane2 = new JScrollPane();
jScrollPane2.setViewportView(getStatus());
}
return jScrollPane2;
}
/**
* This method initializes status
*
* @return javax.swing.JTextArea
*/
private JTextArea getStatus()
{
if (status == null)
{
status = new JTextArea();
status.setFont(new Font("Dialog", Font.PLAIN, 10));
}
return status;
}
public void limpa()
{
status.setText("");
}
} // @jve:decl-index=0:visual-constraint=“30,0”