java按钮
1. java怎么添加一个按钮
你就这态度???((偷笑),不想站在巨人的肩膀上了吗,人活一世,装X二字)
import javax.swing.JButton;
import javax.swing.JFrame;
public class c {
public static void main(String[] args) {
// 你懂的
JFrame jf = new JFrame();
// 你懂的
jf.setSize(300, 400);
// JFrame做为一个画板,他里面可以有控件,有控件就有控件布局的样式,样式为null可以理解为自由布局
jf.setLayout(null);
// JFrame的setVisible的默认值为false,值的意义是是否显示.
jf.setVisible(true);
// 按钮
JButton jb = new JButton("按钮");
// 添加按钮
jf.add(jb);
// 因为上面的布局样式,他设置了x50y80坐标和长100宽30
jb.setBounds(50, 80, 100, 30);
}
}
2. java怎么创建一个按钮
java当中创建按钮,必须使用java swing来创建,实例如下:
JButton有个构造函数是JButton(Icon icon),就是用来创建带图标的按钮的。
可以这样用,如:
JButton jb = new JButton(new ImageIcon("images/myImage.gif"));
这样得到的就是带图标的按钮了。
3. JAVA按钮设置了不显示
publicActionEventFrame(){//这就是窗口的构造方法了,
}
4. java界面添加按钮
为什么要多此一举写继承?写paint?直接new一个JPanel设置背景就完事了。
5. java 按钮问题
import javax.swing.*;
import java.awt.*;
public class Test extends JFrame{
JToolBar tbrMain;
JToggleButton btnFirst,btnSecond,btnThird;
public Test(){
super("按钮组示例");
setSize(400,300);
tbrMain=new JToolBar();
btnFirst=new JToggleButton("第一个");
tbrMain.add(btnFirst);
btnSecond=new JToggleButton("第二个");
tbrMain.add(btnSecond);
btnThird=new JToggleButton("第三个");
tbrMain.add(btnThird);
ButtonGroup grpButtons=new ButtonGroup();
grpButtons.add(btnFirst);
grpButtons.add(btnSecond);
grpButtons.add(btnThird);
add(tbrMain,BorderLayout.NORTH);
setVisible(true);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public static void main(String[] args){
new Test();
}
}
6. java按钮事件
由于不知道你的实现,我自己做了个列子,参考下面的代码和注释:
importjavax.swing.*;
importjava.awt.event.*;
classBottonextendsJFrame{
privateJButtonbutton1,button2;//两个按钮
privatebooleanflag=true;//标志第二个按钮状态
publicBotton(){
super("系统登录");//super调用父类构造方法
button1=newJButton("第一个按钮");
button2=newJButton("第二个按钮");
setLayout(null);//设置为空布形
add(button1);
add(button2);
//设置各个组件的位置、宽高
button1.setBounds(120,200,100,30);
button2.setBounds(260,200,100,30);
//给第一按钮添加事件
button1.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
flag=!flag;//修改标志
button2.setEnabled(flag);//控制第二个按钮
}
});
//设置主窗体在屏幕上的位置、宽高
setBounds(260,200,500,370);
setResizable(false);//设置主窗体属性:大小可见
setVisible(true);//设置主窗体属:可见
//窗口关闭,完全退出
setDefaultCloseOperation(this.EXIT_ON_CLOSE);
validate();//对主窗体刷新
}
publicstaticvoidmain(Stringarg[]){
newBotton();//创建窗体对象
}
}
亲,如果我的回答满意,请即时采纳,你的理解是我回答的动力,谢谢!
7. 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) {
//事件处理
}
}
}
8. java中创建一个按钮组,有10个按钮,分别写着0,1,2,,3。。。。9
用Java创建按钮组的程序如下:
importjava.awt.GridLayout;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjavax.swing.JButton;
importjavax.swing.JFrame;
{
JButton[]b=newJButton[10];
A(){
setLayout(newGridLayout(3,4,5,5));
for(inti=0;i<10;i++){
b[i]=newJButton(String.valueOf(i));
b[i].addActionListener(this);
add(b[i]);
}
setSize(300,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
publicstaticvoidmain(String[]args){
newA();
}
publicvoidactionPerformed(ActionEventae){
}
}
9. JAVA界面添加按钮
按照你的要求添加两个按钮的Java程序如下:
packagecom.sunshine.customer;
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
importjavax.swing.table.DefaultTableModel;
{
privateJPaneltopPanel;
privateJTabletable;
privateJButtonaddRoom=newJButton("添加房间");
privateJButtonremoveRoom=newJButton("删除房间");
privateDefaultTableModeldtm;
publicJTableDemo(){
setTitle("SimpleJTableApplication");
setSize(560,400);
setBackground(Color.gray);
topPanel=newJPanel();
topPanel.setLayout(newBorderLayout());
getContentPane().add(topPanel);
Stringcols[]={"房间号","是否预定","房间价格","房间类型","房间状态","卫生情况","剩余时间"};
StringrowData[][]={{"","","","","","",""},
{"","","","","","",""},{"","","","","","",""},{"","","","","","",""},
{"","","","","","",""},{"","","","","","",""},{"","","","","","",""},
{"","","","","","",""},{"","","","","","",""},{"","","","","","",""},
{"","","","","","",""},{"","","","","","",""},{"","","","","","",""},};
dtm=newDefaultTableModel();
dtm.setDataVector(rowData,cols);
table=newJTable(dtm);
JScrollPanescrollPane=newJScrollPane(table);
topPanel.add(scrollPane,BorderLayout.CENTER);
addRoom.addActionListener(this);
removeRoom.addActionListener(this);
JPanelp=newJPanel();
p.add(addRoom);
p.add(removeRoom);
topPanel.add(p,BorderLayout.SOUTH);
}
publicstaticvoidmain(Stringargs[]){
JTableDemomainFrame=newJTableDemo();
mainFrame.setVisible(true);
}
@Override
publicvoidactionPerformed(ActionEventae){
if(ae.getSource()==addRoom){
Stringrow[]={"","","","","","",""};
dtm.addRow(row);
}
if(ae.getSource()==removeRoom){
dtm.removeRow(table.getSelectedRow());
}
}
}
10. java按钮位置
java中的按钮是组建,组建都是放在容器里的,要设置组建位置,先要对容器布局,然后根据布局把按钮放到想要放的地方;
默认布局是 BorderLayout 按东南西北中排布,向四周扩散;
常见的布局方式还有 FlowLayout布局:从左到右排列,排满后转到下一行继续;
还有GridLayout 按 n行m列的网格布局;
但若要绝对定位位置,可以用null布局;
比如有一个容器p
p.setLayout(null);
Button button=new Button();
button.setBounds(int x,inty,width,height)