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

📄 labelandbutton.java

📁 JAVA学习源代码,大家可以好好参考,请多提宝贵意见
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class LabelAndButton
    extends JFrame {
  private JLabel label;
  private JToggleButton button1;
  private JButton button2;
  private Icon labelIcon, buttonIcon1, buttonIcon2;
  public LabelAndButton() {
    super("Label and Button Test"); //窗口标题
    Container container = getContentPane();
    container.setLayout(new FlowLayout());
    labelIcon = new ImageIcon(LabelAndButton.class.getResource(
        "images\\butter.gif")); //标签图标对象
    label = new JLabel("这是一个文字和图形标签", labelIcon, SwingConstants.LEFT);
    label.setToolTipText("这是带有图形和文字的标签"); //标签提示信息
    label.setVerticalTextPosition(SwingConstants.BOTTOM);
    label.setHorizontalTextPosition(SwingConstants.CENTER);
    button1 = new JToggleButton("JToggleButton"); //JToggleButton按钮对象
    buttonIcon1 = new ImageIcon(LabelAndButton.class.getResource(
        "images\\clickPic.gif"));
    buttonIcon2 = new ImageIcon(LabelAndButton.class.getResource(
        "images\\click.gif"));
    button2 = new JButton(); //JButton按钮对象
    button2.setText("JButton"); //设置按钮显示的文本内容
    button2.setIcon(buttonIcon1); //设置按钮图标
    button2.setRolloverIcon(buttonIcon2); //设置提示图标
    container.add(button1);
    container.add(button2);
    container.add(label);
    ButtonHandler handler = new ButtonHandler(); //定义按钮响应的内部类对象
    button1.addActionListener(handler);
    button2.addActionListener(handler);
    //监听窗口事件,关闭时退出
    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });
    pack(); //自动调整组件位置
    setVisible(true);
  }

  //处理鼠标事件的内部类
  private class ButtonHandler
      implements ActionListener {
    public void actionPerformed(ActionEvent event) {
      JOptionPane.showMessageDialog(null,
                                    "You pressed: " + event.getActionCommand());
    }
  }

  //返回窗体显示大小
  public Dimension getPreferredSize() {
    return new Dimension(240, 260);
  }

  //执行应用程序
  public static void main(String args[]) {
    LabelAndButton labelAndButton = new LabelAndButton();
  }
}

⌨️ 快捷键说明

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