我正在製作3個面板的框架。 1面板上有一些文字,1有單選按鈕可以改變字詞出現的字體,最後一個面板有單選按鈕來改變字體的大小。我已經完成了一切,但我對如何爲大小單選按鈕添加動作偵聽器感到困惑。誰能幫我這個? actionlistener的代碼接近底部。非常感謝你!如何爲2組單選按鈕添加動作列表
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);
}
}
有沒有一種方法可以使用actionListener通過擴展我現有的if else語句來更改文本的大小?在看到答覆後,我認爲這就是我應該如何去做的,我還沒有足夠了解一些被稱爲能夠做到這一點的事情。 – Josh 2010-11-04 14:42:55