「java按钮简介」java按钮布局
今天给各位分享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按钮事件
由于不知道你的实现,我自己做了个列子,参考下面的代码和注释:
import javax.swing.*;
import java.awt.event.*;
class Botton extends JFrame {
private JButton button1, button2; // 两个按钮
private boolean flag = true; // 标志第二个按钮状态
public Botton() {
super("系统登录"); // super调用父类构造方法
button1 = new JButton("第一个按钮");
button2 = new JButton("第二个按钮");
setLayout(null); // 设置为空布形
add(button1);
add(button2);
// 设置各个组件的位置、宽高
button1.setBounds(120, 200, 100, 30);
button2.setBounds(260, 200, 100, 30);
// 给第一按钮添加事件
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
flag = !flag; // 修改标志
button2.setEnabled(flag); // 控制第二个按钮
}
});
// 设置主窗体在屏幕上的位置 、宽高
setBounds(260, 200, 500, 370);
setResizable(false); // 设置主窗体属性:大小可见
setVisible(true); // 设置主窗体属:可见
// 窗口关闭,完全退出
setDefaultCloseOperation(this.EXIT_ON_CLOSE);
validate(); // 对主窗体刷新
}
public static void main(String arg[]) {
new Botton();// 创建窗体对象
}
}
亲,如果我的回答满意,请即时采纳,你的理解是我回答的动力,谢谢!
java 按钮问题
修改大小后要让JButton的容器重新布局才行
附一个简单的示例代码
import java.awt.Dimension;
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.JPanel;
/**
*
* @author Jeky
*/
public class MainFrame extends JFrame {
private JButton button;
private JPanel panel;
public MainFrame() {
button = new JButton("点我修改按钮宽度");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Dimension preferredSize = button.getPreferredSize();
if (preferredSize.width == 150) {
preferredSize.width = 200;
} else {
preferredSize.width = 150;
}
button.setPreferredSize(preferredSize);
panel.revalidate();
}
});
button.setPreferredSize(new Dimension(150, 30));
panel = new JPanel();
panel.setLayout(new FlowLayout(FlowLayout.CENTER));
panel.add(button);
this.getContentPane().add(panel);
this.setSize(300, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
new MainFrame();
}
}
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常见的按钮,文本框,窗体,鼠标,键盘对应的事件类名称
JButton JTextField JFrame
鼠标事件 监听器 适配器
键盘事件
KeyEvent KeyListener KeyAdapter
关于java按钮简介和java按钮布局的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
发布于:2022-12-01,除非注明,否则均为
原创文章,转载请注明出处。