⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 exercise13_11.java

📁 Introduction to java programming 一书中所有编程练习部分的源码
💻 JAVA
字号:
// Exercise13_11.java: Demonstrate TextField properties
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import javax.swing.border.*;

public class Exercise13_11 extends JApplet {
  boolean isStandalone = false;
  JPanel jPanel1 = new JPanel();
  JLabel jLabel1 = new JLabel();
  JTextField jtf = new JTextField();
  JPanel jPanel2 = new JPanel();
  JPanel jPanel3 = new JPanel();
  JRadioButton jrbLeft = new JRadioButton();
  JRadioButton jrbCenter = new JRadioButton();
  JRadioButton jrbRight = new JRadioButton();
  TitledBorder titledBorder1;
  JLabel jLabel2 = new JLabel();
  JTextField jtfColumns = new JTextField();
  JPanel jPanel4 = new JPanel();
  FlowLayout flowLayout1 = new FlowLayout();

  //Construct the applet
  public Exercise13_11() {
    titledBorder1 = new TitledBorder("");
    this.setSize(new Dimension(663, 223));
    jLabel1.setText("Text field");
    jrbLeft.setText("Left");
    jrbLeft.addActionListener(new java.awt.event.ActionListener() {

      public void actionPerformed(ActionEvent e) {
        jrbLeft_actionPerformed(e);
      }
    });
    jrbCenter.setText("Center");
    jrbCenter.addActionListener(new java.awt.event.ActionListener() {

      public void actionPerformed(ActionEvent e) {
        jrbCenter_actionPerformed(e);
      }
    });
    jrbRight.setText("Right");
    jrbRight.addActionListener(new java.awt.event.ActionListener() {

      public void actionPerformed(ActionEvent e) {
        jrbRight_actionPerformed(e);
      }
    });
    jPanel2.setBorder(titledBorder1);
    titledBorder1.setTitle("Horizontal Alignment");
    jPanel3.setBorder(BorderFactory.createEtchedBorder());
    jLabel2.setText("Column Size");
    jtfColumns.setFont(new java.awt.Font("Monospaced", 1, 16));
    jtfColumns.setForeground(Color.red);
    jtfColumns.setColumns(10);
    jtfColumns.addActionListener(new java.awt.event.ActionListener() {

      public void actionPerformed(ActionEvent e) {
        jtfColumns_actionPerformed(e);
      }
    });
    jtf.setFont(new java.awt.Font("Monospaced", 1, 16));
    jtf.setForeground(Color.red);
    jtf.setToolTipText("Demonstrate JTextField");
    jtf.setText("Type any thing");
    jtf.setColumns(10);
    jPanel1.setLayout(flowLayout1);
    this.getContentPane().add(jPanel1,  BorderLayout.NORTH);
    jPanel1.add(jLabel1, null);
    jPanel1.add(jtf, null);
    jPanel2.add(jrbLeft, null);
    jPanel2.add(jrbCenter, null);
    jPanel2.add(jrbRight, null);
    jPanel3.add(jLabel2, null);
    jPanel3.add(jtfColumns, null);
    this.getContentPane().add(jPanel4,  BorderLayout.SOUTH);
    jPanel4.add(jPanel2, null);
    jPanel4.add(jPanel3, null);

    // Group radio buttons
    ButtonGroup btg = new ButtonGroup();
    btg.add(jrbLeft);
    btg.add(jrbCenter);
    btg.add(jrbRight);
  }

  //Get Applet information
  public String getAppletInfo() {
    return "Applet Information";
  }

  //Get parameter info
  public String[][] getParameterInfo() {
    return null;
  }

  //Main method
  public static void main(String[] args) {
    Exercise13_11 applet = new Exercise13_11();
    applet.isStandalone = true;
    JFrame frame = new JFrame();
    frame.setTitle("Exercise13_11");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(applet, BorderLayout.CENTER);
    applet.init();
    applet.start();
    frame.setSize(400,320);
    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
    frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2);
    frame.setVisible(true);
  }

  void jrbLeft_actionPerformed(ActionEvent e) {
    if (jrbLeft.isSelected()) {
      jtf.setHorizontalAlignment(SwingConstants.LEFT);
    }
  }

  void jrbCenter_actionPerformed(ActionEvent e) {
    if (jrbCenter.isSelected()) {
      jtf.setHorizontalAlignment(SwingConstants.CENTER);
    }
  }

  void jrbRight_actionPerformed(ActionEvent e) {
    if (jrbRight.isSelected()) {
      jtf.setHorizontalAlignment(SwingConstants.RIGHT);
    }
  }

  void jtfColumns_actionPerformed(ActionEvent e) {
    jtf.setColumns(
      (new Integer(jtfColumns.getText().trim()).intValue()));
    validate();
  }
}

⌨️ 快捷键说明

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