代码:
package MyAdvancedLayout;
import javax.swing.*;
public class MyBox1 {
JFrame f;
JPanel p;
Box box1,box2,box3,box4;
JButton b11,b21,b31,b12,b22,b32,b13,b23,b33;
public MyBox1(){
f=new JFrame("盒子归一");
p=new JPanel();
b11=new JButton("按钮1");
b21=new JButton("按钮2");
b31=new JButton("按钮3");
b12=new JButton("按钮1");
b22=new JButton("按钮2");
b32=new JButton("按钮3");
b13=new JButton("按钮1");
b23=new JButton("按钮2");
b33=new JButton("按钮3");
//第一个垂直盒子
box1=Box.createVerticalBox();
box1.add(b11);
box1.add(b21);
box1.add(b31);
//第二个垂直盒子
box2=Box.createVerticalBox();
box2.add(b12);
box2.add(Box.createVerticalStrut(10));
box2.add(b22);
box2.add(Box.createVerticalStrut(50));
box2.add(b32);
//第三个垂直盒子
box3=Box.createVerticalBox();
box3.add(b13);
box3.add(Box.createVerticalGlue());
box3.add(b23);
box3.add(Box.createVerticalGlue());
box3.add(b33);
//水平盒子
box4=Box.createHorizontalBox();
box4.add(box1);
box4.add(Box.createHorizontalStrut(50));
box4.add(box2);
box4.add(Box.createHorizontalStrut(50));
box4.add(box3);
p.add(box4);
f.add(p);
f.setVisible(true);
f.setBounds(200,200,400,200);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[]args){
new MyBox1();
}
}
2、教材P441例24.1。(选做题,20分)
代码:
package MyAdvancedLayout;
import java.awt.BorderLayout;
import javax.swing.*;
public class MyBookExample {
JFrame f;
public MyBookExample() {
f=new JFrame("使用箱式布局管理器");
Box topicbox=Box.createHorizontalBox();
f.add(topicbox,BorderLayout.NORTH);
topicbox.add(Box.createHorizontalStrut(5));
JLabel topiclabel=new JLabel("主题:");
topicbox.add(topiclabel);
topicbox.add(Box.createHorizontalStrut(5));
JTextField topictextfield=new JTextField(30);
topicbox.add(topictextfield);
Box box=Box.createVerticalBox();
f.add(box,BorderLayout.CENTER);
box.add(Box.createVerticalStrut(5));
Box contentbox=Box.createHorizontalBox();
contentbox.setAlignmentX(1);
box.add(contentbox);
contentbox.add(Box.createHorizontalStrut(5));
JLabel contentlabel=new JLabel("内容:");
contentlabel.setAlignmentY(0);
contentbox.add(contentlabel);
contentbox.add(Box.createHorizontalStrut(5));
JScrollPane scrollpane=new JScrollPane();
scrollpane.setAlignmentY(0);
contentbox.add(scrollpane);
JTextArea contenttextarea=new JTextArea();
contenttextarea.setLineWrap(true);
scrollpane.setViewportView(contenttextarea);
box.add(Box.createVerticalStrut(5));
JButton submitbutton=new JButton("确定");
submitbutton.setAlignmentX(1);
box.add(submitbutton);
f.setVisible(true);
f.setBounds(300, 200, 300, 300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new MyBookExample();
}
}
3、设计一个简单的字体设置窗口程序FontSet.java,允许用户选择设置字体、字号、字形、颜色等属性。运行结果:(30分)
关键代码及注释如下:
代码:
package MyAdvancedLayout;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class FontSet implements ActionListener{
JFrame f;
JScrollPane sp;
JButton b1,b2;
JLabel l1,l2,l3,l4;
JRadioButton rb1,rb2,rb3;
ButtonGroup bg;
JCheckBox cb1,cb2;
JComboBox cbb;
JList jl;
JTextArea ta;
public FontSet() {
GraphicsEnvironment g=GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] fontName=g.getAvailableFontFamilyNames();
f=new JFrame("字体设计器");
b1=new JButton("退出");
b2=new JButton("编辑");
l1=new JLabel("请选择字体");
l2=new JLabel("请选择字号");
l3=new JLabel("请选择字形");
l4=new JLabel("请选择字色");
rb1=new JRadioButton("红色",true);
rb2=new JRadioButton("绿色");
rb3=new JRadioButton("蓝色");
bg=new ButtonGroup();
bg.add(rb1);
bg.add(rb2);
bg.add(rb3);
cb1=new JCheckBox("加粗");
cb2=new JCheckBox("倾斜");
ta=new JTextArea("这是字体设置的测试文字");
ta.setForeground(Color.RED);
cbb=new JComboBox(fontName);
String[] fontsize = new String[31];
for(int i=0;i<=30;i++) {
int j=i+16;
fontsize[i]=j+"";
}
ta.setFont(new Font("",Font.PLAIN,16));
jl=new JList(fontsize);
sp=new JScrollPane(jl);
//第一个垂直盒子
Box v1=Box.createVerticalBox();
v1.add(l1);
v1.add(cbb);
v1.add(Box.createVerticalStrut(150));
//第二个垂直盒子
Box v2=Box.createVerticalBox();
v2.add(l2);
v2.add(sp);
//第三个垂直盒子
Box v3=Box.createVerticalBox();
v3.add(l3);
v3.add(cb1);
v3.add(cb2);
v3.add(Box.createVerticalGlue());
//第四个垂直盒子
Box v4=Box.createVerticalBox();
v4.add(l4);
v4.add(rb1);
v4.add(rb2);
v4.add(rb3);
v4.add(Box.createVerticalGlue());
//第一个水平盒子
Box h1=Box.createHorizontalBox();
h1.add(Box.createHorizontalStrut(10));
h1.add(v1);
h1.add(Box.createHorizontalStrut(10));
h1.add(v2);
h1.add(Box.createHorizontalStrut(10));
h1.add(v3);
h1.add(Box.createHorizontalStrut(10));
h1.add(v4);
h1.add(Box.createHorizontalStrut(10));
//按钮垂直盒子
Box buttonbox=Box.createVerticalBox();
buttonbox.add(b1);
buttonbox.add(Box.createVerticalStrut(10));
buttonbox.add(b2);
//下方水平盒子
Box h2=Box.createHorizontalBox();
h2.add(ta);
h2.add(Box.createHorizontalStrut(10));
h2.add(buttonbox);
//总垂直盒子
Box vv=Box.createVerticalBox();
vv.add(h1);
vv.add(h2);
b1.addActionListener(this);
b2.addActionListener(this);
f.add(vv,BorderLayout.CENTER);
f.setVisible(true);
f.setBounds(200, 200, 600, 450);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new FontSet();
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("编辑")) {
String font=(String) cbb.getSelectedItem();
String fontsize1=jl.getSelectedValue().toString();
int fontsize=Integer.parseInt(fontsize1);
int fontfont=Font.PLAIN;
if(cb1.isSelected()) {
fontfont+=Font.BOLD;
}
if(cb2.isSelected()) {
fontfont+=Font.ITALIC;
}
if(rb1.isSelected()) {
ta.setForeground(Color.RED);
}else if(rb2.isSelected()) {
ta.setForeground(Color.GREEN);
}else{
ta.setForeground(Color.BLUE);
}
ta.setFont(new Font(font,fontfont,fontsize));
}else {
System.exit(0);
}
}
}
代码:
package MyAdvancedLayout;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class CardExample extends JFrame{
private JPanel cardpanel;
private CardLayout cardlayout;
public static void main(String[] args) {
CardExample frame=new CardExample();
frame.setVisible(true);
}
public CardExample() {
super();
setTitle("使用卡片布局管理器");
setBounds(100,100,600,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
cardlayout =new CardLayout();
cardpanel=new JPanel(cardlayout);
getContentPane().add(cardpanel,BorderLayout.CENTER);
String[]labelnames= {"卡片A","卡片B","卡片C"};
for(int i=0;i
代码:
package MyAdvancedLayout;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class MyCardLayout implements ActionListener{
JFrame f;
JPanel p1,p2;
CardLayout cardlayout;
JLabel[]label=new JLabel[5];
JButton[]button=new JButton[9];
ImageIcon[]icon=new ImageIcon[5];
String[]labelnames={"风景1","风景2","风景3","风景4","风景5"};
String[]buttonnames={"第一张","前一张","风景1","风景2","风景3","风景4","风景5","后一张","最后一张"};
String[]labelpaths={"D:/图片/风景1.jpg","D:/图片/风景2.jpg","D:/图片/风景3.jpg","D:/图片/风景4.jpg","D:/图片/风景5.jpg"};
public MyCardLayout() {
f=new JFrame("图");
cardlayout=new CardLayout();
p1=new JPanel(cardlayout);
p2=new JPanel();
for(int i=0;i
6、电子相册的制作。先用卡片布局先布局4张以上的图片(最想看到的是同学们的成长照片,小学、初中、高中、大学等),然后试分别采用多线程技术以及非多线程技术实现这些图片的自动循环播放。(20分)
代码1:
package MyAdvancedLayout;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import javax.swing.*;
public class MyCardPicture{
JFrame f;
JPanel p1;
CardLayout cardlayout;
JLabel[]label=new JLabel[5];
ImageIcon[]icon=new ImageIcon[5];
String[]labelnames={"风景1","风景2","风景3","风景4","风景5"};
String[]labelpaths={"D:/图片/风景1.jpg","D:/图片/风景2.jpg","D:/图片/风景3.jpg","D:/图片/风景4.jpg","D:/图片/风景5.jpg"};
public MyCardPicture() {
f=new JFrame("图");
cardlayout=new CardLayout();
p1=new JPanel(cardlayout);
for(int i=0;i