java代码

package x1;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class dst extends JFrame implements ActionListener{//声明
JLabel a1,a2;
JTextField b1;
JPasswordField b2;
JButton c1,c2;
public dst() {
super("qq");//显示标题
this.setLayout(null);//手工布局
a1=new JLabel("用户名");//初始化控件
a2=new JLabel("密码");
a1.setBounds(15,5,60,20);
a2.setBounds(15,25,60,20);

c1=new JButton("登录");
c2=new JButton("退出");
c1.setBounds(35,50,60,30);
c2.setBounds(135,50,60,30);

c1.addActionListener(this);
c2.addActionListener(this);//注册监听器

b1=new JTextField("",10);
b2=new JPasswordField("",20);
b1.setBounds(110,5,100,20);
b2.setBounds(110,25,100,20);

getContentPane().add(a1);//捕获窗口以及将控件放入窗口
getContentPane().add(a2);
getContentPane().add(b1);
getContentPane().add(b2);
getContentPane().add(c1);
getContentPane().add(c2);
this.setVisible(true);
this.setSize(260,150);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==c1){
if(b1.getText().equals("123")&&b2.getText().equals("123")){
JOptionPane.showConfirmDialog(this,"登录成功");
}else {
JOptionPane.showMessageDialog(this,"用户名/密码错误,登陆失败");
}
}
if(e.getSource()==c2) {
System.exit(0);
}
}


public static void main(String[] args) {
dst y=new dst();
}

}

package x1;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class d5t extends JFrame implements ActionListener{//声明
JLabel x1;
JRadioButton z1,z2;
JCheckBox y1,y2;
public d5t(){
super("单选框复选按钮");
this.setLayout(null);
x1=new JLabel("请选择相应的样式");
x1. setBounds(15,5,150,20);
x1.setFont(new Font("黑体",Font.PLAIN,20));

z1=new JRadioButton("红色");
z2=new JRadioButton("蓝色");
z1.setBounds( 35,25,60,20);
z2.setBounds( 135,25,60,20);

z1.addActionListener(this);
z2.addActionListener(this);//注册监听器


y1=new JCheckBox("加粗");
y2=new JCheckBox("倾斜");
y1. setBounds(35,50,60,30);
y2. setBounds( 135,50,60,30);
//
////y1.addItemListener(this);
////y2.addItemListener(this);//注册监听器

getContentPane().add(x1);
getContentPane().add(z1);
getContentPane().add(z2);
getContentPane().add(y1);
getContentPane().add(y2);

this. setVisible(true);
this. setSize(260,150);
}
//实现
public void actionPerformed(ActionEvent e)
{if(e.getSource()==z1) {//如果选中的z1
x1.setForeground(Color.red) ;
x1.setText("你选择了红色");
}if(e.getSource()==z2) {//如果选中的z2
x1.setForeground(Color.blue);
x1.setText("你选择了蓝色");
}
}


public static void main(String args[]){
d5t y=new d5t();
}}

 

 

 


package x1;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class caidan extends JFrame{
JMenuItem xj,dk,h,l,lan;
JMenu wj,xt,ys;
JMenuBar m;
public caidan() {
super("菜单");
JMenu wj=new JMenu("文件");
JMenu xt=new JMenu("系统");
JMenu ys=new JMenu("颜色");

JMenuItem xj=new JMenuItem("新建");
JMenuItem dk=new JMenuItem("打开");
JMenuItem h=new JMenuItem("红");
JMenuItem l=new JMenuItem("绿");
JMenuItem lan=new JMenuItem("蓝");

JMenuBar m=new JMenuBar();
wj.add(xj);
wj.add(dk);

ys.add(h);
ys.add(l);
ys.add(lan);

m.add(wj);
m.add(ys);
m.add(xt);
setJMenuBar(m);//将菜单条放到容器中

setSize(300,300);
setVisible(true);


}
public static void main(String[] args) {
caidan tt=new caidan();
}


}

 

 


package x1;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class caidan extends JFrame implements ActionListener{
JMenuItem xj,dk,h,l,lan;
JMenu wj,xt,ys;
JMenuBar m;
JTextArea t;
public caidan() {
super("菜单");
JMenu wj=new JMenu("文件");
JMenu xt=new JMenu("系统");
JMenu ys=new JMenu("颜色");

JMenuItem xj=new JMenuItem("新建");
JMenuItem dk=new JMenuItem("打开");
h=new JMenuItem("红");
l=new JMenuItem("绿");
lan=new JMenuItem("蓝");

h.addActionListener(this);
l.addActionListener(this);
lan.addActionListener(this);




JMenuBar m=new JMenuBar();

t=new JTextArea(5,25);//创建5行25列的多行文本输入框

getContentPane().add(new JScrollPane(t));//将多行文本输入框添加水平滚动条和竖直滚动条

wj.add(xj);
wj.add(dk);

ys.add(h);
ys.add(l);
ys.add(lan);
ys.insertSeparator(2);//在索引值下加分割线



m.add(wj);
m.add(ys);
m.add(xt);
setJMenuBar(m);//将菜单条放到容器中

setSize(300,300);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{if(e.getSource()==h) {
t.setForeground(Color.red) ;
}if(e.getSource()==l) {
t.setForeground(Color.green);
}
if(e.getSource()==lan) {
t.setForeground(Color.blue);
}
}


public static void main(String[] args) {
caidan tt=new caidan();
}


}

 

 

 


package xu01;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class TanChu extends JFrame{
JRadioButtonMenuItem items[];//菜单项
Color[] color= {Color.blue,Color.yellow,Color.red,Color.orange};
JPopupMenu popupMenu;

public TanChu() {
super("右键弹出菜单");
ChangeColorAction action = new ChangeColorAction();
String[] str= {"blue","Yellow","Red","Orange","green"};
ButtonGroup colorGroup=new ButtonGroup();
popupMenu =new JPopupMenu();

items=new JRadioButtonMenuItem[5];
for(int i=0;i<items.length;i++)
{items[i]=new JRadioButtonMenuItem(str[i]);
popupMenu.add(items[i]);
colorGroup.add(items[i]);
items[i].addActionListener(action);
}
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent event) {
triggerEvent(event);
}
public void mouseReleased(MouseEvent event) {
triggerEvent(event);
}
private void triggerEvent(MouseEvent event) {
if(event.isPopupTrigger())
popupMenu.show(event.getComponent(),event.getX(),event.getY());

}


});

getContentPane().setBackground(Color.white);
setSize(230,160);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


}

class ChangeColorAction implements ActionListener{
public void actionPerformed(ActionEvent event) {
for(int i=0;i<items.length;i++)
if(event.getSource()==items[i]) {
getContentPane().setBackground(color[i]);
repaint();
return;

}


}





}
public static void main(String args[])
{
new TanChu();
}

}

 

posted @ 2023-05-25 09:53  薯条1600  阅读(45)  评论(0编辑  收藏  举报