0
点赞
收藏
分享

微信扫一扫

java 图形用户界面的设计与实现practice


编程包含一个标签和一个按钮,单击按钮时,标签的内容在"你好"和"再见"之间切换。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Box extends JFrame implements ActionListener{
JLabel im1;
JButton jb=new JButton();
Box(){
im1=new JLabel("你好");
setLayout(new FlowLayout());
add(im1);
add(jb);
jb.addActionListener(this);
validate();
setBounds(150,150,20,200);
setVisible(true);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
if(im1.getText()=="你好") im1.setText("再见");
else im1.setText("你好");
}
}
public class Main {
public static void main(String[] args) {
new Box();
}
}


java 图形用户界面的设计与实现practice_文本标签

编程包含一个文本框和一个按钮,文本框内容改变时,将窗口标题的内容显示文本内容;单击按钮,清空文本区域的内容。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Box extends JFrame implements ActionListener {
JButton bt;
JTextField text;
Box(){
bt=new JButton();
text=new JTextField(15);
setLayout(new FlowLayout());
text.addActionListener(this);
bt.addActionListener(this);
add(text);
add(bt);
validate();
setBounds(100,100,400,200);
setVisible(true);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==text){
this.setTitle(text.getText());
}
else if(e.getSource()==bt){
text.setText("");
}
}
}
public class Main {
public static void main(String[] args) {
new Box();
}
}


java 图形用户界面的设计与实现practice_界面_02

编写一个应用程序,用户可以在一个文本框中里输入数字字符,按ENTER键后将数字放入一个文本区,当输入大于1000时,弹出一个对话框,提示用户数字已经大于1000。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Box extends JFrame implements ActionListener{
JTextField txt;
JTextArea text;
Box(){
setLayout(new FlowLayout());
txt=new JTextField(25);
text=new JTextArea();
txt.addActionListener(this);
add(txt);
add(text);
validate();
setVisible(true);
setBounds(100,100,300,300);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public void actionPerformed(ActionEvent e){
int num=Integer.parseInt(txt.getText());
if(num>1000){
JOptionPane.showMessageDialog(this,"the number is bigger than 1000");
}
else {
text.append(txt.getText()+" ");
}
txt.setText("");
}
}
public class Main {
public static void main(String[] args) {
new Box();
}
}


java 图形用户界面的设计与实现practice_文本标签_03

编程包含一个复选按钮和一个普通按钮,复选按钮选中时,普通按钮的背景色为红色,未选中时为灰色。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Box extends JFrame implements ActionListener {
JButton bot;
JCheckBox chb;
Box(){
bot=new JButton();
bot.setBackground(Color.gray);
chb=new JCheckBox();
chb.addActionListener(this);
setLayout(new FlowLayout());
add(bot);
add(chb);
validate();
setBounds(100,100,200,200);
setVisible(true);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public void actionPerformed(ActionEvent e){
if(chb.isSelected()){
bot.setBackground(Color.red);
}
else bot.setBackground(Color.gray);
}
}
public class Main {
public static void main(String[] args) {
new Box();
}
}


java 图形用户界面的设计与实现practice_设计_04

编程包含一个单选按钮组和一个普通按钮,单选按钮组中包含三个单选,文本说明分别为"普通"、"黑体"和"斜体"。选择文本标签为"普通"的单选按钮时,普通按钮中的文字为普通字体,选择文本标签为"黑体"的单选按钮时,普通按钮中的文字的字体为黑体,选择文本标签为"斜体"的单选按钮时,普通按钮中的文字的字体为斜体。


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Box extends JFrame implements ActionListener{
JButton bot;
ButtonGroup bg;
JRadioButton jb1,jb2,jb3;
Box() {
bot = new JButton("皆大欢喜");
jb1 = new JRadioButton("普通");
jb2 = new JRadioButton("黑体");
jb3 = new JRadioButton("斜体");
jb1.addActionListener(this);
jb2.addActionListener(this);
jb3.addActionListener(this);
bg=new ButtonGroup();
bg.add(jb1);
bg.add(jb2);
bg.add(jb3);
GridLayout f=new GridLayout(4,1);
setLayout(f);
add(bot);
add(jb1);
add(jb2);
add(jb3);
validate();
setVisible(true);
setBounds(100,400,200,200);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==jb1){
bot.setFont(new Font("宋体", Font.PLAIN, 16));
}
else if(e.getSource()==jb2){
bot.setFont(new Font("宋体",Font.BOLD,16));
}
else if(e.getSource()==jb3){
bot.setFont(new Font("宋体",Font.ITALIC,16));
}
}
}
public class Main {
public static void main(String[] args) {
new Box();
}
}



java 图形用户界面的设计与实现practice_界面_05

编程包含一个下拉列表和一个按钮,下拉列表中有10、14、18三个选项。选择10时,按钮中文字的字号为10,选择14时,按钮中文字的字号为14,选择18时,按钮中文字的字号为18。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Box extends JFrame{
JButton bot;
JComboBox list;
Box() {
bot = new JButton("皆大欢喜");
list = new JComboBox();
list.addItem("10");
list.addItem("14");
list.addItem("18");
//list.addActionListener(this);
list.addItemListener((new ItemListener() {
public void itemStateChanged(ItemEvent e) {
String str = (String) list.getSelectedItem();
bot.setFont(new Font("宋体", Font.PLAIN, Integer.parseInt(str)));
}
}));
add(list);
GridLayout f = new GridLayout(4, 1);
setLayout(f);
add(bot);
add(list);
validate();
setVisible(true);
setBounds(100, 400, 200, 200);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
}
public class Main {
public static void main(String[] args) {
new Box();
}
}

java 图形用户界面的设计与实现practice_设计_06






举报

相关推荐

0 条评论