「java按钮作业」java运行按钮

博主:adminadmin 2022-11-23 01:55:06 155

今天给各位分享java按钮作业的知识,其中也会对java运行按钮进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

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怎么做个简单按钮

你写的按钮计算吧,这个类是一个Applet,其中有一个按钮,这个类本身也是按钮的动作监听器,所以实现了ActionListener 接口用来给按钮调用(也就是 actionPerformed方法),其中的参数e是事件参数,当点击按钮时会发送给按钮使用。e.getSource() == b 就是如果点击是b这个按钮,当监听器给一个按钮使用时没有必要加此判断,e.getSource就是获取发生事件的源对象,比如

c = new JButton("点我有次数哦");

f.getContentPane().add(c);

c.setVisible(true);

c.addActionListener(this);

此时又增加了一个按钮,就可以用e.getSource() 判断点击的是哪一个按钮。

建议你把面向对象搞懂在学swing编程吧,很容易看懂的

java作业:一个窗口两个按钮,按按钮分别出来不同的字。。。。。。大神帮忙啊。。。

代码如下:

import javax.swing.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

public class HelloIFrame {

private static JTextArea area;

public static void main(String[] args) {

JFrame jf = new JFrame();

jf.setTitle("JAVA");

jf.setBounds(500, 200, 300, 300);

JPanel con = new JPanel(null);

area = new JTextArea();

area.setLineWrap(true);

JScrollPane jp = new JScrollPane(area);

jp.setBounds(10, 10, 280, 200);

con.add(jp);

JButton helloButton = new JButton("HELLO!");

JButton clearButton = new JButton("JAVA");

helloButton.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

area.setText("");

area.append("Hello!" + "\n");

}

});

clearButton.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

area.setText("");

area.append("JAVA!" + "\n");

}

});

helloButton.setBounds(70, 220, 75, 30);

clearButton.setBounds(150, 220, 75, 30);

con.add(helloButton);

con.add(clearButton);

jf.add(con);

jf.setResizable(false);

jf.setVisible(true);

jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

}

如果满意请采纳!

JAVA作业1、用三个按钮分别代表三种标题Test\Exercise和Program,选中单选按

import java.awt.Color;

import java.awt.FlowLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.util.Enumeration;

import javax.swing.AbstractButton;

import javax.swing.ButtonGroup;

import javax.swing.JFrame;

import javax.swing.JRadioButton;

import javax.swing.JSlider;

import javax.swing.event.ChangeEvent;

import javax.swing.event.ChangeListener;

public class TestDemo extends JFrame implements ActionListener, ChangeListener

{

private ButtonGroup _group;

private JRadioButton _num1;

private JRadioButton _num2;

private JRadioButton _num3;

private JSlider _slider1;

private JSlider _slider2;

private JSlider _slider3;

public TestDemo(){

_num1 = new JRadioButton("Test");

_num2 = new JRadioButton("Exercise");

_num3 = new JRadioButton("Program");

_group = new ButtonGroup();

_slider1 = new JSlider(0, 255, 0);

_slider2 = new JSlider(0, 255, 0);

_slider3 = new JSlider(0, 255, 0);

this.setLayout(new FlowLayout(FlowLayout.CENTER, 3,3));

_group.add(_num1);

_group.add(_num2);

_group.add(_num3);

this.add(_num1);

this.add(_num2);

this.add(_num3);

this.add(_slider1);

this.add(_slider2);

this.add(_slider3);

this.setBounds(100, 100, 300, 300);

this.setVisible(true);

_num1.addActionListener(this);

_num2.addActionListener(this);

_num3.addActionListener(this);

_slider1.addChangeListener(this);

_slider2.addChangeListener(this);

_slider3.addChangeListener(this);

}

public static void main(String [] args){

TestDemo demo = new TestDemo();

String title = "";

EnumerationAbstractButton elements = demo._group.getElements();

while(elements.hasMoreElements()){

AbstractButton button = elements.nextElement();

if(button.isSelected()){

title = button.getText();

break;

}

}

}

@Override

public void actionPerformed(ActionEvent e)

{

// TODO Auto-generated method stub

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

this.setTitle(_num1.getText());

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

this.setTitle(_num2.getText());

}else{

this.setTitle(_num3.getText());

}

}

@Override

public void stateChanged(ChangeEvent e)

{

// TODO Auto-generated method stub

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

this.getContentPane().setBackground(new Color(_slider1.getValue(), _slider2.getValue(), _slider3.getValue()));

System.out.println(_slider1.getValue());

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

this.getContentPane().setBackground(new Color(_slider1.getValue(), _slider2.getValue(), _slider3.getValue()));

}else{

this.getContentPane().setBackground(new Color(_slider1.getValue(), _slider2.getValue(), _slider3.getValue()));

}

repaint();

}

}

全部自己打的采纳吧

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

The End

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