導航:首頁 > 編程語言 > java菜單小程序

java菜單小程序

發布時間:2021-12-03 06:54:41

1. java swing求各位大哥大姐幫忙寫個小程序,點擊菜單欄,下面換圖片,百度上的國旗類似,但她的不能用

lz 你好

已經幫你實現了

希望能幫助你哈

2. 求一個簡單的JAVA小程序,要有圖形界面。

/*計算器*/
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;
public class Calculator implements ActionListener{
JFrame frame;
JPanel panel;
JTextField tfShow;/*定義顯示文本框*/
JButton b1[]=new JButton[10]; /*數字按鈕*/
JButton b2[]=new JButton[6]; /*操作按鈕*/
boolean isNumber;/*判斷是否輸入多位數字的變數*/
double number;/*存儲輸入數值、顯示結果的變數*/
double result;/*存儲中間運算結果的變數*/
char operator;/*存儲當前操作符的成員變數*/
public Calculator(){
frame=new JFrame("計算器");
frame.setSize(300,300);/*指定框架窗口的大小*/
frame.setResizable(false);/*使框架窗口不可改變大小*/
JPanel contentPane=(JPanel)frame.getContentPane();
contentPane.setBorder(new EmptyBorder(20,20,20,20));/*繪制框架的指定大小的空透明邊框*/
tfShow=new JTextField("0",25);/*指定屬性的文本域*/
tfShow.setHorizontalAlignment(JTextField.RIGHT);/*設置文本域中文本的對齊方式*/
isNumber=true;/*初始值設置*/
number=0;/*初始值設置*/
result=0;/*初始值設置*/
operator=' ';/*初始值設置*/
for(int i=0;i<b1.length;i++){
b1[i]=new JButton(Integer.toString(i));/*創建數字按鈕*/
b1[i].setActionCommand(Integer.toString(i));
b1[i].addActionListener(this);
b1[i].setForeground(Color.blue);
}
String bs[]={"/","*","-","C","+","="};
for(int i=0;i<b2.length;i++){
b2[i]=new JButton(bs[i]);/*創建操作按鈕*/
b2[i].setActionCommand(bs[i]);
b2[i].addActionListener(this);
b2[i].setForeground(Color.red);
}
panel=new JPanel();
panel.setLayout(new GridLayout(4,5));
panel.add(b1[1]);
panel.add(b1[2]);
panel.add(b1[3]);
panel.add(b2[0]);
panel.add(b1[4]);
panel.add(b1[5]);
panel.add(b1[6]);
panel.add(b2[1]);
panel.add(b1[7]);
panel.add(b1[8]);
panel.add(b1[9]);
panel.add(b2[2]);
panel.add(b1[0]);
panel.add(b2[3]);
panel.add(b2[4]);
panel.add(b2[5]);
frame.add(tfShow,BorderLayout.NORTH);/*將文本框放置在框架上方*/
frame.add(panel,BorderLayout.CENTER);/*將裝有按鈕組的panel放在框架的中心*/
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);/*設置框架窗口的默認窗口關閉操作*/
frame.setVisible(true);/*設置框架可見*/
}
public double getDisplay(){/*返回要顯示的結果*/
return number;
}
public void reDisplay(){/*刷新文本域的內容*/
tfShow.setText(""+getDisplay());
}
/*對輸入數字的處理*/
public void numberProcess(int num){
if(isNumber&&num!=0){
String s1=Integer.toString(num);
String s2=Integer.toString((int)(this.number));
this.number=Double.parseDouble(s2+s1);/*對多位數字的處理*/
}else{
this.number=num;
}
isNumber=true;/*輸入連續數字(即多位數字)時為真*/
}
public void operationProcess(char operator){/*根據輸入的操作符改變當前操作符*/
switch(operator){
case '-':
this.operator='-';
break;
case '+':
this.operator='+';
break;
case '*':
this.operator='*';
break;
case '/':
this.operator='/';
break;
}
result=number;
isNumber=false;/*輸入操作符時表示輸入連續數字的標記變數為假*/
}
public void clear(){
number=0;
result=0;
}
public void equal(){/*計算運算結果*/
switch(operator){
case '-':
result=result-number;
break;
case '+':
result=result+number;
break;
case '*':
result=result*number;
break;
case '/':
result=result/number;
break;
case ' ':
result=number;
break;
}
number=result; /*把運算結果賦值給顯示變數*/
isNumber=false;
operator=' ';
}
public static void main(String args[]){
Calculator cal=new Calculator();/*創建計算器*/
}
public void actionPerformed(ActionEvent e){
String command=e.getActionCommand();/*獲取按鈕激發的操作事件的命令名稱*/
char c=command.charAt(0);/*將按鈕命令名稱的第一個字元賦值給一個字元c*/
switch(c){
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '0':
int number=Integer.parseInt(command);
numberProcess(number);/*輸入數字的處理*/
break;
case '+':
case '-':
case '*':
case '/':
operationProcess(c);/*算數運算符的處理*/
break;
case '=':equal();break;/*計算運算結果*/
case 'C':clear();break;/*清零*/
}
reDisplay(); /*在文本域中顯示信息*/
}
}
這是我做的一個計算器:運行截圖

