flowoutjava的简单介绍
今天给各位分享flowoutjava的知识,其中也会对进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
- 1、请教大家一个java的编程问题。
- 2、Java,如何添加一组按钮?并给每个按钮添加监听器?急求
- 3、java编写程序题
- 4、Java JPanel的布局设为FlowLayout,往JPanel添加一连串的JButton,但是JButton不会自动换行显示
- 5、求Java题答案
请教大家一个java的编程问题。
//LMS.java
//所有类写在一个文件内,请将以下内容保存成 LMS.java,编译运行即可,GUI版
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.border.Border;
public class LMS {
public static void main(String[] args){
try{UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());}catch(Exception e){}
new LibraryGUI(new Library()).setVisible(true);
}
}
//书籍类
class Book implements ComparableBook{
private String name;//书名
private String publisher;//出版社
private Author au;//作者
private String sn;//书号
public Book(String name,String sn,String publisher,Author au){
this.name=name;this.sn=sn;this.publisher=publisher;this.au=au;
}
public String getName(){ return name; }
public String getPublisher(){ return publisher; }
public String getSerialNumber(){ return sn; }
public Author getAuthor(){ return au; }
public boolean equals(Object o){ return o instanceof Book ((Book)o).sn.equals(sn); }
public String toString(){ return "书名:"+name+" 书号:"+sn+" 作者:"+au+" 出版社:"+publisher; }
//排序优先顺序:书名,作者,出版社,书号是唯一的,所以不作排序项目
public int compareTo(Book b) {
if(b.name.equals(name))
if(b.au.equals(au))
return b.publisher.compareTo(this.publisher);
else return b.au.getName().compareTo(au.getName());
else return b.name.compareTo(name);
}
}
//作者类
class Author{
private String name;
private String phone;
private String addr;
public Author(String name,String phone,String addr){
this.name=name;this.phone=phone;this.addr=addr;
}
public String getName(){ return name; }
public String getPhone(){ return phone; }
public String getAddr(){ return addr; }
public String getFullInfo(){ return "Name:"+name+" Phone:"+phone+" Addr:"+addr; }
public String toString(){ return name; }
}
//图书库类
class Library{
//存放书籍的容器
private ListBook lib = new ArrayListBook();
//加入图书
public void addBook(Book b){ lib.add(b); }
//删除图书
public void removeBook(Book b){ lib.remove(b); }
//取得全部图书
public ListBook getAllBooks(){ return lib; }
//根据条件查询图书,书名模糊查找,书号和作者精确查找
public ListBook query(String bookName, String bookSN, String bookAu) {
ListBook tmp = new ArrayListBook();
if(bookName.isEmpty())
for(int i=0; ilib.size(); i++)
tmp.add(lib.get(i));
else
for(int i=0; ilib.size(); i++)
if(lib.get(i).getName().indexOf(bookName)-1)
tmp.add(lib.get(i));
if(!bookSN.isEmpty())
for(int i=0; itmp.size(); i++)
if(! (tmp.get(i).getSerialNumber().equals(bookSN))){
tmp.remove(i);
--i;
}
if(!bookAu.isEmpty())
for(int i=0; itmp.size(); i++)
if(!(tmp.get(i).getAuthor().getName().equals(bookAu))){
tmp.remove(i);
--i;
}
return tmp;
}
}
//GUI部分
class LibraryGUI extends JFrame implements ActionListener{
private JTextField bname,bpub,bsn,bauname,bauaddr,bauphone;
private JTextField qbname,qbsn,qauname;
private JPanel north,addp,add1,add2,quep,disp,mainp;
private JButton addb,queb,del;
private Library lib;
private JScrollPane jsp;
private Box box;
LibraryGUI(Library lib){
super("图书XX系统");
this.lib=lib;
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().setLayout(new BorderLayout());
this.setSize(740,600);
this.setLocationRelativeTo(null);
//新增面板
addp=new JPanel(new GridLayout(2,1));
add1=new JPanel(new FlowLayout(FlowLayout.LEFT));
add2=new JPanel(new FlowLayout(FlowLayout.LEFT));
addp.add(add1);addp.add(add2);
addp.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(10,10,10,10),BorderFactory.createLineBorder(Color.LIGHT_GRAY)));
JLabel l;
l=new JLabel("书名:");
add1.add(l);
bname=new JTextField(16);
add1.add(bname);
l=new JLabel(" 书号:");
add1.add(l);
bsn=new JTextField(20);
add1.add(bsn);
l=new JLabel(" 出版社:");
add1.add(l);
bpub=new JTextField(30);
add1.add(bpub);
l=new JLabel("作者:");
add2.add(l);
bauname=new JTextField(16);
add2.add(bauname);
l=new JLabel(" 电话:");
add2.add(l);
bauphone=new JTextField(20);
add2.add(bauphone);
l=new JLabel(" 住址:");
add2.add(l);
bauaddr=new JTextField(46);
add2.add(bauaddr);
add1.add(new JLabel(" "));
addb=new JButton(" 新 增 ");
add1.add(addb);
addb.addActionListener(this);
//查询面板
quep=new JPanel(new FlowLayout(FlowLayout.LEFT));
quep.setBorder(addp.getBorder());
l=new JLabel("书名:");
quep.add(l);
qbname=new JTextField(16);
quep.add(qbname);
l=new JLabel(" 书号:");
quep.add(l);
qbsn=new JTextField(20);
quep.add(qbsn);
l=new JLabel(" 作者:");
quep.add(l);
qauname=new JTextField(16);
quep.add(qauname);
quep.add(new JLabel(" "));
queb=new JButton(" 查 询 ");
quep.add(queb);
queb.addActionListener(this);
del=new JButton(" 删 除 ");
quep.add(del);
del.addActionListener(this);
//
north=new JPanel(new BorderLayout());
north.add(addp,BorderLayout.NORTH);
north.add(quep,BorderLayout.SOUTH);
this.getContentPane().add(north,BorderLayout.NORTH);
//显示面板
disp=new JPanel(new BorderLayout());
disp.setBorder(addp.getBorder());
this.getContentPane().add(disp);
mainp=new JPanel(new BorderLayout());
mainp.setBackground(Color.white);
box=Box.createVerticalBox();
mainp.add(box,BorderLayout.NORTH);
jsp=new JScrollPane(mainp,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
disp.add(jsp);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==addb)
createNewBook();
else if(e.getSource()==queb)
queryBook();
else if(e.getSource()==del)
removeBook();
}
private void removeBook() {
String bn=qbname.getText().trim();
String bsn=qbsn.getText().trim();
String baun=qauname.getText().trim();
if(bn.isEmpty()bsn.isEmpty()baun.isEmpty())
return;
ListBook bks = lib.query(bn,bsn,baun);
for(int i=0; ibks.size(); i++)
lib.removeBook(bks.get(i));
display(lib.getAllBooks());
}
private void queryBook() {
String bn=qbname.getText().trim();
String bsn=qbsn.getText().trim();
String baun=qauname.getText().trim();
display(lib.query(bn,bsn,baun));
}
private void createNewBook() {
Author au = new Author(bauname.getText().trim(),bauphone.getText().trim(),bauaddr.getText().trim());
Book bk = new Book(bname.getText().trim(),bsn.getText().trim(),bpub.getText().trim(),au);
lib.addBook(bk);
display(lib.getAllBooks());
}
private void display(ListBook bks){
box.removeAll();
for(int i=0; ibks.size(); i++){
Book b = bks.get(i);
System.out.println(b);
BookItem bi = new BookItem(b);
bi.setBackground(i%2==0?Color.white:new Color(0xefefef));
box.add(bi);
}
jsp.validate();
}
}
class BookItem extends JPanel{
private static Border border=BorderFactory.createCompoundBorder(
BorderFactory.createMatteBorder(0,0,1,0,Color.LIGHT_GRAY),
BorderFactory.createEmptyBorder(0,5,5,5));
BookItem(Book b){
this.setBorder(border);
this.setLayout(new FlowLayout(FlowLayout.LEFT));
String str="htmltabletr style='color:gray;'" +
"td width=168书名: font color=#000"+b.getName()+"/font/td" +
"td width=188书号:font color=#000"+b.getSerialNumber()+"/font/td" +
"td width=138作者:font color=#000"+b.getAuthor()+"/font/td" +
"td出版社:font color=#000"+b.getPublisher()+"/font/td" +
"/tr/table";
this.add(new JLabel(str));
}
}
Java,如何添加一组按钮?并给每个按钮添加监听器?急求
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class AddButtons extends JFrame implements ActionListener {
private static final long serialVersionUID = 5322709709362727424L;
private JButton[] jb = new JButton[20];
public AddButtons() {
super("添加多个按钮!");
this.setLayout(new FlowLayout());
this.setSize(800, 600);
this.setBounds(10, 10, 800, 600);
for (int i = 0; i jb.length; i++) {
jb[i] = new JButton("按钮" + i);
jb[i].addActionListener(this);
this.add(jb[i]);
}
this.setVisible(true);
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
}
public static void main(String[] args) {
new AddButtons();
}
@Override
public void actionPerformed(ActionEvent e) {
for (int i = 0; i jb.length; i++) {
if (e.getSource().equals(jb[i])) {
System.out.println(i);
}
}
}
}
java编写程序题
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class LoginFrame extends JFrame implements ActionListener {
JTextField jtf;//文本框
JPasswordField jpf;//密码框
public LoginFrame() {
JPanel jpCenter = new JPanel(new GridLayout(2, 2));//2行2列,存放标签和输入框密码框
JLabel jl1 = new JLabel("用户名:");
jtf = new JTextField(8);
JLabel jl2 = new JLabel("密码:");
jpf = new JPasswordField(8);
jpf.setEchoChar('*');
jpCenter.add(jl1);
jpCenter.add(jtf);
jpCenter.add(jl2);
jpCenter.add(jpf);
JPanel jp3 = new JPanel();//存放按钮
JButton jbLogin = new JButton("登录");
jbLogin.addActionListener(this);
JButton jbCancel = new JButton("取消");
jbCancel.addActionListener(this);
jp3.add(jbLogin);
jp3.add(jbCancel);
add(jpCenter);
add(jp3);
setLayout(new FlowLayout());//窗体布局
setTitle("登录");//窗体标题
setSize(260, 160);//大小
setLocationRelativeTo(null);//居中
setResizable(false);//不能缩放
setDefaultCloseOperation(EXIT_ON_CLOSE);//点击关闭时默认退出程序
setVisible(true);//可见
}
public static void main(String[] args) {
new LoginFrame(); //创建窗体
}
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();//得到点击按钮的文字
if (cmd.equals("取消")) {//如果是取消按钮,那么做下面的事情(题目没有要求,所以下面的代码可以删除)
jtf.setText("");
jpf.setText("");
JOptionPane.showMessageDialog(this, "提示下都是123哟~");
jtf.requestFocus();
}
//如果是登录按钮,
if (cmd.equals("登录")) {
String id = jtf.getText().trim();//取得文本框文字
String key = new String(jpf.getPassword()).trim();//取得密码框的信息
//如果密码和用户名都不是123
if (!id.equals("123") !key.equals("123")) {
//提示用户
JOptionPane.showMessageDialog(this, "输入错误,请重新输入", "错误", JOptionPane.ERROR_MESSAGE);
jtf.setText("");//清空文本框
jpf.setText("");//清空密码框
jtf.requestFocus();//文本框获得焦点
} else if (id.equals("123") !key.equals("123")) {//用户名123,密码不对的 情况
JOptionPane.showMessageDialog(this, "输入错误,请重新输入", "错误", JOptionPane.ERROR_MESSAGE);
jpf.setText("");
jpf.requestFocus();
//题目没有说明当密码正确,用户名不正确的时候,该怎么处理,所以,下面的else if代码也可以删除
}else if(key.equals("123")!id.equals("123")){
JOptionPane.showMessageDialog(this, "输入错误,请重新输入", "错误", JOptionPane.ERROR_MESSAGE);
jtf.setText("");
jtf.requestFocus();
}
}
}
}
Java JPanel的布局设为FlowLayout,往JPanel添加一连串的JButton,但是JButton不会自动换行显示
FlowLayout本身就不会自动换行啊。要换行的,可以用GridLayout或GridBagLayout
求Java题答案
1.C 解:java中创建对象时要分两步一要声明,二要用NEW分配内存空间。
2.C 解:在用k=k+1时k没有初始话。
3.D 解:friendly修饰的类变量能被本类和同一个包中类访问。
4.A 解:静态方法不能引用非静态方法。
5.A 解:引用该方法时要有参数,并是要有返回类型的。
6.A 解:我把你查书的,浏览器首先调用INIT()方法进行一些必要的初始话工作。
7.A 解:先应引用包再是把下面的程序放到Mypackage包中。
8.B 解:java的默认布局是FlowLayout 这么什么解释的是书上的。
9.C 解:类MineBase是抽象类要继承它必须要重写它的抽象方法,或把Mine 改成抽象类。
10.C 解:这是select语法看看书就知道了。
附:如果有什么错误请多原谅,祝你学好java 回答完毕 拜拜 谢谢
关于flowoutjava和的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。