使用JPanel.getComponents()方法,可以JPanel上的組件的數(shù)組,然后再對(duì)組件操作就可以了,運(yùn)行如下程序會(huì)看到效果。
10年積累的網(wǎng)站設(shè)計(jì)制作、網(wǎng)站建設(shè)經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶對(duì)網(wǎng)站的新想法和需求。提供各種問(wèn)題對(duì)應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先網(wǎng)站設(shè)計(jì)后付款的網(wǎng)站建設(shè)流程,更有新邱免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
import java.awt.*;
import javax.swing.*;
public class GetComponentsTest extends JFrame {
private Container buttonpanel;
public GetComponentsTest() {
super("GetcomponetsTest");
String[] button = { "按鈕A", "按鈕B", "按鈕C", "按鈕D", "按鈕E", "按鈕F" };
buttonpanel = getContentPane();
buttonpanel.setLayout(new GridLayout(1, 6));
for (int x = 0; x 6; x++) {
buttonpanel.add(new JButton(button[x]));
}
setSize(640, 60);
setVisible(true);
}
public static void main(String[] args) {
GetComponentsTest test = new GetComponentsTest();
Component[] com = test.getContentPane().getComponents();
for(int i=0;i6;i++){
((JButton)com[i]).setFont(new Font("隸書(shū)",Font.ITALIC,20 ));
((JButton)com[i]).setForeground(Color.RED);
}
}
}
步驟如下:
1、在web工程里面創(chuàng)建一個(gè)Servlet類(lèi),繼承HttpServlet,重寫(xiě)doPost,doGet方法,在doPost方法中調(diào)用doGet方法;
2、在doGet方法中把要設(shè)置到j(luò)sp頁(yè)面的值存到request中;
3、在doGet方法中添加轉(zhuǎn)發(fā)到j(luò)sp頁(yè)面的代碼;
4、在jsp頁(yè)面中使用jstl標(biāo)簽獲取存入的值。
事例代碼如下:
Servlet類(lèi):
public class DemoServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setAttribute("name", "nameValue");
request.getRequestDispatcher("/demo.jsp").forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class Demo10 extends JFrame implements ItemListener,
KeyListener
{
String s[] = { "吉林", "遼寧", "黑龍江" };
JComboBox jb1 = new JComboBox(s);
JLabel jla = new JLabel("吉林");
JPanel panel = new JPanel();
String defaultMessage="請(qǐng)輸入城市名";
Component cmp;
public Demo10() {
jb1.setEditable(true);
panel.add(jb1);
panel.add(jla);
this.getContentPane().add(panel);
this.setBounds(100, 100, 300, 300);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
jb1.addItemListener(this);
cmp = jb1.getEditor().getEditorComponent();
cmp.addKeyListener(this);
}
public static void main(String[] args) {
new Demo10();
}
public void itemStateChanged(ItemEvent e){
jla.setText(e.getItem().toString());
}
public void keyPressed(KeyEvent e)
{
}
public void keyReleased(KeyEvent e)
{
jla.setText(jb1.getEditor().getItem().toString());
repaint();
}
public void keyTyped(KeyEvent e)
{
}
}
已經(jīng)測(cè)試過(guò)了??梢詫?shí)現(xiàn)用戶在編輯item時(shí),獲取編輯后的值
大致是HtmlControl被服務(wù)器發(fā)送到頁(yè)面ID不變,比如div id="a" runat="sever"/div
WebControl 被服務(wù)器發(fā)送到頁(yè)面,如果在其他循環(huán)控件里,或者在用戶控件中,ID會(huì)變,
用document.getElementById("%=Button1.ClientID%")
這個(gè)你多看看頁(yè)面源代碼,就知道什么時(shí)候會(huì)變。什么時(shí)候不會(huì)變
類(lèi)似取Text需要這樣寫(xiě):document.getElementById("%=Button1.ClientID%").value
而不是直觀意義上document.getElementById("%=Button1.ClientID%").text
這個(gè)是javascript語(yǔ)言,就這個(gè)語(yǔ)法,這個(gè)屬性,就用value取值,在js中就是這樣,沒(méi)什么說(shuō)的
如果是在.net后臺(tái),才用Button1.Text