本文目录
- 求JAVA编写打字游戏源代码!
- java求一下用swing写的代码
- 急需一个java源码,是最基础的swing 界面设计,sql server数据库连接的小项目
- java图形界面设计实验,求源代码
- 求编写一个超级简单的Java的程序源代码
- java swing左边树图,当点击任意节点时,右边显示显示数据, 求一个简单的源码例子
- java swing 日历控件怎么实现 最好是源码
- 求java源代码使用swing或AWT实现功能:点击按钮,选择一个txt文本文件,并将txt中
求JAVA编写打字游戏源代码!
package chen;import java.awt.*;import java.awt.event.*;import java.awt.image.BufferedImage;import java.util.Vector;import javax.swing.JFrame;public class keyTest extends Window implements KeyListener, Runnable { int width, height; BufferedImage buf; Graphics gc; boolean play = true; Vector《oneChar》 keys = new Vector《oneChar》(); int ok = 0, fail = 0, error = 0, sum = 0; Font small = new Font("宋体", 0, 30); Font big = new Font("宋体", 0, 50); long time = System.currentTimeMillis(); keyTest(Frame f) { super(f); Dimension s = getToolkit().getScreenSize(); width = (int) s.getWidth(); height = (int) s.getHeight(); this.setBounds(0, 0, width, height); this.buf = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); gc = buf.getGraphics(); this.setVisible(true); this.setAlwaysOnTop(true); this.buf = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); gc = buf.getGraphics(); new Thread(this).start(); } public static void main(String s) { JFrame help = new JFrame("打字练习"); help.setVisible(true); help.setDefaultCloseOperation(3); help.addKeyListener(new keyTest(help)); } public void keyTyped(KeyEvent e) { } public synchronized void keyPressed(KeyEvent e) { if (e.getKeyCode() == 27) { play = false; this.dispose(); System.exit(0); } char s = e.getKeyChar(); if (s 》= ’a’ && s 《= ’z’ || (s 》= ’A’ && s 《= ’Z’)) { String l = "" + s; for (int i = 0; i 《 keys.size(); i++) { if (l.equals(((oneChar) keys.elementAt(i)).s)) { keys.removeElementAt(i); ok++; return; } } error++; } } public void keyReleased(KeyEvent e) { } @Override public void update(Graphics g) { gc.setColor(Color.BLACK); gc.fillRect(0, 0, width, height); gc.setColor(Color.red); int l = (ok + error) 》 0 ? (ok * 100 / (ok + error)) : 100; gc.setFont(small); gc.drawString("成功:" + ok + " 错误:" + error + " 失败:" + fail + " 正确率:" + l + "% 时间:" + (System.currentTimeMillis() - time) / 1000, 10, height - 30); gc.setFont(big); oneChar o; for (int i = 0; i 《 keys.size(); i++) { o = keys.elementAt(i); gc.setColor(o.c); gc.drawString(o.s, o.x, o.y += 6); if (o.y 》 height - 10) { fail++; keys.removeElementAt(i); } } g.drawImage(buf, 0, 0, null); } public void run() { while (play) { try { sum++; if (sum % 5 == 0) { newchar(); } Thread.sleep(80); repaint(); } catch (InterruptedException ex) { ex.printStackTrace(); } } } private void newchar() { keys.add(oneChar.getinstance(width)); }}package chen;import java.awt.Color;public class oneChar { static java.util.Random r = new java.util.Random(); public static oneChar getinstance(int maxX) { oneChar a = new oneChar(); int b = r.nextInt(26); a.s = "" + (char) (b + (r.nextInt(4) 》 1 ? ’a’ : ’A’)); a.x = r.nextInt(maxX - 30); a.c = new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256)); return a; } int x, y; Color c; String s;}
java求一下用swing写的代码
帮你写好了!不懂追问!
import javax.swing.BorderFactory;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JTextField;public class Login extends JPanel { public Login(){ setLayout(null); JPanel panel=new JPanel(); panel.setLayout(null); panel.setBorder(BorderFactory.createTitledBorder("登陆")); panel.setBounds(10, 10, 310, 120); add(panel); JLabel nameLabel=new JLabel("姓名"); nameLabel.setBounds(15, 30, 40, 25); panel.add(nameLabel); JTextField nameTextField=new JTextField(); nameTextField.setBounds(55,30,240,30); panel.add(nameTextField); JLabel pwdLabel=new JLabel("密码"); pwdLabel.setBounds(15, 70, 40, 25); panel.add(pwdLabel); JTextField pwdTextField=new JTextField(); pwdTextField.setBounds(55,70,240,30); panel.add(pwdTextField); JButton ok=new JButton("确定"); ok.setBounds(100, 170, 70,30); add(ok); JButton cancel=new JButton("取消"); cancel.setBounds(175, 170, 70,30); add(cancel); } public static void main(String args) { JPanel p=new Login(); JFrame jf=new JFrame("用户登陆"); jf.add(p); jf.setLocation(500,100); jf.setSize(350, 250); jf.setVisible(true); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }}
急需一个java源码,是最基础的swing 界面设计,sql server数据库连接的小项目
我代码库里有。
运行结果:
源码下载:mysql-demo2.rar
代码如下:
import java.awt.BorderLayout;import java.awt.event.*;import java.sql.*;import java.util.Vector;import javax.swing.*;import javax.swing.table.*;class FrmMain extends JFrame { private static final long serialVersionUID = 1L; private JTable productTable; private Vector《Vector《Object》》 productData; public FrmMain() { this.setTitle("产品列表"); this.setSize(600, 600); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setLayout(new BorderLayout()); JToolBar toolbar = new JToolBar(); JButton btnRefresh = new JButton("刷新"); btnRefresh.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { reloadProductList(); } }); toolbar.add(btnRefresh); this.add(toolbar, BorderLayout.NORTH); TableColumnModel columnModel = new DefaultTableColumnModel(); TableColumn column = new TableColumn(0, 50); column.setHeaderValue("编号"); columnModel.addColumn(column); column = new TableColumn(1, 100); column.setHeaderValue("名称"); columnModel.addColumn(column); column = new TableColumn(1, 100); column.setHeaderValue("数量"); columnModel.addColumn(column); this.productData = new Vector《Vector《Object》》(); Vector《Object》 columnsData = new Vector《Object》(); columnsData.add("编号"); columnsData.add("名称"); columnsData.add("数量"); this.productTable = new JTable(productData, columnsData); this.add(new JScrollPane(productTable), BorderLayout.CENTER); reloadProductList(); } // 加载产品数据 private void reloadProductList() { this.productData.clear(); try { Class.forName("com.mysql.jdbc.Driver"); // 数据库用户 String user = "root"; // 数据库密码 String password = ""; Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db_sale", user, password); Statement stmt = conn.createStatement(); // 查询 , 从数据库 db_sale 的 product 表中查询 id, name, qty 字段 ResultSet rs = stmt.executeQuery("SELECT id, name, qty FROM product"); while (rs.next()) { Vector《Object》 product = new Vector《Object》(); product.add(rs.getLong(1)); product.add(rs.getString(2)); product.add(rs.getInt(3)); this.productData.add(product); productTable.updateUI(); } rs.close(); stmt.close(); conn.close(); } catch (Exception e) { e.printStackTrace(); } }}public class App { public static void main(String args) { new FrmMain().setVisible(true); }}
java图形界面设计实验,求源代码
importjavax.swing.*;
importjava.awt.*;
importjava.awt.event.ActionEvent;
importjava.awt.event.;
publicclassextendsJFrameimplements{
JPaneljp1,jp2;
JTextFieldjt1,jt2,jt3;
JButtonbtn_add,btn_sub,btn_clean;
public(){
init();
}
publicvoidinit(){
setTitle("简易计算器");
(null);
setSize(600,100);
jp1=newJPanel();
jp2=newJPanel();
add(jp1,.NORTH);
add(jp2,.SOUTH);
jt1=newJTextField(15);
jp1.add(jt1);
jt2=newJTextField(15);
jp1.add(jt2);
jt3=newJTextField(15);
jp1.add(jt3);
btn_add=newJButton("");
btn_add.(this);
jp2.add(btn_add);
btn_sub=newJButton("-");
btn_sub.(this);
jp2.add(btn_sub);
btn_clean=newJButton("清除");
btn_clean.(this);
jp2.add(btn_clean);
setVisible(true);
}
publicvoid(ActionEvente)
{
if(e.getSource()==btn_clean)
{
jt1.setText("");
jt2.setText("");
jt3.setText("");
}
else
{
if(jt1.getText().equals("")||jt2.getText().equals(""))
{
JOptionPane.(null,"请在前两个框输入数字");
}
else
{
doublenumber1=Double.parseDouble(jt1.getText());
doublenumber2=Double.parseDouble(jt2.getText());
doubleresult=0;
if(e.getSource()==btn_add)
{
result=number1number2;
}
else
{
result=number1-number2;
}
jt3.setText(""result);
}
}
}
}
求编写一个超级简单的Java的程序源代码
import java.awt.FlowLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JPasswordField;import javax.swing.JTextField;public class Login { public static void main(String args) { LoginFrm frame = new LoginFrm(); }}class LoginFrm extends JFrame implements ActionListener{ JLabel nameLabel=new JLabel("用户名:"); JLabel pwdLabel=new JLabel("密码:"); JTextField name=new JTextField(10); JPasswordField password=new JPasswordField(10); JButton butnSure=new JButton("确定"); JButton butnCancel=new JButton("取消"); public LoginFrm() { super("登陆"); setBounds(500, 200, 280, 220); setVisible(true); setLayout(null); nameLabel.setBounds(45, 20, 100, 25); add(nameLabel); add(name); name.setBounds(105, 20, 110, 25); add(pwdLabel); pwdLabel.setBounds(45, 60, 100, 25); add(password); password.setBounds(105, 60, 110, 25); add(butnSure); butnSure.setBounds(45, 100, 80, 25); add(butnCancel); butnCancel.setBounds(135, 100, 80, 25); butnSure.addActionListener(this); butnCancel.addActionListener(this); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); validate();//刷新 } public void actionPerformed(ActionEvent e) { if (e.getSource() ==butnSure){ System.out.println("用户名:"+name.getText()); System.out.println("密码:"+name.getText()); if("admin".equals(name.getText().trim())&&"123".equals(password.getText().trim())){ this.dispose(); new MainFrm("用户界面",name.getText().trim(),password.getText().trim()); }else { JOptionPane.showMessageDialog(this, "用户不存在"); } }else if(e.getSource()==butnCancel){ System.exit(1); } } class MainFrm extends JFrame{ private JLabel info; public MainFrm(String s,String name,String password) { super(s); setBounds(400, 200, 500, 400); setLayout(new FlowLayout()); info=new JLabel("登陆成功,用户名:"+name+",密码:"+password); add(info); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); validate(); } }}
java swing左边树图,当点击任意节点时,右边显示显示数据, 求一个简单的源码例子
import java.awt.BorderLayout;import java.awt.Container;import java.awt.Dimension;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JTree;import javax.swing.event.TreeSelectionEvent;import javax.swing.event.TreeSelectionListener;import javax.swing.tree.DefaultMutableTreeNode;public class TestSwingTree extends JFrame {private JPanel p;public TestSwingTree(String title){super(title);}public void init(){Container c = this.getContentPane();DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");DefaultMutableTreeNode child1 = new DefaultMutableTreeNode("child1");DefaultMutableTreeNode child11 = new DefaultMutableTreeNode("child11");DefaultMutableTreeNode child12 = new DefaultMutableTreeNode("child12");DefaultMutableTreeNode child2 = new DefaultMutableTreeNode("child2");DefaultMutableTreeNode child3 = new DefaultMutableTreeNode("child3");DefaultMutableTreeNode child31 = new DefaultMutableTreeNode("child31");root.add(child1);root.add(child2);root.add(child3);child1.add(child11);child1.add(child12);child3.add(child31);JTree tree = new JTree(root);tree.setPreferredSize(new Dimension(120, 400));tree.addTreeSelectionListener(new TreeSelectionListener() {public void valueChanged(TreeSelectionEvent e) {p.removeAll();JLabel l = new JLabel(e.getPath().toString());l.setBounds(5, 190, 170, 20);p.add(l);p.repaint();}});c.add(tree,BorderLayout.WEST);p = new JPanel();p.setLayout(null);p.setPreferredSize(new Dimension(180, 400));c.add(p,BorderLayout.CENTER);this.setLocation(400, 300);this.setSize(300, 400);this.setResizable(false);this.setVisible(true);this.setDefaultCloseOperation(this.DISPOSE_ON_CLOSE);}public static void main(String args) {new TestSwingTree("Test Swing Jtree").init();}}
java swing 日历控件怎么实现 最好是源码
源代码:
//DatePicker.java
package com.kxsoft.component;
import java.awt.*;
import java.awt.event.*;
import java.util.GregorianCalendar;
import java.util.Date;
import java.util.Calendar;
import java.text.DateFormat;
import java.text.FieldPosition;
import javax.swing.*;
import javax.swing.plaf.BorderUIResource;
public final class DatePicker extends JPanel {
private static final long serialVersionUID = 1L;
private static final int startX = 10;
private static final int startY = 60;
private static final Font smallFont = new Font("Dialog", Font.PLAIN, 10);
private static final Font largeFont = new Font("Dialog", Font.PLAIN, 12);
private static final Insets insets = new Insets(2, 2, 2, 2);
private static final Color highlight = new Color(255, 255, 204);
private static final Color white = new Color(255, 255, 255);
private static final Color gray = new Color(204, 204, 204);
private Component selectedDay = null;
private GregorianCalendar selectedDate = null;
private GregorianCalendar originalDate = null;
private boolean hideOnSelect = true;
private final JButton backButton = new JButton();
private final JLabel monthAndYear = new JLabel();
private final JButton forwardButton = new JButton();
private final JLabel{
new JLabel("日"),
new JLabel("一"),
new JLabel("二"),
new JLabel("三"),
new JLabel("四"),
new JLabel("五"),
new JLabel("六")};
private final JLabel{
{new JLabel(),
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel()},
{new JLabel(),
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel()},
{new JLabel(),
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel()},
{new JLabel(),
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel()},
{new JLabel(),
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel()},
{new JLabel(),
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel()}
};
private final JButton todayButton = new JButton();
private final JButton cancelButton = new JButton();
public DatePicker() {
super();
selectedDate = getToday();
init();
}
public DatePicker(final Date initialDate) {
super();
if (null == initialDate)
selectedDate = getToday();
else
(selectedDate = new GregorianCalendar()).setTime(initialDate);
originalDate = new GregorianCalendar(
selectedDate.get(Calendar.YEAR),
selectedDate.get(Calendar.MONTH),
selectedDate.get(Calendar.DATE));
init();
}
public boolean isHideOnSelect() {
return hideOnSelect;
}
public void setHideOnSelect(final boolean hideOnSelect) {
if (this.hideOnSelect != hideOnSelect) {
this.hideOnSelect = hideOnSelect;
initButtons(false);
}
}
public Date getDate() {
if (null != selectedDate)
return selectedDate.getTime();
return null;
}
private void init() {
setLayout(new AbsoluteLayout());
this.setMinimumSize(new Dimension(161, 226));
this.setMaximumSize(getMinimumSize());
this.setPreferredSize(getMinimumSize());
this.setBorder(new BorderUIResource.EtchedBorderUIResource());
backButton.setFont(smallFont);
backButton.setText("《");
backButton.setMargin(insets);
backButton.setDefaultCapable(false);
backButton.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent evt) {
onBackClicked(evt);
}
});
add(backButton, new AbsoluteConstraints(10, 10, 20, 20));
monthAndYear.setFont(largeFont);
monthAndYear.setHorizontalAlignment(JTextField.CENTER);
monthAndYear.setText(formatDateText(selectedDate.getTime()));
add(monthAndYear, new AbsoluteConstraints(30, 10, 100, 20));
forwardButton.setFont(smallFont);
forwardButton.setText("》");
forwardButton.setMargin(insets);
forwardButton.setDefaultCapable(false);
forwardButton.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent evt) {
onForwardClicked(evt);
}
});
add(forwardButton, new AbsoluteConstraints(130, 10, 20, 20));
int x = startX;
for (int ii = 0; ii 《 dayHeadings.length; ii++) {
dayHeadings.setOpaque(true);
dayHeadings.setBackground(Color.LIGHT_GRAY);
dayHeadings.setForeground(Color.WHITE);
dayHeadings.setHorizontalAlignment(JLabel.CENTER);
add(dayHeadings, new AbsoluteConstraints(x, 40, 21, 21));
x += 20;
}
x = startX;
int y = startY;
for (int ii = 0; ii 《 daysInMonth.length; ii++) {
for (int jj = 0; jj 《 daysInMonth.length; jj++) {
daysInMonth.setOpaque(true);
daysInMonth.setBackground(white);
daysInMonth.setFont(smallFont);
daysInMonth.setHorizontalAlignment(JLabel.CENTER);
daysInMonth.setText("");
daysInMonth.addMouseListener(new MouseAdapter() {
public void mouseClicked(final MouseEvent evt) {
onDayClicked(evt);
}
});
add(daysInMonth, new AbsoluteConstraints(x, y, 21, 21));
x += 20;
}
x = startX;
y += 20;
}
initButtons(true);
calculateCalendar();
}
private void initButtons(final boolean firstTime) {
if (firstTime) {
final Dimension buttonSize = new Dimension(68, 24);
todayButton.setText("今天");
todayButton.setMargin(insets);
todayButton.setMaximumSize(buttonSize);
todayButton.setMinimumSize(buttonSize);
todayButton.setPreferredSize(buttonSize);
todayButton.setDefaultCapable(true);
todayButton.setSelected(true);
todayButton.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent evt) {
onToday(evt);
}
});
cancelButton.setText("取消");
cancelButton.setMargin(insets);
cancelButton.setMaximumSize(buttonSize);
cancelButton.setMinimumSize(buttonSize);
cancelButton.setPreferredSize(buttonSize);
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent evt) {
onCancel(evt);
}
});
} else {
this.remove(todayButton);
this.remove(cancelButton);
}
求java源代码使用swing或AWT实现功能:点击按钮,选择一个txt文本文件,并将txt中
搞定了
package com.monubia;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.io.BufferedReader;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;import javax.swing.JButton;import javax.swing.JDialog;import javax.swing.JFileChooser;import javax.swing.JTextArea;import javax.swing.WindowConstants;import javax.swing.SwingUtilities;import javax.swing.filechooser.FileNameExtensionFilter;/*** This code was edited or generated using CloudGarden’s Jigloo* SWT/Swing GUI Builder, which is free for non-commercial* use. If Jigloo is being used commercially (ie, by a corporation,* company or business for any purpose whatever) then you* should purchase a license for each developer using Jigloo.* Please visit www.cloudgarden.com for details.* Use of Jigloo implies acceptance of these licensing terms.* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.*/public class Txt extends javax.swing.JFrame {private JButton jButton_open;private JTextArea jTextArea1;/*** Auto-generated main method to display this JFrame*/public static void main(String args) {SwingUtilities.invokeLater(new Runnable() {public void run() {Txt inst = new Txt();inst.setLocationRelativeTo(null);inst.setVisible(true);}});}public Txt() {super();initGUI();}private void initGUI() {try {setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);getContentPane().setLayout(null);{jButton_open = new JButton();getContentPane().add(jButton_open);jButton_open.setText("Open");jButton_open.setBounds(155, 114, 92, 49);jButton_open.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent evt) {jButton_openMouseClicked(evt);}});}{jTextArea1 = new JTextArea();getContentPane().add(jTextArea1);jTextArea1.setBounds(0, 0, 384, 262);}pack();setSize(400, 300);} catch (Exception e) { //add your error handling code heree.printStackTrace();}}private void jButton_openMouseClicked(MouseEvent evt) {//点击了打开JFileChooser open=new JFileChooser();FileNameExtensionFilter txt= new FileNameExtensionFilter("Txt File", "txt");open.setFileFilter(txt);int ret=open.showOpenDialog(this);if(ret==JFileChooser.APPROVE_OPTION){jButton_open.setOpaque(false);jButton_open.setVisible(false);System.out.println(open.getSelectedFile().getAbsolutePath());try {BufferedReader br=new BufferedReader(new FileReader(open.getSelectedFile().getAbsolutePath()));String line=null;while((line=br.readLine())!=null){jTextArea1.append(line+"\n");}} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}