GUI编程基础02Swing(了解)

public void init(){

JFrame jFrame = new JFrame(“这是一个JFrame窗口”);

jFrame.setVisible(true);

jFrame.setBounds(100,100,200,200);

jFrame.setBackground(Color.black);

//设置文字

JLabel jLabel = new JLabel(“JFrameDemo”);

jFrame.add(jLabel);

//文本居中

jLabel.setHorizontalAlignment(SwingConstants.CENTER);

//容器实例化

Container contentPane = jFrame.getContentPane();

contentPane.setBackground(Color.yellow);

//关闭事件

jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

}

public static void main(String[] args) {

//建立一个窗口

new JFrameDemo().init();

}

}

在这里插入图片描述

弹窗

import javax.swing.*;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

public class DialogDemo extends JFrame {

public DialogDemo() {

this.setVisible(true);

this.setSize(700, 500);

this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

//JFrame 放东西

Container container = this.getContentPane();

//绝对布局

container.setLayout(null);

//按钮

JButton jButton = new JButton(“点击弹出一个对话框”);

jButton.setBounds(30, 30, 200, 50);

//点击按钮的时候弹出弹窗

jButton.addActionListener(new ActionListener() {//监听器

@Override

public void actionPerformed(ActionEvent e) {

//弹窗

new MyDialogDemo();

}

});

container.add(jButton);

}

public static void main(String[] args) {

new DialogDemo();

}

}

class MyDialogDemo extends JDialog {

public MyDialogDemo() {

this.setVisible(true);

this.setBounds(100, 100, 500, 500);

Container container = this.getContentPane();

container.add(new JLabel(“警告⚠”));

}

}

在这里插入图片描述

标签

图标icon

import javax.swing.*;

import java.awt.*;

//图标

public class IconDemo extends JFrame implements Icon {

private int width;

private int height;

public IconDemo() {

//无参构造

}

public IconDemo(int width, int height) {

//有参构造

this.width = width;

this.height = height;

}

public void init() {

IconDemo iconDemo = new IconDemo(15, 15);

//图标放在标签上,也可以放在按钮上

JLabel jLabel = new JLabel(“icontest”, iconDemo, SwingConstants.CENTER);

Container container = getContentPane();

container.add(jLabel);

this.setVisible(true);

this.pack();

}

public static void main(String[] args) {

new IconDemo().init();

}

@Override

public void paintIcon(Component c, Graphics g, int x, int y) {

g.fillOval(x, y, width, height);

}

@Override

public int getIconWidth() {

return this.width;

}

@Override

public int getIconHeight() {

return this.height;

}

}

在这里插入图片描述

图片icon

import javax.swing.*;

import java.awt.*;

import java.net.URL;

public class ImageIconDemo extends JFrame {

public ImageIconDemo() {

//获取图片地址

JLabel jLabel = new JLabel(“ImageIcon”);

URL url = ImageIconDemo.class.getResource(“tx.jpg”);

ImageIcon imageIcon = new ImageIcon(url);

jLabel.setIcon(imageIcon);

jLabel.setHorizontalAlignment(SwingConstants.CENTER);

Container container = getContentPane();

container.add(jLabel);

setVisible(true);

setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

setBounds(100,100,500,500);

}

public static void main(String[] args) {

new ImageIconDemo();

}

}

在这里插入图片描述

面板

import javax.swing.*;

import java.awt.*;

public class JPanelDemo extends JFrame {

public JPanelDemo() {

Container container = this.getContentPane();

container.setLayout(new GridLayout(2,1,10,10));//10上下间距

JPanel jPanel1 = new JPanel(new GridLayout(1,3));

jPanel1.add(new JButton(“1”));

jPanel1.add(new JButton(“1”));

jPanel1.add(new JButton(“1”));

container.add(jPanel1);

this.setVisible(true);

this.setSize(500,500);

this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

}

public static void main(String[] args) {

new JPanelDemo();

}

}

