本文目錄一覽:
- 1、JAVA下拉按鈕選項監聽事件怎麼寫?
- 2、java swing如何實現工具欄的下拉按鈕
- 3、如何向java jframe中添加下拉列表 按鈕 文本框 最後把信息存儲在文件里
- 4、java下拉按鈕
- 5、如何用java實現下拉菜單
- 6、如何用JAVA來實現下拉菜單的功能
JAVA下拉按鈕選項監聽事件怎麼寫?
javax.swing.JComboBox
void
addItemListener(ItemListener aListener)
Adds an ItemListener.
java swing如何實現工具欄的下拉按鈕
用JComboBox xx=new JComboBox();這是下拉列表組件。
xx.addItem(“雙色球”);
xx.addItem(“3D”);
xx.addItem(“七樂彩”);
通過這個方法添加菜單。
更加具體的要自己查jdk幫助文檔,以後會查文檔才是王道
如何向java jframe中添加下拉列表 按鈕 文本框 最後把信息存儲在文件里
lz 你好
(ps: lz 加點兒分吧 太少了……)
具體代碼如下:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class Test extends JFrame {
private JLabel name,phone,sex;
private JTextField inputName,inputPhone;
private JComboBox sexBox;
private String[] item = {“男”, “女”};
private JButton save;
public Test() {
super(“信息管理”);
setSize(240,300);
setLayout(new FlowLayout(FlowLayout.CENTER, 10, 30));
name = new JLabel(“姓名:”);
phone = new JLabel(“手機號:”);
sex = new JLabel(“性別:”);
inputName = new JTextField(14);
inputPhone = new JTextField(14);
sexBox = new JComboBox(item);
save = new JButton(“儲存”);
save.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(inputName.getText().equals(“”) || inputPhone.getText().equals(“”)) {
return;
}
try {
File f = new File(“info.txt”);
BufferedWriter bw = new BufferedWriter(new FileWriter(f, true));
if(f.length() == 0){
bw.write(“姓名\t性別\t手機號”);
bw.newLine();
}
String str = inputName.getText()+”\t”+sexBox.getSelectedItem()+”\t”+inputPhone.getText();
bw.write(str);
bw.newLine();
bw.close();
} catch (Exception ex) {
}
}
});
getContentPane().setBackground(Color.WHITE);
getContentPane().add(Box.createHorizontalStrut(5));
getContentPane().add(name);
getContentPane().add(inputName);
getContentPane().add(phone);
getContentPane().add(inputPhone);
getContentPane().add(Box.createHorizontalStrut(35));
getContentPane().add(sex);
getContentPane().add(sexBox);
getContentPane().add(Box.createHorizontalStrut(35));
getContentPane().add(save);
setLocationRelativeTo(null);
setVisible(true);
setDefaultCloseOperation(3);
}
public static void main (String[] args) {
new Test();
}
}
希望能幫助你哈
java下拉按鈕
你可以當成是彈出式菜單。 黑三角相當如按鈕,點擊按鈕後,菜單彈出。
個人觀點,大家可以補充。。
如何用java實現下拉菜單
//核心代碼
String[] sg= {“蘋果”,”香蕉”,”櫻桃”,”山楂”};
JComboBoxString jcb = new JComboBoxString(sg);
完整代碼
import java.awt.*;
import javax.swing.*;
public class MFrame extends JFrame{
public MFrame() {
String[] sg= {“蘋果”,”香蕉”,”櫻桃”,”山楂”};
JComboBoxString jcb = new JComboBoxString(sg);
add(jcb);
setLayout(new FlowLayout());//把JFrame設置成流式布局
setTitle(“測試”);//設置窗口標題
setSize(220, 100);//設置窗口大小
setLocationRelativeTo(null);//設置窗口居中
setDefaultCloseOperation(EXIT_ON_CLOSE);//設置點擊關閉退出jvm虛擬機
setVisible(true);//設置窗口可見
}
public static void main(String[] args) {
new MFrame();//創建窗口
}
}
如何用JAVA來實現下拉菜單的功能
import java.awt.*;
import java.awt.event.*;
class xl Frame implements ItemListener //借口
{
static Choice chc=new Choice();//創建下拉框的對象
}
public static void main(String args[])
{
frm.setLayout(new FlowLayout());//創建布局格式
frm.setSize(200,200);//設置框架大小
frm.add(chc);//把下拉框組件加入到框架裡面
chc.addItemListener(frm);//把chc設置為聆聽者
chc.add(‘上海’);
chc.add(‘北京’);//在下拉框裡面加入所需要的東西,例如上海,北京。
frm.setVisible(true);//窗體設為可視
}
//後面借著就是對應觸發下拉框索要執行的。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/270729.html