Java面板字体更改单选按钮问题

发布于 2024-09-27 19:05:29 字数 4494 浏览 3 评论 0原文

由于我的动作监听器,我在完成这项任务时遇到了麻烦。现在,面板有 2 个复选框,可将字体更改为粗体和/或斜体。我想添加 4 个单选按钮,可以更改“说法”的字体。谁能告诉我我的动作监听器做错了什么?谢谢你!

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class StyleOptionsPanel extends JPanel implements ActionListener, ItemListener
{
private JLabel saying;
private JCheckBox bold, italic; // have a button for style1, button for style2, button for style3
private JRadioButton style1, style2, style3, style4; //creates the buttons for each of the 4 styles
{
    bold = new JCheckBox ("Bold", true);
    italic = new JCheckBox ("Italic", true);

    style1 = new JRadioButton ("Arial", true);
    style2 = new JRadioButton ("Times New Roman", false);
    style3 = new JRadioButton ("Verdana", false);
    style4 = new JRadioButton ("Thonburi", false);

    //also check out quote options panel file for example of radio buttons.
    //after creating your radio buttons, you have to create a button group. copy line exactly from quote options panel
    //comedy = new JRadioButton ("Comedy", true); - example of assigning radio button
    //comedy.setBackground (Color.green);
    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);

    add (style1);
    add (style2);
    add (style3);
    add (style4);
}
//-----------------------------------------------------------------
//  Sets up a panel with a label and some check boxes that
//  control the style of the label's font.
//-----------------------------------------------------------------
public StyleOptionsPanel()
{
    saying = new JLabel ("Say it with style!");
    saying.setFont (new Font ("Helvetica", Font.PLAIN, 36)); // we'll need this later

    bold = new JCheckBox ("Bold"); // what is in quotes doesn't have to match variable being created but it should be descriptive
    bold.setBackground (Color.green); // you can also set the background color of the checkbox
    italic = new JCheckBox ("Italic");
    italic.setBackground (Color.green); // this doesn't seem to change anything for me?

    //StyleListener listener = new StyleListener(); //whenever you create a textbox, you have to add a listener for it
    //bold.addActionListener (listener);
    //italic.addActionListener (listener);

    add (saying); //it matters what order these go in, it affects the way that your program looks
    add (bold);
    add (italic);
    add (style1);
    add (style2);
    add (style3);
    add (style4);

    setBackground (Color.cyan);
    setPreferredSize (new Dimension(300, 100));
}

//*****************************************************************
//  Represents the listener for both check boxes and the radio boxes.
//*****************************************************************
private class StyleListener implements ActionListener, ItemListener
{
    //--------------------------------------------------------------
    //  Updates the style of the label font style.
    //--------------------------------------------------------------
    public void itemStateChanged(ItemEvent e) 
    {
        int style = Font.PLAIN;

        if (bold.isSelected()) //this is a boolean statement, on the inside you will always expect some kind of boolean statement
            style = Font.BOLD;

        if (italic.isSelected())
            style += Font.ITALIC; // style += means that it takes whatever style the font already is and then adds the value Italic to it
        //public void actionPerformed (ActionEvent event) //also use this code in homework

        Object source = e.getSource();// TODO Auto-generated method stub

    }

    public void actionPerformed(ActionEvent e) // this is our bread and butter, it is basically what we will be changing
    {


        if (source == style1) //if (source == style1)
            saying.setText (Arial); //quote.setText(Helvetica)
        else (source == style2)
        saying.setText (TimesNewRoman);
        else (source == style3)
        saying.setText (Verdana);
        else (source == style4)
        saying.setText(Thonburi);
    }

}

@Override
public void itemStateChanged(ItemEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub

}

}