在这里插入图片描述

JScrollPanel(滚动条)

import javax.swing.*;

import java.awt.*;

public class JScrollDemo extends JFrame {

public JScrollDemo() {

Container container = this.getContentPane();

//文本域

JTextArea jTextArea = new JTextArea(20,50);

jTextArea.setText(“欢迎”);

JScrollPane jScrollPane = new JScrollPane(jTextArea);

container.add(jScrollPane);

this.setVisible(true);

this.setBounds(100,100,300,350);

this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

}

public static void main(String[] args) {

new JScrollDemo();

}

}

在这里插入图片描述

按钮

图片按钮

import javax.swing.*;

import java.awt.*;

import java.net.URL;

public class JButtonDemo01 extends JFrame {

public JButtonDemo01() {

Container container = this.getContentPane();

//将一个图片变为图标

URL resource = JButtonDemo01.class.getResource(“tx.jpg”);

ImageIcon imageIcon = new ImageIcon(resource);

//把这个图标放在按钮上

JButton jButton = new JButton();

jButton.setIcon(imageIcon);//设置图标

jButton.setToolTipText(“这是一个图片按钮”);

container.add(jButton);

this.setVisible(true);

this.setSize(500,300);

this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

}

public static void main(String[] args) {

new JButtonDemo01();

}

}

在这里插入图片描述

单选按钮

import javax.swing.*;

import java.awt.*;

import java.net.URL;

public class JButtonDemo02 extends JFrame {

public JButtonDemo02() {

Container container = this.getContentPane();

//将一个图片变为图标

URL resource = JButtonDemo01.class.getResource(“tx.jpg”);

ImageIcon imageIcon = new ImageIcon(resource);

//单选框

JRadioButton jRadioButton01 = new JRadioButton(“JRadioButton01”);

JRadioButton jRadioButton02 = new JRadioButton(“JRadioButton02”);

JRadioButton jRadioButton03 = new JRadioButton(“JRadioButton03”);

//单选框只能选择一个,要分组,一个组中只能选择一个

ButtonGroup buttonGroup = new ButtonGroup();

buttonGroup.add(jRadioButton01);

buttonGroup.add(jRadioButton02);

buttonGroup.add(jRadioButton03);

container.add(jRadioButton01,BorderLayout.CENTER);

container.add(jRadioButton02,BorderLayout.NORTH);

container.add(jRadioButton03,BorderLayout.SOUTH);

this.setVisible(true);

this.setSize(500,300);

this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

}

public static void main(String[] args) {

new JButtonDemo02();

}

}

在这里插入图片描述

其中,因单选框只能选择一个,故需要将button分组

复选按钮

import javax.swing.*;

import java.awt.*;

import java.net.URL;

public class JButtonDemo03 extends JFrame {

public JButtonDemo03() {

Container container = this.getContentPane();

//将一个图片变为图标

URL resource = JButtonDemo01.class.getResource(“tx.jpg”);

ImageIcon imageIcon = new ImageIcon(resource);

//复选框

JCheckBox jCheckBox1 = new JCheckBox(“jCheckBox1”);

JCheckBox jCheckBox2 = new JCheckBox(“jCheckBox2”);

container.add(jCheckBox1,BorderLayout.NORTH);

container.add(jCheckBox2,BorderLayout.SOUTH);

this.setVisible(true);

this.setSize(500, 300);

this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

}

public static void main(String[] args) {

new JButtonDemo03();

}

}

在这里插入图片描述

列表

下拉框

简陋的下拉框:

import javax.swing.*;

import java.awt.*;

public class TestComboboxDemo01 extends JFrame {

public TestComboboxDemo01() {

Container container = this.getContentPane();

JComboBox status = new JComboBox();

status.addItem(null);

status.addItem(“正在热映”);

status.addItem(“已下架”);

status.addItem(“即将上映”);

this.setVisible(true);

this.setSize(500, 300);

this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

}

public static void main(String[] args) {

new TestComboboxDemo01();

}

}

