扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
import java.awt.*;
创新互联是一家集网站建设,正定企业网站建设,正定品牌网站建设,网站定制,正定网站建设报价,网络营销,网络优化,正定网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class ReadBook extends JFrame {
JTextArea jta;
JTextField jtf;
JButton jb;
public ReadBook() {
jta = new JTextArea();
jtf = new JTextField(30);
jtf.setText("文件保存路径如c:\\ab.txt");
jb = new JButton("保存文字");
JPanel jp = new JPanel();
jp.add(jtf);
jp.add(jb);
add(jta);
add(jp, BorderLayout.SOUTH);
setBounds(500, 100, 500, 380);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
jb.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//-------------核心代码---------
String path = jtf.getText();
File f = new File(path);
String txt = jta.getText().replaceAll("\n", "\r\n");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(f));
bw.write(txt);//写入文件中
bw.close();
} catch (Exception e1) {
e1.printStackTrace();
}
//-------------核心代码---------
}
});
}
public static void main(String[] args) {
new ReadBook();
}
}
//这是哪个背景类
import java.awt.Image;
import java.awt.*;
import javax.swing.*;
public class ImageBack extends JPanel{
Image im = null;
public imageback(Image im){
this.im=im;
int width = Toolkit.getDefaultToolkit().getScreenSize().width;
int height = Toolkit.getDefaultToolkit().getScreenSize().height;
this.setSize(width,height);
this.setVisible(true);
}
public static void main(String args[]){
}
public void paintComponent(Graphics g){
//清屏
super.paintComponent(g);
g.drawImage(im, 0, 0, this.getWidth(),this.getHeight(),this);
}
}
import javax.swing.*;
import java.io.File;
import java.io.IOException;
import java.net.*;
/**
* 学生管理系统主界面
*/
public class StuMain extends JFrame{
//背景图片
Image image=null;
public StuMain() {
try {
image = ImageIO.read(new File("images/start.jpg"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//把你要的背景图片放到背景面板里
imageback im = new imageback(image);
//把有背景的图片的JPanel放到JFrame里面
this.add(im);
//添加框架的关闭事件处理
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
//设置框架的大小
//设置标题
this.setTitle("学生管理系统");
this.setVisible(true);
}
public static void main(String args[]){
new StuMain();
}
}
如果那不懂随时米我 qq896182794
运行如图
参考代码如下
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class RegDemo extends JFrame implements ActionListener{
JTextField jtf;
JPasswordField jpf;
public RegDemo() {
//组件的创建, 和布局安排
JPanel jpc = new JPanel();//默认流式布局
JPanel jp1 = new JPanel(new GridLayout(2, 2,5,10));//网格布局
jp1.setBorder(BorderFactory.createTitledBorder("用户注册"));
JLabel jl1 = new JLabel("用户名:");
jtf = new JTextField(10);
JLabel jl2 = new JLabel("密码:");
jpf = new JPasswordField(10);
jpf.setEchoChar('*');//用*号来隐藏密码的显示
jp1.add(jl1);jp1.add(jtf);
jp1.add(jl2);jp1.add(jpf);
jpc.add(jp1);
add(jpc);
JButton jb1 = new JButton("提交");
jb1.addActionListener(this);
jb1.setActionCommand("yes");
JButton jb2 = new JButton("取消");
jb2.addActionListener(this);
jb2.setActionCommand("no");
JPanel jp2 = new JPanel();
jp2.add(jb1);jp2.add(jb2);
add(jp2,BorderLayout.SOUTH);
setTitle("用户注册界面");
setSize(280, 280);
setLocationRelativeTo(null);//窗口居中
setDefaultCloseOperation(EXIT_ON_CLOSE);//
setVisible(true);
}
public static void main(String[] args) {
new RegDemo();
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("yes")){
String name = jtf.getText().trim();
String pwd = new String(jpf.getPassword());
if(name.equals("")||pwd.equals("")){
JOptionPane.showMessageDialog(this, "你还没有输入用户名或者密码");
}else{
JOptionPane.showMessageDialog(this, "注册成功!用户名"+name+",密码"+pwd);
}
}else{
jtf.setText("");
jpf.setText("");
}
}
}
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.Border;
class MainFrame extends JFrame {
private static final long serialVersionUID = 1L;
private MapString, Integer sizes = new HashMapString, Integer();
private MapString, Integer styles = new HashMapString, Integer();
private MapString, Integer toppings = new HashMapString, Integer();
public MainFrame() {
sizes.put("Extra Large", 10);
sizes.put("Large", 8);
sizes.put("Medium", 5);
sizes.put("Small", 3);
styles.put("Deep Dish", 20);
styles.put("Regular", 10);
styles.put("Thin Crust", 5);
styles.put("Chicago", 3);
toppings.put("Cheese", 8);
toppings.put("Tomato", 7);
toppings.put("Peppers", 6);
toppings.put("Peperoni", 5);
this.setTitle("布局及事件处理");
this.setSize(450, 350);
this.setLayout(new BorderLayout());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel lblTitle = new JLabel();
lblTitle.setText("Pizzeria Juno");
lblTitle.setFont(new Font("宋体", Font.BOLD, 36));
lblTitle.setHorizontalAlignment(SwingConstants.CENTER);
this.add("North", lblTitle);
JPanel bodyPanel = new JPanel();
bodyPanel.setLayout(new GridLayout(2, 1));
this.add("Center", bodyPanel);
JPanel listPanel = new JPanel();
listPanel.setLayout(new GridLayout(1, 3));
listPanel.setSize(200, 200);
bodyPanel.add(listPanel);
Border lineBorder = BorderFactory.createLineBorder(Color.BLACK);
JPanel sizePanel = new JPanel();
sizePanel.setLayout(new BorderLayout());
listPanel.add(sizePanel);
JLabel sizeTitle = new JLabel();
sizeTitle.setText("Sizes");
sizePanel.add("North", sizeTitle);
JList sizeList = new JList(sizes.keySet().toArray());
sizeList.setSize(100, 100);
sizeList.setBorder(lineBorder);
sizeList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
sizePanel.add(sizeList);
JPanel stylePanel = new JPanel();
stylePanel.setLayout(new BorderLayout());
listPanel.add(stylePanel);
JLabel styleTitle = new JLabel();
styleTitle.setText("Styles");
stylePanel.add("North", styleTitle);
JList styleList = new JList(styles.keySet().toArray());
styleList.setSize(100, 100);
styleList.setBorder(lineBorder);
styleList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
stylePanel.add(styleList);
JPanel toppingPanel = new JPanel();
toppingPanel.setLayout(new BorderLayout());
listPanel.add(toppingPanel);
JLabel toppingTitle = new JLabel();
toppingTitle.setText("Toppings");
toppingPanel.add("North", toppingTitle);
JList toppingList = new JList(toppings.keySet().toArray());
toppingList.setSize(100, 100);
toppingList.setBorder(lineBorder);
toppingPanel.add(toppingList);
JTextArea txtResult = new JTextArea();
txtResult.setEditable(false);
bodyPanel.add(txtResult);
JPanel bottomPanel = new JPanel();
bottomPanel.setLayout(new GridLayout(1, 3));
this.add("South", bottomPanel);
JLabel label1 = new JLabel("Click to complete order");
bottomPanel.add(label1);
JButton btnRingUp = new JButton("Ring up");
btnRingUp.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(sizeList.getSelectedValue() == null) {
JOptionPane.showMessageDialog(MainFrame.this, "Please select size.");
return;
}
if(styleList.getSelectedValue() == null) {
JOptionPane.showMessageDialog(MainFrame.this, "Please select style.");
return;
}
if(toppingList.getSelectedValue() == null) {
JOptionPane.showMessageDialog(MainFrame.this, "Please select topping.");
return;
}
float total = 0;
String size = sizeList.getSelectedValue().toString();
total += sizes.get(size);
String style = styleList.getSelectedValue().toString();
total += styles.get(style);
String result = size + " Pizza, " + style + " Style";
Object[] toppings = toppingList.getSelectedValues();
for(Object topping : toppings) {
result += "\n +" + topping.toString();
total += MainFrame.this.toppings.get(topping.toString());
}
result += "\n Total: " + total;
txtResult.setText(result);
}
});
bottomPanel.add(btnRingUp);
JButton btnQuit = new JButton("Quit");
btnQuit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
MainFrame.this.dispose();
}
});
bottomPanel.add(btnQuit);
}
}
public class App {
public static void main(String[] args) {
MainFrame mainFrame = new MainFrame();
mainFrame.setVisible(true);
}
}
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class vv extends JDialog {
private static final long serialVersionUID = 1L;
private JLabel l_Id = new JLabel("登陆账户", JLabel.CENTER);
private JLabel l_pw = new JLabel("登陆密码", JLabel.CENTER);
private JTextField t_Id = new JTextField(10);
private JPasswordField t_pw = new JPasswordField(10);
private JButton btnLogin;
private JButton btnClose;
public vv() {
super();
setResizable(false);
getContentPane().setBackground(new Color(225, 225, 225));
getContentPane().setLayout(null);
initialize();
}
protected void initialize() {
setTitle("系统登录");
l_Id.setBounds(48, 43, 53, 25);
t_Id.setBounds(110, 43, 150, 25);
l_pw.setBounds(48, 93, 53, 25);
t_pw.setBounds(110, 93, 150, 25);
getContentPane().add(l_Id);
getContentPane().add(l_pw);
getContentPane().add(t_Id);
getContentPane().add(t_pw);
btnLogin = new JButton();
btnLogin.setText("登 录");
btnLogin.setBounds(70, 142, 85, 28);
btnLogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
addBtnLoginActionListener();
}
});
getContentPane().add(btnLogin);
btnClose = new JButton();
btnClose.setText("关 闭");
btnClose.setBounds(175, 142, 85, 28);
btnClose.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dispose();
System.exit(-1);
}
});
getContentPane().add(btnClose);
}
private void addBtnLoginActionListener() {
String user = t_Id.getText();
String password = new String(t_pw.getPassword());
if (user.equals("")) {
JOptionPane.showMessageDialog(this, "帐号不可为空", "Caution",
JOptionPane.WARNING_MESSAGE);
return;
}
if (password.equals("")) {
JOptionPane.showMessageDialog(this, "密码不可为空", "Caution",
JOptionPane.WARNING_MESSAGE);
return;
}
String sql = "select * FROM login WHERE id = '" + user + "' and pw = '"
+ password + "'";
boolean success = false;
// TODO:数据校验 success = executeQuery(sql);
if (success) {
// TODO: 如果数据校验成功 显示主界面 并关闭登录界面
JOptionPane.showMessageDialog(this, "成功登录", "提示",
JOptionPane.INFORMATION_MESSAGE);
this.dispose();
} else {
JOptionPane.showMessageDialog(this, "帐号或密码错误!", "警告",
JOptionPane.WARNING_MESSAGE);
t_pw.requestFocus(); // 密码框选中
}
}
public Dimension getPreferredSize() {
return new Dimension(320, 170);
}
public void show() {
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension screen = tk.getScreenSize();
Dimension d = getSize();
this.setLocation((screen.width - d.width) / 2,
(screen.height - d.height) / 2);
// 输入密码后回车相当于点击了登录按钮
getRootPane().setDefaultButton(btnLogin);
t_pw.requestFocus();
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setSize(300, 220);
super.show();
}
public static void main(String[] args) {
vv loginFrame = new vv();
loginFrame.setVisible(true);
}
}
希望对你有帮助
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流