test.txt

来自「编写程序验证 JButton, JCheckBox, JRadioButton」· 文本 代码 · 共 98 行

TXT
98
字号
import java.awt.*;       	// Java core packages
import java.awt.event.*; 	// Java core packages
import javax.swing.*; 	// Java extension packages

public class J_TestButton extends JFrame implements ItemListener
{
    private String          	m_string= "No pain, no gain.";
    private int             	m_font = Font.PLAIN;
    private JTextField      	m_text;
    private JButton         	m_buttonCommand;
    private JCheckBox[]    m_check= new JCheckBox[2];
    private JRadioButton[]m_radio= new JRadioButton[3];

    public J_TestButton()
    {
        super( "Test buttons" );
        
        Container container = getContentPane();
        container.setLayout( new FlowLayout(FlowLayout.LEFT) );

        m_text = new JTextField( m_string, 25 );    // JTextField
        container.add( m_text ); 

       //
       // … … …
      //
 	 //  setSize( 300, 120 );
      //   setVisible( true );
  //} // End of constructor: J_TestButton()
   m_buttonCommand = new JButton( "Reset" );   // JButton
        container.add( m_buttonCommand );
        m_buttonCommand.addActionListener( new ActionListener()
            {
                public void actionPerformed( ActionEvent event )
                {
                    m_text.setText(m_string);
                    m_check[0].setSelected(false);
                    m_check[1].setSelected(false);
                    m_radio[0].setSelected(true);
                    m_radio[1].setSelected(false);
                    m_radio[2].setSelected(false);
                } // End of method: actionPerformed
            } // End of inner class for handling m_buttonCommand
        );
m_check[0] = new JCheckBox( "Bold font" );       // JCheckBox
        m_check[1] = new JCheckBox( "Italic font" );
        for (int i= 0; i< 2; i++)
        {
            container.add( m_check[i] );
            m_check[i].addItemListener(this);
        } // End of loop: for (JCheckBox)
m_radio[0]= new JRadioButton("Serif", true);   
        m_radio[1]= new JRadioButton("Monospaced", false);
        m_radio[2]= new JRadioButton("SansSerif", false);
        ButtonGroup radioGroup = new ButtonGroup();
        for (int i= 0; i< 3; i++)
        {
            container.add( m_radio[i] );
            radioGroup.add(m_radio[i]);
            m_radio[i].addItemListener(this);
        } // End of loop: for (JRadioButton)


        setSize( 300, 120 );
        setVisible( true );
    } // End of constructor: J_TestButton()
public void itemStateChanged( ItemEvent event )
    {
        String s= "Serif";
        
        if (event.getSource() == m_check[0])
        {
            if ( event.getStateChange() == ItemEvent.SELECTED )
                 m_font= m_font | Font.BOLD;
            else m_font= m_font & (Font.BOLD ^ (-1));
        }
        if (event.getSource() == m_check[1])
        {
            if ( event.getStateChange() == ItemEvent.SELECTED )
                 m_font= m_font | Font.ITALIC;
            else m_font= m_font & (Font.ITALIC ^ (-1));
        }
        if ( event.getSource() == m_radio[0] )
            s= "Serif";
        else if ( event.getSource() == m_radio[1] )
            s= "Monospaced";
        else if ( event.getSource() == m_radio[2] )
            s= "SansSerif";
        m_text.setFont(new Font( s, m_font, 14 ) );
    } // End of method: itemStateChanged
public static void main(String args[])
    {
      J_TestButton app = new J_TestButton();
      app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    } // End of method: main
} // End of class: J_TestButton

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?