Я делаю рамку из 3 панелей. На 1 панели есть текст, на 1 есть переключатели, чтобы изменить шрифт, на котором появляется поговорка, а на последней панели есть переключатели, чтобы изменить размер шрифта. У меня все сделано, но я не совсем понимаю, как добавить слушатель действия для переключателей размера. Кто-нибудь может мне с этим помочь? Код для ActionListener находится внизу. Большое спасибо!
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Layouts extends JPanel implements ActionListener
{
int style = Font.PLAIN;
String font = "Arial";
private JLabel saying, overAll, top, sizeP, fontP;
private JPanel panel1, panel2, panel3, panel4;
private JRadioButton style1, style2, style3, style4, size1, size2, size3, size4;
//-----------------------------------------------------------------
// Sets up a panel with a label and some check boxes that
// control the style of the label's font.
//-----------------------------------------------------------------
public Layouts()
{
style1 = new JRadioButton ("Arial", true);
style2 = new JRadioButton ("Times New Roman", false);
style3 = new JRadioButton ("Verdana", false);
style4 = new JRadioButton ("Thonburi", false);
size1 = new JRadioButton ("36", false);
size2 = new JRadioButton ("24", false);
size3 = new JRadioButton ("20", false);
size4 = new JRadioButton ("16", true);
panel1 = new JPanel();
panel2 = new JPanel();
panel3 = new JPanel();
saying = new JLabel ("Say it with style!");
saying.setFont (new Font ("Helvetica", Font.PLAIN, 36)); // we'll need this later
ButtonGroup group = new ButtonGroup(); //use this code for the homework
group.add (style1);
group.add (style2);
group.add (style3);
group.add (style4);
style1.addActionListener (this);
style2.addActionListener (this);
style3.addActionListener (this);
style4.addActionListener (this);
ButtonGroup group2 = new ButtonGroup();
group.add(size1);
group.add(size2);
group.add(size3);
group.add(size4);
size1.addActionListener (this);
size2.addActionListener (this);
size3.addActionListener (this);
size4.addActionListener (this);
setBackground (Color.red);
setPreferredSize (new Dimension(500, 100));
setLayout(new BorderLayout());
add(panel1, BorderLayout.NORTH);
add(panel2, BorderLayout.EAST);
add(panel3, BorderLayout.WEST);
//add(panel2, East);
//add(panel3, West);
panel1.add(saying);
panel1.setBackground(Color.yellow);
panel2.setLayout(new GridLayout());
panel2.setBackground(Color.red);
panel2.add(style1);
panel2.add(style2);
panel2.add(style3);
panel2.add(style4);
panel3.setLayout(new BoxLayout(panel3, BoxLayout.Y_AXIS));
panel3.setBackground(Color.red);
panel3.add(size1);
panel3.add(size2);
panel3.add(size3);
panel3.add(size4);
}
//*****************************************************************
// Represents the listener for both check boxes and the radio boxes.
//*****************************************************************
public void actionPerformed(ActionEvent e) // this is our bread and butter, it is basically what we will be changing
{
Object source = e.getSource();
if (style1.isSelected())
font = "Arial";
else
if (style2.isSelected())
font = "Times New Roman";
else
if (style3.isSelected())
font = "Verdana";
else
if (style4.isSelected())
font = "Thonburi";
saying.setFont(new Font (font, style, 36));
}
public static void main (String[] args)
{
JFrame frame = new JFrame ("Layouts");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
Layouts panel = new Layouts();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}