「java按钮模式」java选择按钮

博主:adminadmin 2022-12-03 13:06:05 74

本篇文章给大家谈谈java按钮模式,以及java选择按钮对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

Java,按钮怎么实现?

给你个示例程序:

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Container;

import java.awt.FlowLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

 

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

 

public class Test extends JFrame implements ActionListener {

    private JPanel panel0 = null, panel2 = null;

    private JButton b1 = null, b2 = null, b3 = null, b4 = null;

    public Test() {

        Container c = this.getContentPane();

        //边框布局

        c.setLayout(new BorderLayout());

        //创建panel

        panel0 = new JPanel();

        panel2 = new JPanel();

        //为2个panel设置底色

        panel0.setBackground(Color.red);

        panel2.setBackground(Color.BLUE);

        //2个panel都是用流水布局

        panel0.setLayout(new FlowLayout());

        panel2.setLayout(new FlowLayout());

        //创建按钮

        b1 = new JButton("panel2黄色");

        b2 = new JButton("panel2绿色");

        b3 = new JButton("panel0橙色");

        b4 = new JButton("panel0灰色");

        /**

         * 添加按钮事件

         */

        b1.addActionListener(this);

        b2.addActionListener(this);

        b3.addActionListener(this);

        b4.addActionListener(this);

        /**

         * 将按钮添加相应panel上

         */

        panel0.add(b1);

        panel0.add(new JLabel());

        panel0.add(b2);

        panel2.add(b3);

        panel2.add(b4);

        /**

         * 将panel添加到容器

         */

        c.add(panel0, BorderLayout.CENTER);

        c.add(panel2, BorderLayout.EAST);

        this.setSize(500, 500);

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        this.setVisible(true);

         

    }    

    public static void main(String[] args) {

        new Test();    

    }

    @Override

    public void actionPerformed(ActionEvent e) {

        // TODO Auto-generated method stub

        if (e.getSource() == b1) {

            panel2.setBackground(Color.yellow);

        } else if (e.getSource() == b2) {

            panel2.setBackground(Color.green);

        } else if (e.getSource() == b3) {

            panel0.setBackground(Color.ORANGE);

        }  else if (e.getSource() == b4) {

            panel0.setBackground(Color.GRAY);

        } 

    }

}

JAVA中怎么实现按钮功能?

使用图形用户界面

class Gui extends JFrame implements ActionListener {

private JButton jb = new JButton() ;

Gui() {

super("Gui") ;

this.add(jb) ;//添加按钮

jb.addActionListener(this) ;//按钮事件监听

//当然你可以按自己的想法做布局

this.pack();

this.setVisible(true);//可见

this.setResizable(false);//不可修改大小

this.setLocation(100, 100);//起始位置

}

//覆写ActionListener接口中的事件处理方法

@Override

public void actionPerformed(ActionEvent e) {

if(e.getSource() == jb) {

//事件处理

}

}

}

java中怎样给按钮设置快捷键?

键盘事件  KeyListener

例:(简化了的代码)

public class ShowKeyListener extends KeyAdapter {

private JButton btn_ok;

public ShowKeyListener ()  {

btn_ok.addKeyListener(this);

}

@Override

public void KetPressed(KeyEvent e) {

// 获取键盘键  KeyEvent.getKeyCode()

if(e.getKeyCode() == KeyEvent.VK_ENTER) {

...

}

}

}

延展阅读:

Java是一门面向对象编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承、指针等概念,因此Java语言具有功能强大和简单易用两个特征。Java语言作为静态面向对象编程语言的代表,极好地实现了面向对象理论,允许程序员以优雅的思维方式进行复杂的编程。

关于java按钮模式和java选择按钮的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-12-03,除非注明,否则均为首码项目网原创文章,转载请注明出处。