3. 求JAVA窗口小程序

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
@SuppressWarnings("serial")
public class LoginFrame extends JFrame {
private String ACC = "張三";
private String PW = "123";
private JTextField account;
private JPasswordField password;
private JButton regist;
private JButton login;
private JButton exit;

public LoginFrame() {
setLayout(new GridBagLayout());//將布局管理器設置成GridBagLayout
GridBagConstraints con = new GridBagConstraints();//這個對象主要用來控制組件的位置以及其他一些信息
con.insets = new Insets(5,5,5,5);//將組件的周圍緊密度設置成5個像素寬

con.gridx = 0;//x坐標在0位置
con.gridy = 0;//y坐標在0位置
add(new JLabel("帳號:"), con);//添加組件時注意第二個參數要放GridBagConstraints這個對象

con.gridx++;
account = new JTextField(15);
add(account, con);

con.gridx = 0;
con.gridy++;
add(new JLabel("密碼:"), con);

con.gridx++;
password = new JPasswordField(15);
add(password, con);

con.gridx = 0;
con.gridy++;
con.gridwidth = 2;//在該坐標上占兩個格子的位置
JPanel p = new JPanel(new FlowLayout(FlowLayout.CENTER, 5, 5));
regist = new JButton("注冊");
regist.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ACC = account.getText();
PW = new String(password.getPassword());
JOptionPane.showMessageDialog(LoginFrame.this, "注冊成功!");
}
});
p.add(regist);

login = new JButton("登錄");
login.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(ACC.equals(account.getText()) && PW.equals(new String(password.getPassword()))) {
JOptionPane.showMessageDialog(LoginFrame.this, "登錄成功!");
} else {
JOptionPane.showMessageDialog(LoginFrame.this, "用戶名或密碼錯誤!");
}
}
});
p.add(login);

exit = new JButton("退出");
exit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
p.add(exit);
add(p, con);

pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}

public static void main(String[] args) {
new LoginFrame();
}
}

4. 求一個三種設計模式結合的java小程序

//##食堂:DinningHall.java
publicclassDinningHall{

privateDinningHallinstance=null;
privateDinningHall{
}

(){

//單例模式:創建食堂對象
if(instance==null){

instance=newDinningHall();
}

returninstance;

}
publicFoodcreateFood(Stringorder){

//工廠模式:創建一些吃的
if("1號菜".equals(order)){

//比如菜單上1號菜是魚香肉絲
returnnewYuXiangRouSi();
}
elseif("2號菜".equals(order)){

//比如菜單上2號菜是宮保雞丁
returnnewGongBaoJiDing();
}
else{

returnnull;

}

}

//建造者模式start:集中管理,生產一些吃的東西
privateList<Food>list=newArrayList<Food>();
publicList<Food>getFoods(){
returnlist;

}

publicvoidproceSomeMantou(intcount){
for(inti=0;i<count;i++){
list.add(newManTou());
}
}
//建造者模式end

}
//##食物:Food.java
publicinterfaceFood(){
publicStringgetName();

}
//##魚香肉絲:YuXiangRouSi.java
(){
publicStringgetName(){
return"魚香肉絲";

}
}
//##宮保雞丁:GongBaoJiDing.java
(){
publicStringgetName(){
return"宮保雞丁";

}
}
//##饅頭:Mantou.java
(){
publicStringgetName(){
return"饅頭";

}
}
//##測試類Tester.java
publicclassTester{
publicstaticvoidmain(){

//1.單例模式
DinningHalldh=DinningHall.getInstance();
//2.工廠模式
Foodf1=DinningHall.createFood("1號菜");
System.out.println(f1.getName());

//3.建造者模式
DinningHall.proceSomeMantou(5);
List<Food>foods=DinningHall.getFoods();
for(Foodf:foods){

System.out.println(foods+" ");

}

System.out.println();
}
}

