当我使用JRadioButton设置JTextArea的字体时,我陷入了困境,我只是设置了一个组,设置了粗体和斜体文本。而且,当我输入int编号时,我无法设置JTextArea中接收JTextField的文本大小。我的DoExit方法不起作用。我已经使用JComboBox设置了更改文本的颜色,但是我无法设置大小和样式。
import javax.swing.JFrame;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
/**
*
* @author anmanh
*/
public class BTH2 extends JFrame implements ActionListener{
JLabel jlTiitle,jlND, jlColor, jlStyle, jlSize;
JTextField jtxSize;
JTextArea jtaText;
JComboBox jcbColor;
JRadioButton jrbBold,jrbItalic;
JButton jbtClear, jbtExit ;
ButtonGroup btn;
public BTH2(){
this.setSize(800, 250);
this.setTitle("Test 3");
this.setVisible(true);
Font f1 =new Font("Arial", Font.BOLD, 14);
Font f2 =new Font("Arial", Font.ITALIC, 14);
jlTiitle = new JLabel("Custom text");
jlTiitle.setFont(new Font("Time New Roman", Font.BOLD, 20));
jlTiitle.setForeground(Color.BLUE);
jlND = new JLabel("Enter any text: ");
jlND.setFont(new Font("Arial", Font.BOLD, 14));
jlND.setForeground(Color.black);
jlColor = new JLabel("Select color: ");
jlColor.setFont(new Font("Arial", Font.BOLD, 14));
jlColor.setForeground(Color.black);
jlStyle = new JLabel("Select font: ");
jlStyle.setFont(new Font("Arial", Font.BOLD, 14));
jlStyle.setForeground(Color.black);
jlSize = new JLabel("Enter text size: ");
jlSize.setFont(new Font("Arial", Font.BOLD, 14));
jlSize.setForeground(Color.black);
jtaText = new JTextArea("Enter any text ");
jtaText.setPreferredSize(new Dimension(180, 160));
jtaText.setFont(f1);
jtxSize = new JTextField();
jtxSize.setColumns(10);
jtxSize.setFont(f1);
jcbColor = new JComboBox();
jcbColor.setFont(f1);
jcbColor.addItem(" Mau Do ");
jcbColor.addItem(" Mau Xanh ");
jcbColor.addItem(" Mau Den ");
jcbColor.addItem(" Mau Vang ");
jcbColor.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
if (jcbColor.getSelectedIndex() == 0) {
jtaText.setForeground(Color.RED);
}
if (jcbColor.getSelectedIndex() == 1) {
jtaText.setForeground(Color.GREEN);
}
if (jcbColor.getSelectedIndex() == 2) {
jtaText.setForeground(Color.BLACK);
}
if (jcbColor.getSelectedIndex() == 3) {
jtaText.setForeground(Color.YELLOW);
}
}
});
jbtClear = new JButton("Clear");
jbtClear.setFont(f2);
jbtClear.setForeground(Color.BLACK);
jbtClear.setBackground(Color.gray);
jbtClear.addActionListener(this);
jbtExit = new JButton("Thoat");
jbtExit.setFont(f2);
jbtExit.setForeground(Color.BLACK);
jbtExit.setBackground(Color.gray);
jbtClear.addActionListener(this);
jrbBold = new JRadioButton("Chu dam");
jrbBold.setFont(f1);
jrbItalic = new JRadioButton("Chu nghieng");
jrbItalic.setFont(f2);
btn = new ButtonGroup();
btn.add(jrbBold);
btn.add(jrbItalic);
if(jrbBold.isSelected()){
Font font1 = new Font("Arial", Font.BOLD, 20);
jtaText.setFont(font1);
// jtaText.setFont(jtaText.getText(), Font.BOLD);
}
if(jrbItalic.isSelected()){
Font font2 = new Font("Arial", Font.ITALIC, 20);
jtaText.setFont(font2);
}
JPanel pNorth = new JPanel();
pNorth.add(jlTiitle);
this.add(pNorth, BorderLayout.NORTH);
JPanel pSouth = new JPanel();
pSouth.setLayout(new FlowLayout());
pSouth.add(jbtClear);
pSouth.add(jbtExit);
this.add(pSouth, BorderLayout.SOUTH);
JPanel pWest = new JPanel();
pWest.setLayout(new GridLayout(1, 2));
pWest.add(jlND);
pWest.add(jtaText);
this.add(pWest, BorderLayout.WEST);
JPanel pEast = new JPanel();
pEast.setLayout(new GridLayout(3, 2));
pEast.add(jlColor);
pEast.add(jcbColor);
pEast.add(jlStyle);
JPanel j1 = new JPanel();
j1.setLayout(new FlowLayout());
j1.add(jrbBold);
j1.add(jrbItalic);
pEast.add(j1);
pEast.add(jlSize);
pEast.add(jtxSize);
this.add(pEast, BorderLayout.EAST);
//Set size Jtextarea from jTextfield
jtaText.setFont(jtxSize.getFont());
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == jbtClear) {
doClear();
}
if (e.getSource() == jbtExit) {
doExit();
}
}
void doClear(){
jtaText.setText(" ");
jtxSize.setText(" ");
}
void doExit() {
if (JOptionPane.showConfirmDialog(null, "Do you want to exit?",
"Notify", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
System.exit(0);
}
}
void checkInput(){
//Chech empty
if (jtxSize.getText().equals("")) {
JOptionPane.showMessageDialog(null, "Ban chua nhap
size","Error",JOptionPane.OK_OPTION);
return;
}
//check type of input
if (!isNumber(jtxSize.getText(), 0)) {
JOptionPane.showMessageDialog(null, "Ban chua nhap size la
Integer","Error",JOptionPane.OK_OPTION);
return;
}
}
boolean isNumber(String number, int type){
try{
if (type == 0) {
int n = Integer.parseInt(number);
}else{
double n = Double.parseDouble(number);
}
} catch (Exception e){
return false;
}
return true;
}
}