更新的話,可以使用自身的validate方法;
成都創(chuàng)新互聯(lián)公司主要從事網(wǎng)站設(shè)計(jì)制作、做網(wǎng)站、網(wǎng)頁設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)涇源,10余年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):028-86922220
也可以使用SwingUtiltis.updateComponentUI等方法解決。
示例代碼如下:
import?java.awt.*;
import?java.awt.event.*;
import?javax.swing.*;
public?class?LookAndFeelDemo2?extends?JFrame?{
private?JRadioButton?radio1?=?new?JRadioButton("Metal");
private?JRadioButton?radio2?=?new?JRadioButton("Windows");
private?JRadioButton?radio3?=?new?JRadioButton("Motif");
private?JPanel?panelNorth?=?new?JPanel();
private?JPanel?panelSouth?=?new?JPanel();
private?JTextArea?area?=?new?JTextArea(6,?20);
private?JButton?btn?=?new?JButton("button");
private?JCheckBox?chk?=?new?JCheckBox("checkBox");
private?JComboBox?cmbLookAndFeel?=?new?JComboBox(new?String[]?{?"Metal",
"Widows",?"Motif",?"GTK"?});
public?LookAndFeelDemo2(String?title)?{
super(title);
Container?contentPane?=?this.getContentPane();
ButtonGroup?group?=?new?ButtonGroup();
group.add(radio1);
group.add(radio2);
group.add(radio3);
panelNorth.add(radio1);
panelNorth.add(radio2);
panelNorth.add(radio3);
panelSouth.add(btn);
panelSouth.add(chk);
panelSouth.add(cmbLookAndFeel);
contentPane.add(panelNorth,?BorderLayout.NORTH);
contentPane.add(area,?BorderLayout.CENTER);
contentPane.add(panelSouth,?BorderLayout.SOUTH);
pack();
setVisible(true);
setSize(300,?400);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//?監(jiān)聽Item事件
radio1.addItemListener(new?MyItemListener());
radio2.addItemListener(new?MyItemListener());
radio3.addItemListener(new?MyItemListener());
}
private?class?MyItemListener?implements?ItemListener?{
public?void?itemStateChanged(ItemEvent?e)?{
//?取得點(diǎn)擊按鈕的名字
String?itemName?=?((JRadioButton)?e.getSource()).getText();
changeLookAndFeel(itemName);
}
}
//?設(shè)置外觀的private類型方法
private?void?changeLookAndFeel(String?name)?{
String?lookAndFeel?=?"";
if?(name.equals("Metal"))?{
lookAndFeel?=?"javax.swing.plaf.metal.MetalLookAndFeel";
}?else?if?(name.equals("Windows"))?{
lookAndFeel?=?"com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
}?else?if?(name.equals("Motif"))?{
lookAndFeel?=?"com.sun.java.swing.plaf.motif.MotifLookAndFeel";
}else{
//取得默認(rèn)的metal外觀
lookAndFeel?=?"javax.swing.plaf.metal.MetalLookAndFeel";
}
try?{
UIManager.setLookAndFeel(lookAndFeel);
//?運(yùn)行時(shí)指定LookAndFeel,需要SwingUtilities.updateComponentTreeUI(Component
//?c)實(shí)現(xiàn)動(dòng)態(tài)的更新
SwingUtilities.updateComponentTreeUI(this);
area.setText("當(dāng)前外觀類名:\n"?+?lookAndFeel);
}?catch?(Exception?ex)?{
ex.printStackTrace();
}
}
public?static?void?main(String[]?args)?{
new?LookAndFeelDemo2("LookAndFeel");
}
}
public void update(String sql,String[] args)
{
try
{
con=getCon();//這里應(yīng)該在本類有一個(gè)getCon()的方法吧,用來拿到數(shù)據(jù)庫鏈接的
pstat=con.prepareStatement(sql);//創(chuàng)建一個(gè)prepareStatement,用來執(zhí)行sql語句的,pstat在成員變量里有定義吧
for(int i=0;iargs.length;i++)//這里遍歷傳進(jìn)來的args數(shù)組
{
pstat.setString(i+1, args[i]);//這里是執(zhí)行了sql語句的,把sql語句里的"?" 填充,比如那條sql的第一個(gè)問號(hào)用args[0]代替;
}
pstat.executeUpdate();//執(zhí)行更新
}catch(Exception e){e.printStackTrace();}
}
這段代碼的總體意思是傳入一條sql語句,和需要更新的內(nèi)容(在args數(shù)組里面)..
public static void Update(Connection con, String sql, Object... objects) {
PreparedStatement pre = null;
try {
pre = con.prepareStatement(sql);
for (int i = 0; i objects.length; i++) {
pre.setObject(i + 1, objects[i]);
}
pre.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}