5. 做一個java小程序

可以幫寫,需要可以聯系我

6. 求java經典小程序代碼

7. java小程序如何實現以下操作:點擊菜單選項中的「選擇圖片」出現一個打開文件對話框,可以自己選擇圖片

自己寫的,不過有一點就是圖片的大小是固定的
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.io.*;

public class Tupian extends JFrame implements ActionListener{
JLabel jl=new JLabel("圖片");
JMenuBar jmb=new JMenuBar();
JMenu jm=new JMenu("文件");
JMenuItem jmi=new JMenuItem("選擇圖片");
JPanel jp=new JPanel(new FlowLayout(FlowLayout.CENTER));
public Tupian() {
super("瀏覽圖片");
jmb.add(jm);
jm.add(jmi);
jp.add(jl);
jmi.addActionListener(this);
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
this.setLayout(new BorderLayout());
this.add(jmb,BorderLayout.NORTH);
this.add(jp,BorderLayout.CENTER);
this.setSize(800,600);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e){
JFileChooser chooser=new JFileChooser();
int i=chooser.showOpenDialog(this);
if(i==chooser.APPROVE_OPTION){
Image image=new ImageIcon(chooser.getSelectedFile().getPath()).getImage();//調整
image=image.getScaledInstance(300, 400, Image.SCALE_DEFAULT );//圖像300,400
jl.setIcon(new ImageIcon(image));
jl.setText("");
}
if(i==chooser.CANCEL_OPTION)return;
}
public static void main (String[] args) {
new Tupian();
}
}

8. 求一個JAVA設計小程序,使用至少兩種JAVA設計模式,求完整代碼!!!

武將來 伍孚、李傕、郭自汜、顏良、文丑、潘鳳、俞涉、武安國、穆順、華雄、牛輔、張濟、樊稠、胡軫、胡車兒、李肅、高順、張任、高覽、曹性、閔純、紀靈、馬休、馬鐵、高覽、袁譚、袁熙、袁尚、高幹、麴義、呂翔、呂曠、韓猛、淳於瓊、焦觸、張南、馬延、雷薄、張勛、陳紀、橋蕤、郝萌、侯成、宋憲、魏續、成廉、蔡瑁、張允、黃祖、蘇飛、呂公、侯選、程銀、李堪、張橫、梁興、成宜、馬玩、楊秋

9. java實現GUI小程序

..這個好基礎。。我在這里不想打代碼了。。你想確定好布局,這個總體用border布局,回北,中,南,然後分別加答入組件就好,上面一個lable和一個Jcombox,中間就JTextField然後lable,下面2個JButton。搞定。

10. java小程序 選中一個菜單時用一個Label顯示選的菜單項名在Frame上

你是想顯示在你設置的label上吧。那個label應該這樣:首先聲明,在
String s="";下面加一句
Label show = null;
然後構造方法里初始化show這樣寫:show= new Label();
然後在actionPerformed方法里這樣寫:
MenuItem co=(MenuItem)e.getSource();
if(co==boy)
{
s="I am a boy";
show.setText(s);
}
else if(co==girl)
{
s="I am a girl";
show.setText(s);
}
else
System.exit(0);
只有setText才可以給Label賦值,並讓他顯示出來,開始你直接初始化的時候寫的Label show= new Label(s);並不能隨時改變s的值。明白了么?
如果想要調好的程序留下郵箱我發給你。

閱讀全文

與java菜單小程序相關的資料

熱點內容
電影14路末班車免費觀看 瀏覽:665
ak愛看福利電影院 瀏覽:438
配置文件的工具 瀏覽:886
跨平台編程語言哪個好 瀏覽:316
win7桌面文件整理 瀏覽:757
如何進入電子網路發票應用系統 瀏覽:80
一部電影女主是蛇變的 瀏覽:779
恐怖片吻戲 瀏覽:62
有個小說主角叫楚天 瀏覽:884
大數據的內涵有哪些內容 瀏覽:800
日本大奶 電影 瀏覽:91
大數據反間 瀏覽:945
一個電影坐別人車後面有血 瀏覽:852
空姐大戰猛鬼電影 瀏覽:507
朴惠麗韓劇什麼電視 瀏覽:554
《柏林漂流》未刪減 瀏覽:804
男主有戰爭後遺症小說 瀏覽:44
泰國精子電影 瀏覽:859
杭州大樹網路技術 瀏覽:83
截取pdf文件的某些頁 瀏覽:98

友情鏈接