扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
import java.awt.Container;
创新互联公司专注为客户提供全方位的互联网综合服务,包含不限于网站制作、成都做网站、洪山网络推广、小程序定制开发、洪山网络营销、洪山企业策划、洪山品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联公司为所有大学生创业者提供洪山建站搭建服务,24小时服务热线:13518219792,官方网址:www.cdcxhl.com
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
class aa implements ActionListener
{
JButton a, b;
public aa(JButton c, JButton d)
{
a = c;
b = d;
}
public void actionPerformed(ActionEvent e)
{
a.setVisible(false);
b.setVisible(false);
}
}
public class J1 extends JFrame
{
private static final long serialVersionUID = 1L;
JButton j1 = new JButton("Game1");
JButton j2 = new JButton("Game2");
JButton j3 = new JButton("点击摇骰子");
J1()
{
setTitle("Game");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 400);
setVisible(true);
final Container c = getContentPane();
c.setLayout(new FlowLayout());
c.add(j1);
c.add(j2);
j1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
c.add(new JLabel("Game1"));
j3.setVisible(true);
c.add(j3);
}
});
j1.addActionListener(new aa(j1, j2));
j2.addActionListener(new aa(j1, j2));
j3.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int a = (int) (Math.random() * 6);
switch(a)
{
case 0:
c.add(new JLabel("1"));
break;
case 1:
c.add(new JLabel("2"));
break;
case 2:
c.add(new JLabel("3"));
break;
case 3:
c.add(new JLabel("4"));
break;
case 4:
c.add(new JLabel("5"));
break;
case 5:
c.add(new JLabel("6"));
break;
}
SwingUtilities.updateComponentTreeUI(c);
}
});
}
public static void main(String[] args)
{
new J1();
}
}
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class Login {
private JFrame frame = new JFrame("登录");
private Container c = frame.getContentPane();
private JTextField username = new JTextField();
private JPasswordField password = new JPasswordField();
private JButton ok = new JButton("确定");
private JButton cancel = new JButton("取消");
public Login(){
frame.setSize(300,200);
c.setLayout(new BorderLayout());
initFrame();
frame.setVisible(true);
}
private void initFrame() {
//顶部
JPanel titlePanel = new JPanel();
titlePanel.setLayout(new FlowLayout());
titlePanel.add(new JLabel("系统管理员登录"));
c.add(titlePanel,"North");
//中部表单
JPanel fieldPanel = new JPanel();
fieldPanel.setLayout(null);
JLabel l1 = new JLabel("用户名:");
l1.setBounds(50, 20, 50, 20);
JLabel l2 = new JLabel("密 码:");
l2.setBounds(50, 60, 50, 20);
fieldPanel.add(l1);
fieldPanel.add(l2);
username.setBounds(110,20,120,20);
password.setBounds(110,60,120,20);
fieldPanel.add(username);
fieldPanel.add(password);
c.add(fieldPanel,"Center");
//底部按钮
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());
buttonPanel.add(ok);
buttonPanel.add(cancel);
c.add(buttonPanel,"South");
}
public static void main(String[] args){
new Login();
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class aa implements ActionListener {
JButton jb;
public aa(JButton jb) {//构造器,把JButton传递进来,
this.jb = jb;
}
public void actionPerformed(ActionEvent e) {
jb.setVisible(false);//设置jbutton不可见
}
}
public class J1 extends JFrame{
JButton j1 = new JButton("Game1");
JButton j2 = new JButton("Game2");
J1(){
setTitle("Game");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new FlowLayout());
c.add(j1);
c.add(j2);
j1.addActionListener(new aa(j1));//把j1这个按钮传递进过去
j2.addActionListener(new aa(j2));//把j2这个按钮传递进过去
setSize(400,400);
setVisible(true);
}
public static void main(String[] args) {
new J1();
}
}
效果图
效果图
参考代码
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class KeyFrame extends JFrame implements KeyListener{
JLabel jlkey;
public KeyFrame() {
jlkey = new JLabel("请输入字母或者数字,其他字符不显示");
add(jlkey);
addKeyListener(this);
setLayout(new FlowLayout());
setSize(260, 160);
setTitle("输入...");
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new KeyFrame();
}
public void keyTyped(KeyEvent e) {//敲击键盘
c=e.getKeyChar();//得到键入的字符
repaint();//重绘
}
public void keyPressed(KeyEvent e) {//按下键盘
// c=e.getKeyChar();
// repaint();
}
public void keyReleased(KeyEvent e) {//释放键盘
}
char c ;
@Override
public void paint(Graphics g) {
super.paint(g);
//如果只能显示输入的是字母或者数字,那么需要if判断下
if((c='Z' c='A')||(c='z'c='a')||(c='9'c='0')){// 注意比较的是字符 '9' 和字符'0'
Font font = g.getFont();
g.setColor(Color.BLUE);
g.setFont(new Font(font.getName(),font.getStyle(),20));
g.drawString(c+"",100, 100); //绘制
}
}
}
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流