新建一個窗口,然后實(shí)現(xiàn)一個關(guān)閉按鈕”窗口的功能
10年積累的成都網(wǎng)站設(shè)計、網(wǎng)站建設(shè)經(jīng)驗(yàn),可以快速應(yīng)對客戶對網(wǎng)站的新想法和需求。提供各種問題對應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識你,你也不認(rèn)識我。但先網(wǎng)站制作后付款的網(wǎng)站建設(shè)流程,更有沾化免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
import?java.awt.*;
import?java.awt.event.*;
public?class?TestWindowEvent?{
public?static?void?main?(String[]?args)?{
new?Frame88?("WindowAdapter");
}
}
class?Frame88?extends?Frame?{
Frame88?(String?s)?{
super(s);
setBounds?(300,300,200,70);
setLayout?(null);
setVisible?(true);
addWindowListener?(new?WindowAdapter()?{
public?void?windowClosing(WindowEvent?e)?{
setVisible?(false);
System.exit(0);
}
}??);
}?
}
兄弟幫你寫了一個:
import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Random;
public class Print {
public static void main(String[] args) {
new Te();
}
}
class Te extends Frame implements ActionListener {
Color cc = Color.red;
int x = -20, y = -50;
Random r = new Random();
public Te() {
this.setLayout(null);
Button b = new Button("畫圓");
this.add(b);
b.setBounds(30,30,50,50);
b.addActionListener(this);
this.addWindowListener(new WindowAdapter () {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
this.setBounds(200,200,500,400);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
this.cc = Color.red;
this.x = r.nextInt(400);
do {
int x1 = r.nextInt(300);
this.y = x1;
} while (this.y 50);
this.repaint();
}
@Override
public void paint(Graphics g) {
Color c = g.getColor();
g.setColor(cc);
g.drawRect(x,y,50,50);
g.setColor(c);
}
}
你就這態(tài)度???((偷笑),不想站在巨人的肩膀上了嗎,人活一世,裝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的默認(rèn)值為false,值的意義是是否顯示.
jf.setVisible(true);
// 按鈕
JButton jb = new JButton("按鈕");
// 添加按鈕
jf.add(jb);
// 因?yàn)樯厦娴牟季謽邮?,他設(shè)置了x50y80坐標(biāo)和長100寬30
jb.setBounds(50, 80, 100, 30);
}
}
參考代碼,
注意圖片的路徑,拿不準(zhǔn)的話,就使用絕對路徑吧
import?java.awt.*;
import?java.awt.event.*;
import?javax.swing.*;
//我的圖片路徑是?src\\images\\1.gif??.有四張?從1.jpg~~4.jpg
public?class?ImageDemo?extends?JFrame?{
JLabel?jl;
JPanel?jp;
public?ImageDemo()?{
jp?=?new?JPanel();
int?i;
for?(i?=?0;?i??4;?i++)?{
if(i?==0){//初始化的時候,默認(rèn)顯示的圖片
jl?=?new?JLabel(new?ImageIcon("src\\images\\"+1+".gif"));
}
//按鈕
JButton?jb?=?new?JButton("第"+(i+1)+"張圖");
int?z?=?i;
//當(dāng)按鈕點(diǎn)擊的時候
jb.addActionListener(new?ActionListener()?{
@Override
public?void?actionPerformed(ActionEvent?e)?{
//設(shè)置jl的圖片
jl.setIcon(new?ImageIcon("src\\images\\"+(z+1)+".gif"));
}
});
jp.add(jb);
}
this.setLocation(200,?120);
this.setSize(500,200);
this.setLayout(new?BorderLayout());
this.add(jl);
this.add(jp,BorderLayout.SOUTH);
this.setTitle("圖片瀏覽");
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
}
public?static?void?main(String[]?args)?{
new?ImageDemo();
}
}