通过按钮来实现页面的转换以及信息的存储
代码(自定义布局类太多了,就不上传了,需要的话要我)
1页:
package my.pages;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import js.swing.JsPanel;
import js.swing.activity.JsActivity;
import js.swing.border.JsBorder;
import js.swing.layout.JsColumnLayout;
import js.swing.layout.JsMarginLayout;
import js.swing.layout.JsRowLayout;
/*
* 第一页
*/
public class Step1Activity extends JsActivity
{
JTextField nameField = new JTextField();
JComboBox<String>sexField = new JComboBox<>();
JTextField birthdayField = new JTextField();
JTextField phoneField = new JTextField();
public Step1Activity()
{
this.setLayout(new BorderLayout());
initTop("基础信息");
initCenter();
initBottom();
}
//顶部
private void initTop(String title)
{
JLabel label = new JLabel(title);
label.setFont(new Font("微软雅黑",Font.PLAIN,18));
this.add(label, BorderLayout.PAGE_START);
label.setOpaque(true);
label.setBackground(Color.WHITE);
label.setBorder(BorderFactory.createMatteBorder(1, 0, 0, 0, new Color(0xCCCCCC)));
JsBorder.addPadding(label, 10);
}
//中心
private void initCenter()
{
JsPanel center = new JsPanel();
center.setLayout(new JsColumnLayout());
if(true)
{
JsPanel row = new JsPanel();
row.setLayout(new JsRowLayout());
center.add(row, "30px");
row.padding(4);
row.add(new JLabel("姓名"), "60px");
row.add(nameField, "1w");
}
if(true)
{
JsPanel row =new JsPanel();
row.setLayout(new JsRowLayout());
center.add(row, "30px");
row.padding(4);
row.add(new JLabel("性别"), "60px");
sexField.addItem("女");
sexField.addItem("男");
row.add(sexField, "1w");
}
if(true)
{
JsPanel row =new JsPanel();
row.setLayout(new JsRowLayout());
center.add(row, "30px");
row.padding(4);
row.add(new JLabel("生日"), "60px");
row.add(birthdayField, "1w");
}
if(true)
{
JsPanel row =new JsPanel();
row.setLayout(new JsRowLayout());
center.add(row, "30px");
row.padding(4);
row.add(new JLabel("联系方式"), "60px");
row.add(phoneField, "1w");
}
center.setPreferredSize(new Dimension(400,9999));
center.setOpaque(true);
center.setBackground(new Color(0,0,0,0));
JPanel wrapper = new JPanel();
wrapper.setLayout(new JsMarginLayout());
wrapper.add(center, new JsMarginLayout.Margin(20, -1, -1, -1));
wrapper.setBorder(BorderFactory.createMatteBorder(1, 0, 1, 0, new Color(0xCCCCCC)));
this.add(wrapper, BorderLayout.CENTER);
}
//按钮
private void initBottom()
{
JPanel bottom = new JPanel();
this.add(bottom, BorderLayout.PAGE_END);
JButton next = new JButton("下一步");
bottom.add(next);
next.addActionListener( (e)->{
saveData();
startActivity(Step2Activity.class, null);
});
}
@Override
public void onCreate(Object intent) {
// TODO Auto-generated method stub
}
@Override
public void onStart() {
// TODO Auto-generated method stub
}
//上下文
private void saveData()
{
Resume r = (Resume) context.getParam("resume", null);
r.name = nameField.getText().trim();
r.sex = sexField.getSelectedIndex() == 1;
r.birthday = birthdayField.getText().trim();
r.cellphone = phoneField.getText().trim();
}
}
2页:
package my.pages;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Image;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.filechooser.FileNameExtensionFilter;
import js.swing.JsPanel;
import js.swing.JsToaster;
import js.swing.activity.JsActivity;
import js.swing.border.JsBorder;
import js.swing.image.JsImageView;
import js.swing.layout.JsColumnLayout;
import js.swing.layout.JsMarginLayout;
/*
* 第二页
*/
public class Step2Activity extends JsActivity
{
JsImageView photoField = new JsImageView();
File imageFile; //选中图片的文件
public Step2Activity()
{
this.setLayout(new BorderLayout());
initTop("头像设置");
initCenter();
initBottom();
}
//顶部
private void initTop(String title)
{
JLabel label = new JLabel(title);
label.setFont(new Font("微软雅黑",Font.PLAIN,18));
this.add(label, BorderLayout.PAGE_START);
label.setOpaque(true);
label.setBackground(Color.WHITE);
label.setBorder(BorderFactory.createMatteBorder(1, 0, 0, 0, new Color(0xCCCCCC)));
JsBorder.addPadding(label, 10);
}
//中心
private void initCenter()
{
JsPanel center = new JsPanel();
center.setLayout(new JsColumnLayout(10));
center.setPreferredSize(new Dimension(200, 240));
center.add(photoField, "200px");
JButton button = new JButton ("选择图片");
center.add(button, "1w");
JPanel wrapper = new JPanel();
wrapper.setLayout(new JsMarginLayout());
wrapper.add(center, new JsMarginLayout.Margin(20,-1,-1,-1));
wrapper.setBorder(BorderFactory.createMatteBorder(1, 0, 1, 0, new Color(0xCCCCCC)));
this.add(wrapper, BorderLayout.CENTER);
button.addActionListener( (e)->{
openFile();
});
}
//底部
private void initBottom()
{
JPanel bottom = new JPanel();
this.add(bottom, BorderLayout.PAGE_END);
JButton preButton = new JButton("上一步");
bottom.add(preButton);
preButton.addActionListener((e)->{
back();
});
JButton nextButton = new JButton("下一步");
bottom.add(nextButton);
nextButton.addActionListener( (e)->{
// if(!ispicture())
// JOptionPane.showMessageDialog(this, "请上传照片!");
// else
// {
saveData();
startActivity(Step3Activity.class, null);
//}
});
}
@Override
public void onCreate(Object intent) {
// TODO Auto-generated method stub
}
@Override
public void onStart()
{
Resume r = (Resume) context.getParam("resume", null);
if(r.photo!=null)
{
openFile(r.photo);
}
}
private boolean ispicture()
{
if(imageFile==null) return false;
else return true;
}
private void openFile()
{
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter
= new FileNameExtensionFilter("图片文件", "jpg", "jpeg", "png");
chooser.setFileFilter(filter);
int ret = chooser.showOpenDialog(this);
if (ret == JFileChooser.APPROVE_OPTION)
{
// 结果为:已经存在的一个文件
File file = chooser.getSelectedFile();
openFile(file);
}
}
private void openFile(File file)
{
try {
Image image = ImageIO.read(file);
imageFile = file;
photoField.setImage( image );
saveData();
}catch(Exception e)
{
JsToaster.show(this, JsToaster.Leval.WARN, e.getMessage());
}
}
private void saveData()
{
Resume r = (Resume) context.getParam("resume", null);
r.photo = imageFile;
}
}
3页:
package my.pages;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.TextArea;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import js.swing.activity.JsActivity;
import js.swing.border.JsBorder;
import js.swing.layout.JsMarginLayout;
public class Step3Activity extends JsActivity
{
JTextArea textarea = new JTextArea();
public Step3Activity()
{
this.setLayout(new BorderLayout());
initTop("自我介绍");
initCenter();
initBottom();
}
private void initTop(String title)
{
JLabel label = new JLabel(title);
label.setFont(new Font("微软雅黑", Font.PLAIN, 18));
this.add(label, BorderLayout.PAGE_START);
label.setOpaque(true);
label.setBackground(Color.WHITE);
label.setBorder(BorderFactory.createMatteBorder(1, 0, 0, 0, new Color(0xCCCCCC)));
JsBorder.addPadding(label, 10);
}
private void initCenter()
{
textarea.setPreferredSize(new Dimension(9999,9999));
JPanel wrapper = new JPanel();
wrapper.setLayout(new JsMarginLayout());
wrapper.add(textarea, new JsMarginLayout.Margin(4,4,4,4));
wrapper.setBorder(BorderFactory.createMatteBorder(1, 0, 1, 0, new Color(0xCCCCCC)));
this.add(wrapper, BorderLayout.CENTER);
}
private void initBottom()
{
JPanel bottom = new JPanel();
this.add(bottom, BorderLayout.PAGE_END);
JButton finishButton = new JButton("完成");
bottom.add(finishButton);
finishButton.addActionListener( (e)->{
saveData();
startActivity(SummaryActivity.class, null);
});
}
@Override
public void onCreate(Object intent) {
// TODO Auto-generated method stub
}
@Override
public void onStart() {
// TODO Auto-generated method stub
}
public void saveData()
{
Resume r = (Resume) context.getParam("resume", null);
r.introduce= textarea.getText();
}
}
总结页:
package my.pages;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import js.swing.JsToaster;
import js.swing.activity.JsActivity;
public class SummaryActivity extends JsActivity
{
public SummaryActivity()
{
this.setLayout(new BorderLayout());
JLabel label = new JLabel("已经处理你的简历,请等待通知...");
label.setFont(new Font("宋体 ", Font.PLAIN, 20));
label.setOpaque(true);
label.setBackground(new Color(0xFFFFFF));
label.setHorizontalAlignment(SwingConstants.CENTER);
this.add(label, BorderLayout.CENTER);
}
@Override
public void onCreate(Object intent) {
// TODO Auto-generated method stub
}
@Override
public void onStart()
{
Resume r = (Resume) context.getParam("resume", null);
// 处理 ...
JsToaster.show(this, "处理简历...");
}
}
信息:
package my.pages;
import java.io.File;
/* 表格里的数据项
*
*/
public class Resume
{
public String name;
public boolean sex;
public String birthday;
public String cellphone;
public File photo; // 照片
public String introduce; // 自我介绍
}
MyFrame:
package my;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import js.swing.activity.JsActivityPane;
import my.pages.Resume;
import my.pages.Step1Activity;
import my.pages.Step2Activity;
public class MyFrame extends JFrame
{
JsActivityPane activityPane=new JsActivityPane();
public MyFrame(String title)
{
super(title);
// Content Pane
JPanel root = new JPanel();
this.setContentPane(root);
root.setLayout(new BorderLayout());
root.add(activityPane, BorderLayout.CENTER);
//开始导航
activityPane.putParam("resume", new Resume());
activityPane.startActivity(Step1Activity.class, null);
}
}