在GUI程序中更改字体

时间:2014-04-12 19:09:20

标签: java user-interface

我正在为学校编写一个使用GUI界面来改变文本外观的程序。这包括使其粗体,斜体和改变它的大小。我想出了如何斜体和粗体文本,但当我试图弄清楚如何更改文本的大小,它编译,但当我尝试运行它我收到输出:静态错误:此类没有接受String []的静态void main方法。我该如何解决这个问题,如果我没有正确执行此操作,如何更改文本的大小?

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

public class StyleGUI
{

    private final int WIDTH = 300, HEIGHT = 150;
    private int fontSize = 36;
    private int style = Font.PLAIN;
    private JLabel saying;
    private JCheckBox bold, italic, small, medium, large;
    private JPanel primary;

    //-----------------------------------------------------------------
    //  Sets up a panel with a label and some check boxes that
    //  control the style of the label's font.
    //-----------------------------------------------------------------
    public StyleGUI()
    {
 saying = new JLabel ("Say it with style!");
 saying.setFont (new Font ("Helvetica", style, fontSize));


 bold = new JCheckBox ("Bold");
 bold.setBackground (Color.cyan);
 italic = new JCheckBox ("Italic");
 italic.setBackground (Color.cyan);
 small = new JCheckBox ("Small");
 small.setBackground (Color.cyan);
 medium = new JCheckBox ("Medium");
 medium.setBackground (Color.cyan);
 large = new JCheckBox ("Large");
 large.setBackground (Color.cyan);


 StyleListener listener = new StyleListener();
 bold.addItemListener (listener);
 italic.addItemListener (listener);
 small.addItemListener (listener);
 medium.addItemListener (listener);
 large.addItemListener (listener);


 primary = new JPanel();
 primary.add (saying);
 primary.add (bold);
 primary.add (italic);
 primary.add (small);
 primary.add (medium);
 primary.add (large);

 primary.setBackground (Color.cyan);
 primary.setPreferredSize (new Dimension(WIDTH, HEIGHT));
    }

    //-----------------------------------------------------------------
    //  Returns the primary panel containing the GUI.
    //-----------------------------------------------------------------
    public JPanel getPanel()
    {
 return primary;
    }


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

   if (bold.isSelected())
       style = Font.BOLD;

   if (italic.isSelected())
       style += Font.ITALIC;

   if (small.isSelected())
     style -= fontSize;

     if (medium.isSelected())
      style = fontSize;

     if (large.isSelected())
        style += fontSize;


     saying.setFont (new Font ("Helvetica", style, fontSize));
      }
   }
}




import javax.swing.*;

public class StyleOptions
{
   //-----------------------------------------------------------------
   //  Creates and presents the program frame.
   //-----------------------------------------------------------------
   public static void main (String[] args)
   {
      JFrame styleFrame = new JFrame ("Style Options");
      styleFrame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

      StyleGUI gui = new StyleGUI();
      styleFrame.getContentPane().add (gui.getPanel());

      styleFrame.pack();
      styleFrame.show();
   }
}

1 个答案:

答案 0 :(得分:0)

您收到的错误非常明确,可能是您尝试运行StyleGUI,而不是StyleOptions,这是包含main方法的类,因此是Java应用程序。

分配样式和字体大小时,您会遇到一些错误。关于字体样式,我会说你缺少使用+=运算符来设置BOLD

关于我要说的样式你应该定义一个默认的,中等大小,并根据选中的复选框添加或减去它。由于我认为一次只能选择其中一种尺寸,因此您可以考虑使用CheckboxGroup

可能是这样的

<强> StyleGUI

private static final int DEFAULT_SIZE = 36;

...
int fontSize = DEFAULT_SIZE;

if (small.isSelected())  fontSize = DEFAULT_SIZE - 5;     
if (medium.isSelected()) fontSize = DEFAULT_SIZE;
if (large.isSelected())  fontSize = DEFAULT_SIZE + 5;

...