To com o seguinte problema:
tenho um textarea , e 3 checkboxmenuitem definidos como negrito , italico , sublinhado.Gostaria de saber como eu faço , para q quando os tre estiverem marcados o texto , ficar negrito , italico e sublinhado , ao mesmo tempo.
desde já gradço!!
Acho que este exemplo pode te ajudar:
[code]import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JTextField;
/**
-
@author aeeduart
-
-
Para alterar o gabarito para este comentário do tipo gerado vá para
-
Janela>Preferências>Java>Geração de Códigos>Código e Comentários
*/
public class FrameTeste extends JFrame implements ActionListener {
private javax.swing.JPanel jContentPane = null;
private JTextField cpfField = null;
private javax.swing.JTextArea jTextArea = null;
private javax.swing.JCheckBox jChkItalico = null;
private javax.swing.JCheckBox jChkNegrito = null;
public static void main(String[] args) {
FrameTeste teste = new FrameTeste();
teste.show();
}
/**
-
This is the default constructor
/
public FrameTeste() {
super();
initialize();
}
/*
-
This method initializes this
-
-
@return void
/
private void initialize() {
this.setSize(351, 267);
this.setContentPane(getJContentPane());
}
/*
-
This method initializes jContentPane
-
-
@return javax.swing.JPanel
/
private javax.swing.JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new javax.swing.JPanel();
jContentPane.setLayout(null);
jContentPane.add(getJTextArea(), null);
jContentPane.add(getJChkItalico(), null);
jContentPane.add(getJChkNegrito(), null);
}
return jContentPane;
}
/*
-
This method initializes jTextArea
-
-
@return javax.swing.JTextArea
/
private javax.swing.JTextArea getJTextArea() {
if (jTextArea == null) {
jTextArea = new javax.swing.JTextArea();
jTextArea.setBounds(35, 87, 216, 115);
}
return jTextArea;
}
/*
-
This method initializes jCheckBox
-
-
@return javax.swing.JCheckBox
/
private javax.swing.JCheckBox getJChkItalico() {
if (jChkItalico == null) {
jChkItalico = new javax.swing.JCheckBox();
jChkItalico.setBounds(38, 53, 93, 21);
jChkItalico.setText(“Italico”);
jChkItalico.addActionListener(this);
}
return jChkItalico;
}
/*
-
This method initializes jCheckBox1
-
-
@return javax.swing.JCheckBox
/
private javax.swing.JCheckBox getJChkNegrito() {
if (jChkNegrito == null) {
jChkNegrito = new javax.swing.JCheckBox();
jChkNegrito.setBounds(155, 54, 95, 21);
jChkNegrito.setText(“Negrito”);
jChkNegrito.addActionListener(this);
}
return jChkNegrito;
}
/ (não-Javadoc)
-
@see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
public void actionPerformed(ActionEvent e) {
if (getJChkItalico().isSelected() && getJChkNegrito().isSelected()) {
jTextArea.setFont(
new java.awt.Font(
jTextArea.getFont().getName(),
Font.ITALIC | Font.BOLD,
jTextArea.getFont().getSize()));
} else if (getJChkItalico().isSelected()) {
jTextArea.setFont(
new java.awt.Font(
jTextArea.getFont().getName(),
Font.ITALIC,
jTextArea.getFont().getSize()));
} else if (getJChkNegrito().isSelected()) {
jTextArea.setFont(
new java.awt.Font(
jTextArea.getFont().getName(),
Font.BOLD,
jTextArea.getFont().getSize()));
} else {
jTextArea.setFont(
new java.awt.Font(
jTextArea.getFont().getName(),
Font.PLAIN,
jTextArea.getFont().getSize()));
}
}
}
[/code]
Desculpe-me não tive tempo de comentar o código, mas está funcionando.