I am having trouble with this assignment because my of my actionlisteners. Right now the panel has 2 checkboxes to change the font into bold and or italic. I want to add 4 radio buttons that can change what font the "saying" is in. Can anyone tell me what I'm doing wrong with my actionlisteners? Thank you!

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class StyleOptionsPanel extends JPanel implements ActionListener, ItemListener
{
private JLabel saying;
private JCheckBox bold, italic; // have a button for style1, button for style2, button for style3
private JRadioButton style1, style2, style3, style4; //creates the buttons for each of the 4 styles
{
    bold = new JCheckBox ("Bold", true);
    italic = new JCheckBox ("Italic", true);

    style1 = new JRadioButton ("Arial", true);
    style2 = new JRadioButton ("Times New Roman", false);
    style3 = new JRadioButton ("Verdana", false);
    style4 = new JRadioButton ("Thonburi", false);

    //also check out quote options panel file for example of radio buttons.
    //after creating your radio buttons, you have to create a button group. copy line exactly from quote options panel
    //comedy = new JRadioButton ("Comedy", true); - example of assigning radio button
    //comedy.setBackground (Color.green);
    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);

    add (style1);
    add (style2);
    add (style3);
    add (style4);
}
//-----------------------------------------------------------------
//  Sets up a panel with a label and some check boxes that
//  control the style of the label's font.
//-----------------------------------------------------------------
public StyleOptionsPanel()
{
    saying = new JLabel ("Say it with style!");
    saying.setFont (new Font ("Helvetica", Font.PLAIN, 36)); // we'll need this later

    bold = new JCheckBox ("Bold"); // what is in quotes doesn't have to match variable being created but it should be descriptive
    bold.setBackground (Color.green); // you can also set the background color of the checkbox
    italic = new JCheckBox ("Italic");
    italic.setBackground (Color.green); // this doesn't seem to change anything for me?

    //StyleListener listener = new StyleListener(); //whenever you create a textbox, you have to add a listener for it
    //bold.addActionListener (listener);
    //italic.addActionListener (listener);

    add (saying); //it matters what order these go in, it affects the way that your program looks
    add (bold);
    add (italic);
    add (style1);
    add (style2);
    add (style3);
    add (style4);

    setBackground (Color.cyan);
    setPreferredSize (new Dimension(300, 100));
}

//*****************************************************************
//  Represents the listener for both check boxes and the radio boxes.
//*****************************************************************
private class StyleListener implements ActionListener, ItemListener
{
    //--------------------------------------------------------------
    //  Updates the style of the label font style.
    //--------------------------------------------------------------
    public void itemStateChanged(ItemEvent e) 
    {
        int style = Font.PLAIN;

        if (bold.isSelected()) //this is a boolean statement, on the inside you will always expect some kind of boolean statement
            style = Font.BOLD;

        if (italic.isSelected())
            style += Font.ITALIC; // style += means that it takes whatever style the font already is and then adds the value Italic to it
        //public void actionPerformed (ActionEvent event) //also use this code in homework

        Object source = e.getSource();// TODO Auto-generated method stub

    }

    public void actionPerformed(ActionEvent e) // this is our bread and butter, it is basically what we will be changing
    {


        if (source == style1) //if (source == style1)
            saying.setText (Arial); //quote.setText(Helvetica)
        else (source == style2)
        saying.setText (TimesNewRoman);
        else (source == style3)
        saying.setText (Verdana);
        else (source == style4)
        saying.setText(Thonburi);
    }

}

@Override
public void itemStateChanged(ItemEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub

}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜雨飘雪 2024-10-04 19:05:29

正如之前的答案,您添加了错误的 ActionListener。

StyleListener styleListener = new StyleListener();
style1.addActionListener(styleListener);
style2.addActionListener(styleListener);
style3.addActionListener(styleListener);
style4.addActionListener(styleListener);

您也应该对复选框执行相同的操作

bold.addItemListener(styleListener);
italic.addItemListener(styleListener);

您的主 StyleOptionsPanel 不需要成为侦听器。

As previous answer, your are adding the wrong ActionListener.

StyleListener styleListener = new StyleListener();
style1.addActionListener(styleListener);
style2.addActionListener(styleListener);
style3.addActionListener(styleListener);
style4.addActionListener(styleListener);

and you should also do the same for the checkboxes

bold.addItemListener(styleListener);
italic.addItemListener(styleListener);

Your main StyleOptionsPanel need not to be be a listener.

上课铃就是安魂曲 2024-10-04 19:05:29

您将其作为 ActionListener 添加到单选按钮,但侦听器方法是在单独的 StyleListener 类中实现的。

You are adding this as an ActionListener to your radio buttons, yet listener methods are implemented in a separate StyleListener class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文