當前位置:首頁 » 編程語言 » javaswing代碼

javaswing代碼

發布時間: 2022-05-17 00:24:46

java Swing 代碼問題

importjava.awt.*;
importjavax.swing.*;

{//實現Icon介面
privateintwidth;//聲明圖標的寬
privateintheight;//聲明圖標的長
publicintgetIconHeight(){//實現getIconHeight()方法
returnthis.height;
}
publicintgetIconWidth(){//實現getIconWidth()方法
returnthis.width;
}
publicDrawIcon(intwidth,intheight){//定義構造方法
this.width=width;
this.height=height;
}
//實現paintIcon()方法
publicvoidpaintIcon(Componentarg0,Graphicsarg1,intx,inty){
arg1.fillOval(x,y,width,height);//繪制一個圖形
}
publicstaticvoidmain(Stringargs[]){
DrawIconicon=newDrawIcon(15,15);
//創建一個標簽,並設置標簽上的文字在標簽正中間
JLabeljl=newJLabel("測試",icon,SwingConstants.CENTER);
JFramejf=newJFrame();//創建一個JFrame窗口
Containercontainer=jf.getContentPane();

container.add(jl);//內容面板加入jl標簽
jf.setTitle("swing程序");//設置標題
jf.setLocation(100,100);//設置jf的位置
jf.setSize(200,200);//設置jf的大小
jf.setVisible(true);//設置可見
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//設置關閉按鈕後退出
}

}

Ⅱ 有一段java程序是實現swing組件的,還缺哪些代碼,哪位能幫忙補一下

我稍加改一下,在 public void actionPerformed(ActionEvent e) 中加了try catch 捕獲異常,如下:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import javax.swing.*;

public class JDemo extends JFrame implements ActionListener{
JLabel lb;
JTextField jt1,jt2,jt3;
public JDemo(){
Container c=getContentPane();
c.setLayout(new FlowLayout());
jt1=new JTextField(10);
c.add(jt1);
JLabel lb1=new JLabel("+");
c.add(lb1);
jt2=new JTextField(10);
c.add(jt2);
JLabel lb2=new JLabel("=");
c.add(lb2);
jt3=new JTextField(10);
c.add(jt3);
JButton jb=new JButton("OK");
c.add(jb);
jb.addActionListener(this);
lb=new JLabel();
c.add(lb);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setSize(200,200);
}
public void actionPerformed(ActionEvent e) //加入一個捕獲異常try catch 命令
{
try{
if(Integer.parseInt(jt1.getText())+Integer.parseInt(jt2.getText())==Integer.parseInt(jt3.getText()))
{lb.setText("沒問題!");
lb.setForeground(Color.blue);
}
else
{lb.setText("對不起!");
lb.setForeground(Color.red);
}
}catch (NumberFormatException ee) {
lb.setText(" 請輸入數字");
}
}
public static void main(String args[]){
new JDemo();
}
看看行不行?
}

Ⅲ java中的swing按鈕代碼問題求教

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Calendar;
public class hhh extends Frame
{
public static void main(String[] args)
{
new hhh();
}
public hhh()
{
setSize(300, 200);
setLocationRelativeTo(null);
setVisible(true);
addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent w){System.exit(0);}});
add(r);
add(bt,BorderLayout.NORTH);
bt.addActionListener(new ActionListener()
{public void actionPerformed(ActionEvent e)
{
new Thread(hhh.this.r).start();
}
});
}
Button bt=new Button("顯示");
Rec r=new Rec(bt);
}
class Rec extends Panel implements Runnable
{
private Button bt;
Rec(Button _bt){
bt=_bt;
}
int x1=30,x2=30,y1=10,y2=10;
public void paint(Graphics g)
{
g.setColor(Color.red);
g.fillOval(x2,y2,20,20);
}
public void run()
{
for(int i=30;i>=0;i-=10)
{
try {x1=-i;x2=i;Thread.sleep(1000);}
catch (Exception e){}
bt.setVisible(false);//錯誤
repaint();
}
}
}
把button傳入構造函數就可以

