import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;public class MainFrame extends Frame {
private String dbURL = "jdbc:mysql://localhost/book?useUnicode=true&characterEncoding=GBK"; // 数据库标识名
private String user = "root"; // 数据库用户
private String password = ""; // 数据库用户密码
// JButton jbt = new JButton();
// JButton jbtSmall = new JButton();
private Panel borderpanel;
private Panel flowpanel;
private Panel gridpanel;
//private Panel cardpanel;
private JLabel jl1 = new JLabel("序号:"+111+"\t书名:"+11+"\t副标题:"+11+"\t作者:"+11+ "\t国籍:"+11+"\t出版社:"+11+"\t价格:"+11+"\tISBN:"+11);
public MainFrame(String title) throws SQLException, ClassNotFoundException
{
super(title);
//窗口大小600*400
setSize(1024,768);
//距离左上角图标距离100,100
setLocation(0,0);
//setBorderLayoutPanel();
//setGridLayoutPanel();
setFlowLayoutPanel();
//setCardLayoutPanel();
setLayout(new GridLayout(2,2));
//add(borderpanel);
add(flowpanel);
//add(cardpanel);
//add(gridpanel);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
public void setFlowLayoutPanel() throws SQLException, ClassNotFoundException
{
flowpanel=new Panel();
Class.forName("com.mysql.jdbc.Driver"); // 加载驱动器
Connection con = DriverManager.getConnection(dbURL, user, password); // 获取连接
String sqlStr = "select * from user"; // SQL查询语句
//System.out.print(sqlStr);
Statement st = con.createStatement(); // 获取PreparedStatement对象
ResultSet rs = st.executeQuery(sqlStr); // 执行查询
while (rs.next()) { // 遍历ResultSet
String str=rs.getString("url");
final String strid=rs.getString("id"); // 获取数据
final String strtitle=rs.getString("title");
final String strsubhead=rs.getString("subhead");
final String strauthors=rs.getString("authors");
final String strnationality=rs.getString("nationality");
final String strpublisher=rs.getString("publisher");
final String strprice=rs.getString("price");
final String strisbn=rs.getString("isbn");
//final String strid=s.getString("url");
//JButton btn=new JButton("");
JButton btnSmall=new JButton("");
ImageIcon icon = new ImageIcon(str);
//btn.setIcon(icon); // 将图片加入
//btn.setVisible(false);//替代jlb.show();
//btn.invalidate();
//btn.setSize=(80,80);
btnSmall.setIcon(scaleImage(icon));
btnSmall.setLocation(0, 0);
btnSmall.setSize(100, 150);
btnSmall.setVisible(true);
btnSmall.invalidate();
btnSmall.setLocation(0,0);
//flowpanel.add(btn);
flowpanel.add(btnSmall);
btnSmall.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
//JLabel jl = new JLabel("序号:"+strid+"\t书名:"+strtitle+"\t副标题:"+strsubhead+"\t作者:"+strauthors+ "\t国籍:"+strnationality+"\t出版社:"+strpublisher+"\t价格:"+strprice+"\tISBN:"+strisbn);
//add(jl);
//setGridLayoutPanel(strid,strtitle,strsubhead,strauthors,strnationality,strpublisher,strprice,strisbn);
setBorderLayoutPanel(strid,strtitle,strsubhead,strauthors,strnationality,strpublisher,strprice,strisbn);
add(borderpanel);
//setCardLayoutPanel(strid,strtitle,strsubhead,strauthors,strnationality,strpublisher,strprice,strisbn);
}
}
);
}
}
private ImageIcon scaleImage(ImageIcon icon) {
int width = icon.getIconWidth();
//System.out.print(width);
int height = icon.getIconHeight();
//if (width <= 100 && height <= 150) {
// return icon;
// }
Image image = icon.getImage();
image = image.getScaledInstance(100, 150, Image.SCALE_DEFAULT);
return new ImageIcon(image);
}
public void setGridLayoutPanel(String strid,String strtitle,String strsubhead,String strauthors,String strnationality,String strpublisher,String strprice,String strisbn)
{
gridpanel=new Panel();
gridpanel.setLayout(new GridLayout(2,2));
//Button btn1=new Button("Button1");
//gridpanel.add(btn1);
JLabel jl = new JLabel("序号:"+strid+"\t书名:"+strtitle+"\t副标题:"+strsubhead+"\t作者:"+strauthors+ "\t国籍:"+strnationality+"\t出版社:"+strpublisher+"\t价格:"+strprice+"\tISBN:"+strisbn);
//add(jl);
//JLabel jl1 = new JLabel("序号:"+111+"\t书名:"+11+"\t副标题:"+11+"\t作者:"+11+ "\t国籍:"+11+"\t出版社:"+11+"\t价格:"+11+"\tISBN:"+11);
gridpanel.add(jl);
gridpanel.setVisible(false);
}
public void setBorderLayoutPanel(String strid,String strtitle,String strsubhead,String strauthors,String strnationality,String strpublisher,String strprice,String strisbn)
{
borderpanel=new Panel();
borderpanel.setLayout(new BorderLayout());
//Button btn1=new Button("Button1");
//gridpanel.add(btn1);
//JLabel jl = new JLabel("序号:"+strid+"\t书名:"+strtitle+"\t副标题:"+strsubhead+"\t作者:"+strauthors+ "\t国籍:"+strnationality+"\t出版社:"+strpublisher+"\t价格:"+strprice+"\tISBN:"+strisbn);
//add(jl);
//JLabel jl1 = new JLabel("序号:"+111+"\t书名:"+11+"\t副标题:"+11+"\t作者:"+11+ "\t国籍:"+11+"\t出版社:"+11+"\t价格:"+11+"\tISBN:"+11);
//borderpanel.add(jl);
//gridpanel.setVisible(true);
//borderpanel.add(jl,BorderLayout.NORTH);
//borderpanel.add(jl,BorderLayout.SOUTH);
//borderpanel.add(jl,BorderLayout.WEST);
//borderpanel.add(jl,BorderLayout.EAST);
//borderpanel.add(jl,BorderLayout.CENTER);
borderpanel.add(jl1,BorderLayout.SOUTH);
}
/*public void setCardLayoutPanel(String strid,String strtitle,String strsubhead,String strauthors,String strnationality,String strpublisher,String strprice,String strisbn)
{
final CardLayout cl=new CardLayout();
cardpanel=new Panel();
cardpanel.setLayout(cl);
Button btn1=new Button("黑桃A");
JButton jbt=new JButton("1616");
JLabel jl = new JLabel("序号:"+strid+"\t书名:"+strtitle+"\t副标题:"+strsubhead+"\t作者:"+strauthors+ "\t国籍:"+strnationality+"\t出版社:"+strpublisher+"\t价格:"+strprice+"\tISBN:"+strisbn);
//Button btn2=new Button("红桃K");
ActionListener al=new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
cl.next(cardpanel);
}
};
btn1.addActionListener(al);
//btn2.addActionListener(al);
cardpanel.add(jl,"1");
cardpanel.add(jbt,"1");
}*/
/* public void method(String strid,String strtitle,String strsubhead,String strauthors,String strnationality,String strpublisher,String strprice,String strisbn){
try {
JLabel jl = new JLabel("序号:"+strid+"\t书名:"+strtitle+"\t副标题:"+strsubhead+"\t作者:"+strauthors+ "\t国籍:"+strnationality+"\t出版社:"+strpublisher+"\t价格:"+strprice+"\tISBN:"+strisbn);
add(jl);
} catch (Exception ex) {
ex.printStackTrace(); // 输出出错信息
}
}*/
public static void main(String[] args) throws SQLException, ClassNotFoundException {
MainFrame yf=new MainFrame("我的书架");
yf.setVisible(true);
//JLabel jl = new JLabel("书 名:"+111+"\t书 名:"+11+"\t 书 名:"+11+"\t书 名:"+11+ "\t书 名:"+11+"\t书 名:"+11+"\t书 名:"+11+"\t书 名:"+11);
//yf.add(jl);
}}以上是我的代码,我想实现点击一个图标,就显示相关的信息,但是无法立即显示,只能改变窗口大小后才能显示?
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;public class MainFrame extends Frame {
private String dbURL = "jdbc:mysql://localhost/book?useUnicode=true&characterEncoding=GBK"; // 数据库标识名
private String user = "root"; // 数据库用户
private String password = ""; // 数据库用户密码
// JButton jbt = new JButton();
// JButton jbtSmall = new JButton();
private Panel borderpanel;
private Panel flowpanel;
private Panel gridpanel;
//private Panel cardpanel;
private JLabel jl1 = new JLabel("序号:"+111+"\t书名:"+11+"\t副标题:"+11+"\t作者:"+11+ "\t国籍:"+11+"\t出版社:"+11+"\t价格:"+11+"\tISBN:"+11);
public MainFrame(String title) throws SQLException, ClassNotFoundException
{
super(title);
//窗口大小600*400
setSize(1024,768);
//距离左上角图标距离100,100
setLocation(0,0);
//setBorderLayoutPanel();
//setGridLayoutPanel();
setFlowLayoutPanel();
//setCardLayoutPanel();
setLayout(new GridLayout(2,2));
//add(borderpanel);
add(flowpanel);
//add(cardpanel);
//add(gridpanel);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
public void setFlowLayoutPanel() throws SQLException, ClassNotFoundException
{
flowpanel=new Panel();
Class.forName("com.mysql.jdbc.Driver"); // 加载驱动器
Connection con = DriverManager.getConnection(dbURL, user, password); // 获取连接
String sqlStr = "select * from user"; // SQL查询语句
//System.out.print(sqlStr);
Statement st = con.createStatement(); // 获取PreparedStatement对象
ResultSet rs = st.executeQuery(sqlStr); // 执行查询
while (rs.next()) { // 遍历ResultSet
String str=rs.getString("url");
final String strid=rs.getString("id"); // 获取数据
final String strtitle=rs.getString("title");
final String strsubhead=rs.getString("subhead");
final String strauthors=rs.getString("authors");
final String strnationality=rs.getString("nationality");
final String strpublisher=rs.getString("publisher");
final String strprice=rs.getString("price");
final String strisbn=rs.getString("isbn");
//final String strid=s.getString("url");
//JButton btn=new JButton("");
JButton btnSmall=new JButton("");
ImageIcon icon = new ImageIcon(str);
//btn.setIcon(icon); // 将图片加入
//btn.setVisible(false);//替代jlb.show();
//btn.invalidate();
//btn.setSize=(80,80);
btnSmall.setIcon(scaleImage(icon));
btnSmall.setLocation(0, 0);
btnSmall.setSize(100, 150);
btnSmall.setVisible(true);
btnSmall.invalidate();
btnSmall.setLocation(0,0);
//flowpanel.add(btn);
flowpanel.add(btnSmall);
btnSmall.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
//JLabel jl = new JLabel("序号:"+strid+"\t书名:"+strtitle+"\t副标题:"+strsubhead+"\t作者:"+strauthors+ "\t国籍:"+strnationality+"\t出版社:"+strpublisher+"\t价格:"+strprice+"\tISBN:"+strisbn);
//add(jl);
//setGridLayoutPanel(strid,strtitle,strsubhead,strauthors,strnationality,strpublisher,strprice,strisbn);
setBorderLayoutPanel(strid,strtitle,strsubhead,strauthors,strnationality,strpublisher,strprice,strisbn);
add(borderpanel);
//setCardLayoutPanel(strid,strtitle,strsubhead,strauthors,strnationality,strpublisher,strprice,strisbn);
}
}
);
}
}
private ImageIcon scaleImage(ImageIcon icon) {
int width = icon.getIconWidth();
//System.out.print(width);
int height = icon.getIconHeight();
//if (width <= 100 && height <= 150) {
// return icon;
// }
Image image = icon.getImage();
image = image.getScaledInstance(100, 150, Image.SCALE_DEFAULT);
return new ImageIcon(image);
}
public void setGridLayoutPanel(String strid,String strtitle,String strsubhead,String strauthors,String strnationality,String strpublisher,String strprice,String strisbn)
{
gridpanel=new Panel();
gridpanel.setLayout(new GridLayout(2,2));
//Button btn1=new Button("Button1");
//gridpanel.add(btn1);
JLabel jl = new JLabel("序号:"+strid+"\t书名:"+strtitle+"\t副标题:"+strsubhead+"\t作者:"+strauthors+ "\t国籍:"+strnationality+"\t出版社:"+strpublisher+"\t价格:"+strprice+"\tISBN:"+strisbn);
//add(jl);
//JLabel jl1 = new JLabel("序号:"+111+"\t书名:"+11+"\t副标题:"+11+"\t作者:"+11+ "\t国籍:"+11+"\t出版社:"+11+"\t价格:"+11+"\tISBN:"+11);
gridpanel.add(jl);
gridpanel.setVisible(false);
}
public void setBorderLayoutPanel(String strid,String strtitle,String strsubhead,String strauthors,String strnationality,String strpublisher,String strprice,String strisbn)
{
borderpanel=new Panel();
borderpanel.setLayout(new BorderLayout());
//Button btn1=new Button("Button1");
//gridpanel.add(btn1);
//JLabel jl = new JLabel("序号:"+strid+"\t书名:"+strtitle+"\t副标题:"+strsubhead+"\t作者:"+strauthors+ "\t国籍:"+strnationality+"\t出版社:"+strpublisher+"\t价格:"+strprice+"\tISBN:"+strisbn);
//add(jl);
//JLabel jl1 = new JLabel("序号:"+111+"\t书名:"+11+"\t副标题:"+11+"\t作者:"+11+ "\t国籍:"+11+"\t出版社:"+11+"\t价格:"+11+"\tISBN:"+11);
//borderpanel.add(jl);
//gridpanel.setVisible(true);
//borderpanel.add(jl,BorderLayout.NORTH);
//borderpanel.add(jl,BorderLayout.SOUTH);
//borderpanel.add(jl,BorderLayout.WEST);
//borderpanel.add(jl,BorderLayout.EAST);
//borderpanel.add(jl,BorderLayout.CENTER);
borderpanel.add(jl1,BorderLayout.SOUTH);
}
/*public void setCardLayoutPanel(String strid,String strtitle,String strsubhead,String strauthors,String strnationality,String strpublisher,String strprice,String strisbn)
{
final CardLayout cl=new CardLayout();
cardpanel=new Panel();
cardpanel.setLayout(cl);
Button btn1=new Button("黑桃A");
JButton jbt=new JButton("1616");
JLabel jl = new JLabel("序号:"+strid+"\t书名:"+strtitle+"\t副标题:"+strsubhead+"\t作者:"+strauthors+ "\t国籍:"+strnationality+"\t出版社:"+strpublisher+"\t价格:"+strprice+"\tISBN:"+strisbn);
//Button btn2=new Button("红桃K");
ActionListener al=new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
cl.next(cardpanel);
}
};
btn1.addActionListener(al);
//btn2.addActionListener(al);
cardpanel.add(jl,"1");
cardpanel.add(jbt,"1");
}*/
/* public void method(String strid,String strtitle,String strsubhead,String strauthors,String strnationality,String strpublisher,String strprice,String strisbn){
try {
JLabel jl = new JLabel("序号:"+strid+"\t书名:"+strtitle+"\t副标题:"+strsubhead+"\t作者:"+strauthors+ "\t国籍:"+strnationality+"\t出版社:"+strpublisher+"\t价格:"+strprice+"\tISBN:"+strisbn);
add(jl);
} catch (Exception ex) {
ex.printStackTrace(); // 输出出错信息
}
}*/
public static void main(String[] args) throws SQLException, ClassNotFoundException {
MainFrame yf=new MainFrame("我的书架");
yf.setVisible(true);
//JLabel jl = new JLabel("书 名:"+111+"\t书 名:"+11+"\t 书 名:"+11+"\t书 名:"+11+ "\t书 名:"+11+"\t书 名:"+11+"\t书 名:"+11+"\t书 名:"+11);
//yf.add(jl);
}}以上是我的代码,我想实现点击一个图标,就显示相关的信息,但是无法立即显示,只能改变窗口大小后才能显示?
setVisible(false);
然后再
setVisble(true);如果说:
只能改变窗口大小后才能显示?
那么你也可以setSize();
在原来大小基础上加或减一个很小的数,这样看不出来
如jlb1.invalidate() 或 borderpanel.invalidate()还有,不要每次点击按钮都new一个panel,add到界面中,这样用下来,内存中会有太多panel在那儿。
public void actionPerformed(ActionEvent e) {
setBorderLayoutPanel(strid,strtitle,strsubhead,strauthors,strnationality,strpublisher,strprice,strisbn);
add(borderpanel);
borderpanel.invalidate();
}
}
);
是不是这样,还是老样子,要调一下窗口才行,是不是只能想二楼说的那样微调下窗口才能实现啊,还有我也不想每次点击按钮都new一个panel,但是目前我只能想到这种方法,不知道还能怎么弄
setVisible(false);
再setVisible(true);
一下就行
界面上看不出来的
而且又不耗上面内存虽然看起来没那么高端
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;public class MainFrame extends Frame {
private String dbURL = "jdbc:mysql://localhost/book?useUnicode=true&characterEncoding=GBK"; // 数据库标识名
private String user = "root"; // 数据库用户
private String password = ""; // 数据库用户密码
private Panel borderpanel = new Panel();
private Panel flowpanel;
private Panel gridpanel;
private JLabel jl1 = new JLabel("序号:" + 111 + "\t书名:" + 11 + "\t副标题:" + 11
+ "\t作者:" + 11 + "\t国籍:" + 11 + "\t出版社:" + 11 + "\t价格:" + 11
+ "\tISBN:" + 11); public MainFrame(String title) throws SQLException, ClassNotFoundException {
super(title);
// 窗口大小600*400
setSize(600, 400);
// 距离左上角图标距离100,100
setLayout(new GridLayout(2, 1));
setFlowLayoutPanel();
add(flowpanel);
borderpanel.setLayout(new BorderLayout());
borderpanel.add(jl1, BorderLayout.SOUTH);
add(borderpanel);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
} public void setFlowLayoutPanel() throws SQLException,
ClassNotFoundException {
flowpanel = new Panel();
Class.forName("com.mysql.jdbc.Driver"); // 加载驱动器
Connection con = DriverManager.getConnection(dbURL, user, password); // 获取连接
String sqlStr = "select * from user"; // SQL查询语句
Statement st = con.createStatement(); // 获取PreparedStatement对象
ResultSet rs = st.executeQuery(sqlStr); // 执行查询
while (rs.next()) { // 遍历ResultSet
String str=rs.getString("url");
final String strid=rs.getString("id"); // 获取数据
final String strtitle=rs.getString("title");
final String strsubhead=rs.getString("subhead");
final String strauthors=rs.getString("authors");
final String strnationality=rs.getString("nationality");
final String strpublisher=rs.getString("publisher");
final String strprice=rs.getString("price");
final String strisbn=rs.getString("isbn");
JButton btnSmall=new JButton("");
ImageIcon icon = new ImageIcon(str);
btnSmall.setIcon(scaleImage(icon));
btnSmall.setSize(100, 150);
btnSmall.setVisible(true);
btnSmall.invalidate(); flowpanel.add(btnSmall); btnSmall.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jl1.setText("序号:" + strid + "\t书名:" + strtitle + "\t副标题:"
+ strsubhead + "\t作者:" + strauthors + "\t国籍:"
+ strnationality + "\t出版社:" + strpublisher
+ "\t价格:" + strprice + "\tISBN:" + strisbn);
}
}); }
} private ImageIcon scaleImage(ImageIcon icon) {
int width = icon.getIconWidth();
// System.out.print(width);
int height = icon.getIconHeight();
// if (width <= 100 && height <= 150) {
// return icon;
// }
Image image = icon.getImage();
image = image.getScaledInstance(100, 150, Image.SCALE_DEFAULT);
return new ImageIcon(image);
} public static void main(String[] args) throws SQLException,
ClassNotFoundException {
MainFrame yf = new MainFrame("我的书架");
yf.setVisible(true);
}}
flowpanel = new Panel();
FlowLayout layout = new FlowLayout();
layout.setAlignment(FlowLayout.LEFT);
flowpanel.setLayout(layout);
Class.forName("com.mysql.jdbc.Driver"); // 加载驱动器