import?java.awt.*;
創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),正定企業(yè)網(wǎng)站建設(shè),正定品牌網(wǎng)站建設(shè),網(wǎng)站定制,正定網(wǎng)站建設(shè)報價,網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,正定網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學習、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實用型網(wǎng)站。
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.*;
/**
* 學生管理系統(tǒng)主界面
*/
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);
//添加框架的關(guān)閉事件處理
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
//設(shè)置框架的大小
//設(shè)置標題
this.setTitle("學生管理系統(tǒng)");
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()?{
//組件的創(chuàng)建,?和布局安排
JPanel?jpc?=?new?JPanel();//默認流式布局
JPanel?jp1?=?new?JPanel(new?GridLayout(2,?2,5,10));//網(wǎng)格布局
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("系統(tǒng)登錄");
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("關(guān) 閉");
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:數(shù)據(jù)校驗 success = executeQuery(sql);
if (success) {
// TODO: 如果數(shù)據(jù)校驗成功 顯示主界面 并關(guān)閉登錄界面
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);
}
}
希望對你有幫助