Ⅳ java怎麼做swing界面

swing做界面,一般需要一種基本知識:

一:關於布局的知識,常用的布局要熟悉,比如邊界布局,流式布局,絕對布局,網格布局等. 布局決定了組件(按鈕,文本框,下拉框等)所在的位置

二:關於事件響應機制,比如點擊按鈕事件,如何響應.

三:一些常用的組件,按鈕, 文本框, 下拉框,復選按鈕, 單選按鈕, 等

舉例: 簡單的代碼,實現給窗口隨機更換背景色

importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;

{

publicDemoFrame(){
JButtonjb1=newJButton("點擊換背景色");//定義按鈕組件
jb1.addActionListener(this);//給按鈕添加事件響應機制,按鈕點擊時執行actionPerformed方法
add(jb1);//把組件添加到窗口
setLayout(newFlowLayout());//流式布局
setTitle("Color");//窗口標題
setSize(300,200);//大小
setLocationRelativeTo(null);//居中
setDefaultCloseOperation(EXIT_ON_CLOSE);//點擊關閉時退出系統
setVisible(true);//窗口可見
}
publicstaticvoidmain(String[]args){//main方法,啟動窗口
newDemoFrame();//創建窗口
}

publicvoidactionPerformed(ActionEvente){//當按鈕被點擊時,執行下面的代碼
//隨機顏色,並設置成為窗口的背景色
getContentPane().setBackground(newColor((int)(Math.random()*256),(int)(Math.random()*256),(int)(Math.random()*256)));
}
}

Ⅳ java Swing的一些代碼問題怎麼解決

Swing是一個用於開發Java應用程序用戶界面的開發工具包。它以抽象窗口工具包(AWT)為基礎使跨平台應用程序可以使用任何可插拔的外觀風格。Swing開發人員只用很少的代碼就可以利用Swing豐富、靈活的功能和模塊化組件來創建優雅的用戶界面。

用Swing創建圖形界面步驟:

(1)導入Swing包

(2)選擇界面風格

(3)設置頂層容器

(4)設置按鈕和標簽

(5)將組件放到容器上

(6)為組件增加邊框

(7)處理事件

(8)輔助技術支持

Ⅵ JAVA Swing 問題 程序代碼

import java.awt.*;
import java.net.URL;
import javax.swing.*;
public class MyImageIcon extends JFrame {
private static final long serialVersionUID = 1L;
public MyImageIcon() {
Container container = getContentPane();
// 創建一個標簽
JLabel jl = new JLabel("這是一個JFrame窗體", JLabel.CENTER);
// 獲取圖片所在的URL
// URL url = MyImageIcon.class.getResource("imageButton.jpg");
Icon icon = new ImageIcon("imageButton.jpg"); // 實例化Icon對象
jl.setIcon(icon);// 為標簽設置圖片
// 設置文字放置在標簽中間
jl.setHorizontalAlignment(SwingConstants.CENTER);
jl.setOpaque(true);// 設置標簽為不透明狀態
container.add(jl); // 將標簽添加到容器中去
setSize(250, 100);// 設置窗體大小
setVisible(true);// 使窗體可見
// 設置窗體關閉模式
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String args[]) {
new MyImageIcon(); // 實例化MyImageIcon對象
}
}

Ⅶ java Swing的一些代碼問題

依次解釋:
第一個方法是設置是否可以編輯的,你設置為false就是說這個組件不可編輯
第二個方法是設置該組件內容水平對齊的方向的,你設置的是居中對齊
第三個方法是設置該組件的列數的你設置的是18列
第四個方法設置該組件在垂直方向上與其他組件的間距的
第五個方法設置該組件在水平方向上與其他組件的間距的

Ⅷ java中的swing什麼意思

