我正在制作一个有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);
}
}
答案 0 :(得分:1)
您无需将style1-4硬编码到侦听器中。你看到你是如何获得行动的来源,但你从未使用它?该源是触发此事件的单选按钮对象。您可以在单选按钮上获取值的标签。然后根据刚刚触发事件的单选按钮的标签值设置字体。此外,您可能希望将其拆分为两个事件处理程序,每组一个单选按钮。使其更易于阅读,更容易辨别哪个集被点击。
答案 1 :(得分:1)
而不是
saying.setFont(new Font (font, style, 36));
你的actionPerformed方法中的试试这个:
saying.setFont(new JLabel().getFont()); // get the default font from a new JLabel
this.repaint();
所以你想要显示的字体似乎有问题。字体实际上在你的机器上吗?
System.out.println(new Font("Arial",style,36).getFamily());
实际打印出Arial还是......别的?