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)