Swing是一個用於開發Java應用程序用戶界面的開發工具包。以抽象窗口工具包(AWT)為基礎使跨平台應用程序可以使用任何可插拔的外觀風格。
Swing開發人員只用很少的代碼就可以利用Swing豐富、靈活的功能和模塊化組件來創建優雅的用戶界面。 工具包中所有的包都是以swing作為名稱.

Ⅸ java的swing

Swing是一個用於開發Java應用程序用戶界面的開發工具包。
以抽象窗口工具包(AWT)為基礎使跨平台應用程序可以使用任何可插拔的外觀風格。Swing開發人員只用很少的代碼就可以利用Swing豐富、靈活的功能和模塊化組件來創建優雅的用戶界面。 工具包中所有的包都是以swing作為名稱,例如javax.swing,javax.swing.event。

Ⅹ 求一個java swing帶界面的萬年歷代碼

按照你的要求編寫的Java swing 帶界面的萬年歷代碼如下

//日歷
importjava.awt.BorderLayout;
importjava.awt.Color;
importjava.awt.Font;
importjava.awt.GridLayout;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.util.Calendar;
importjavax.swing.BorderFactory;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JPanel;
{
JButtonjb1=newJButton("<<");
JButtonjb2=newJButton("<");
JButtonjb3=newJButton(">");
JButtonjb4=newJButton(">>");
JPaneljp1=newJPanel();
JPaneljp2=newJPanel();
JPaneljp3=newJPanel();
JPaneljp4=newJPanel();
JLabeljl1=newJLabel();
JLabeljl2=newJLabel();
JLabel[]jl=newJLabel[49];
String[]week={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
Calendarc=Calendar.getInstance();
intyear,month,day;
intnowyear,nowmonth,nowday;
CCI(){
super("簡單日歷");
nowyear=c.get(Calendar.YEAR);
nowmonth=c.get(Calendar.MONTH)+1;
nowday=c.get(Calendar.DAY_OF_MONTH);
year=nowyear;
month=nowmonth;
day=nowday;
Strings=year+"年"+month+"月";
jl1.setForeground(Color.RED);
jl1.setFont(newFont(null,Font.BOLD,20));
jl1.setText(s);
jb1.addActionListener(this);
jb2.addActionListener(this);
jb3.addActionListener(this);
jb4.addActionListener(this);
jp1.add(jb1);jp1.add(jb2);jp1.add(jl1);jp1.add(jb3);jp1.add(jb4);
jp2.setLayout(null);
createMonthPanel();
jp2.add(jp3);
jl2.setFont(newFont(null,Font.BOLD,20));
jl2.setText("今天是"+nowyear+"年"+nowmonth+"月"+nowday+"日");
jp4.add(jl2);
add(jp1,BorderLayout.NORTH);
add(jp2,BorderLayout.CENTER);
add(jp4,BorderLayout.SOUTH);
setSize(500,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
@Override
publicvoidactionPerformed(ActionEventae){
if(ae.getSource()==jb1){
year=year-1;
Strings=year+"年"+month+"月";
jl1.setText(s);
jp3.removeAll();
createMonthPanel();
jp3.validate();
}
if(ae.getSource()==jb2){
if(month==1){
year=year-1;
month=12;
}else{
month=month-1;
}
Strings=year+"年"+month+"月";
jl1.setText(s);
jp3.removeAll();
createMonthPanel();
jp3.validate();
}
if(ae.getSource()==jb3){
if(month==12){
year=year+1;
month=1;
}else{
month=month+1;
}
Strings=year+"年"+month+"月";
jl1.setText(s);
jp3.removeAll();
createMonthPanel();
jp3.validate();
}
if(ae.getSource()==jb4){
year=year+1;
Strings=year+"年"+month+"月";
jl1.setText(s);
jp3.removeAll();
createMonthPanel();
jp3.validate();
}
}
publicstaticvoidmain(String[]args){
newCCI();
}
publicintgetMonthDays(intyear,intmonth){
switch(month){
case1:
case3:
case5:
case7:
case8:
case10:
case12:
return31;
case2:
if((year%4==0&&year%100!=0)||year%400==0){
return29;
}else{
return28;
}
default:
return30;
}
}
publicvoidcreateMonthPanel(){
c.set(year,month-1,getMonthDays(year,month));
intweekOfMonth=c.get(Calendar.WEEK_OF_MONTH);
if(weekOfMonth==6){
jp3.setLayout(newGridLayout(7,7));
jp3.setBounds(50,20,420,350);
}else{
jp3.setLayout(newGridLayout(6,7));
jp3.setBounds(50,20,420,300);
}
jp3.setBorder(BorderFactory.createEtchedBorder());
for(inti=0;i<7;i++){
jl[i]=newJLabel(week[i],JLabel.CENTER);
jl[i].setFont(newFont(null,Font.BOLD,20));
jl[i].setBorder(BorderFactory.createEtchedBorder());
jp3.add(jl[i]);
}
c.set(year,month-1,1);
intemptyFirst=c.get(Calendar.DAY_OF_WEEK)-1;
intdaysOfMonth=getMonthDays(year,month);
for(inti=6+emptyFirst;i>=7;i--){
intintyear=year;
intintmonth=month;
if(intmonth==1){
intyear=intyear-1;
intmonth=12;
}else{
intmonth=intmonth-1;
}
intintdays=getMonthDays(intyear,intmonth);
jl[i]=newJLabel((intdays+7-i)+"",JLabel.CENTER);
jl[i].setFont(newFont(null,Font.BOLD,20));
jl[i].setForeground(Color.GRAY);
jl[i].setBorder(BorderFactory.createEtchedBorder());
jp3.add(jl[i]);
}
for(inti=7+emptyFirst;i<daysOfMonth+7+emptyFirst;i++){
jl[i]=newJLabel((i-7-emptyFirst+1)+"",JLabel.CENTER);
jl[i].setFont(newFont(null,Font.BOLD,20));
if((i+1)%7==0||(i+1)%7==1){
jl[i].setForeground(Color.RED);
}elseif((i-7-emptyFirst+1)==nowday&&month==nowmonth&&year==nowyear)
jl[i].setForeground(Color.BLUE);
else
jl[i].setForeground(Color.BLACK);

jl[i].setBorder(BorderFactory.createEtchedBorder());
jp3.add(jl[i]);
}
if(weekOfMonth==6)
for(inti=48;i>=daysOfMonth+emptyFirst+7;i--){
jl[i]=newJLabel((49-i)+"",JLabel.CENTER);
jl[i].setFont(newFont(null,Font.BOLD,20));
jl[i].setForeground(Color.GRAY);
jl[i].setBorder(BorderFactory.createEtchedBorder());
jp3.add(jl[i]);
}
else
for(inti=41;i>=daysOfMonth+emptyFirst+7;i--){
jl[i]=newJLabel((42-i)+"",JLabel.CENTER);
jl[i].setFont(newFont(null,Font.BOLD,20));
jl[i].setForeground(Color.GRAY);
jl[i].setBorder(BorderFactory.createEtchedBorder());
jp3.add(jl[i]);
}
}
}
熱點內容
夢三國2副本腳本 發布:2025-05-14 19:29:58 瀏覽:859
phpxmlhttp 發布:2025-05-14 19:29:58 瀏覽:432
Pua腳本 發布:2025-05-14 19:24:56 瀏覽:448
蘋果像素低為什麼比安卓好 發布:2025-05-14 19:13:23 瀏覽:459
安卓機微信怎麼設置紅包提醒 發布:2025-05-14 19:00:15 瀏覽:271
androidsystem許可權設置 發布:2025-05-14 18:56:02 瀏覽:970
mq腳本 發布:2025-05-14 18:45:37 瀏覽:25
仙境傳說ro解壓失敗 發布:2025-05-14 18:45:01 瀏覽:868
betweenand的用法sql 發布:2025-05-14 18:39:25 瀏覽:250
tplink攝像頭存儲卡格式化 發布:2025-05-14 18:37:08 瀏覽:347