javaswing代码
Ⅰ 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]);
}
}
}