在这里插入图片描述

简单列表框

import javax.swing.*;

import java.awt.*;

import java.util.Vector;

public class TestComboboxDemo02 extends JFrame {

public TestComboboxDemo02() {

Container container = this.getContentPane();

//生成列表的内容

//String[] contents = {“1”,“2”,“3”};

Vector contents = new Vector();

JList jList = new JList(contents);

contents.add(“zhangsan”);

contents.add(“lisi”);

javaGUI图形界面 public class login extends JFrame { private JComboBox nameJComboBox; private JPanel userJPanel; private JLabel pictureJLabel; private JButton okJButton,cancelJButton; private JLabel nameJLabel,passwordJLabel,note; private JPasswordField passwordJPasswordField; private String name1; private String password1; private String user; private ImageIcon myImageIcon; public login( ) { createUserInterface(); // 调用创建用户界面方法 } private void createUserInterface() { Container contentPane = getContentPane(); contentPane.setLayout( null ); userJPanel = new JPanel(); userJPanel.setBounds( 35, 120, 300, 96 ); userJPanel.setBorder(BorderFactory.createEtchedBorder() ); //显示一圈边儿 userJPanel.setLayout( null ); contentPane.add( userJPanel ); nameJComboBox = new JComboBox(); nameJComboBox.setBounds( 100, 12, 170, 25 ); nameJComboBox.addItem( "admin" ); nameJComboBox.addItem( "aloie" ); nameJComboBox.setSelectedIndex( 0 ); nameJComboBox.setEditable(true); userJPanel.add( nameJComboBox ); pictureJLabel=new JLabel(); pictureJLabel.setBounds(45,0,380,118); pictureJLabel.setIcon(new ImageIcon("pic.gif")); contentPane.add(pictureJLabel); nameJLabel=new JLabel("姓 名:"); nameJLabel.setBounds(20,12,80,25); userJPanel.add(nameJLabel); passwordJPasswordField=new JPasswordField(); passwordJPasswordField.setBounds(100,60,170,25); userJPanel.add(passwordJPasswordField); passwordJLabel=new JLabel("密 码:"); passwordJLabel.setBounds(20,60,80,25); userJPanel.add(passwordJLabel); note=new JLabel("密码与用户名相同"); note.setBounds(0,295,180,25); add(note); okJButton=new JButton("登 陆"); okJButton.setBounds(60,250,80,25); contentPane.add(okJButton); okJButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { okJButtonActionPerformed(event); } } ); cancelJButton=new JButton("取 消"); cancelJButton.setBounds(210,250,80,25); contentPane.add(cancelJButton); cancelJButton.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent event ) { System.exit(0); //退出登陆 } } ); setTitle( "登陆窗口" ); setSize( 380, 350 ); setResizable( false ); //将最大化按钮设置为不可用 } private void okJButtonActionPerformed( ActionEvent event ) { //okJButton响应事件,检查用户名和密码的匹配 name1= nameJComboBox.getSelectedItem().toString(); if (name1.equals("admin") ) { if (passwordJPasswordField.getText().equals("admin")) { showNewWindow(); setVisible( false); } else { JOptionPane.showMessageDialog( this,"密码错误,拒绝登陆", "密码错误 !", JOptionPane.ERROR_MESSAGE ); } } else if (name1.equals("aloie")) { if ( passwordJPasswordField.getText().equals("aloie") ) { showNewWindow(); setVisible(false); } else { JOptionPane.showMessageDialog( this,"密码错误,拒绝登陆", "密码错误 !", JOptionPane.ERROR_MESSAGE ); } } } public void showNewWindow() { JFrame jf=new JFrame("main Frame"); jf.setSize(500,400); jf.setVisible(true); jf.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } public static void main( String[] args ) { JFrame.setDefaultLookAndFeelDecorated(true); login mylogin = new login( ); mylogin.setVisible( true ); mylogin.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值