?? userjframe.java
字號:
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.JScrollPane;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import javax.swing.JOptionPane;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import javax.swing.JComboBox;
import javax.swing.JList;
/*
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
*/
class zoo{
int sum = 3;
static int birdn = 2;
static int fishn = 1;
static int burun = 0;
static int insectn = 0;
static Object birds[][] = new String[2][10];
static Object fishes[][] = new String[2][10];
static Object burus[][] = new String[2][10];
static Object insects[][] = new String[2][10];
public zoo(){
birds[0][0] = "企鵝";
birds[1][0] = "企鵝生長在南極,憨態可掬";
birds[0][1] = "駝鳥";
birds[1][1] = "駝鳥生長在澳洲,體態滑稽";
fishes[0][0] = "鯊魚";
fishes[1][0] = "鯊魚生長在深海,性情殘忍";
}
public static String showmessage(){
String str = "----------動物清單------------\n";
int i = 0;
str += "鳥類:\n\n";
for(int i1 = 0;i1 <= birdn - 1;i1++)
{str += Integer.toString(++i) + birds[0][i1] + "\n" + birds[1][i1] + "\n";
}
str += "--------------------------------------------------\n";
i = 0;
str += "魚類:\n\n";
for(int i1 = 0;i1 <= fishn - 1;i1++)
{str += Integer.toString(++i) + fishes[0][i1] + "\n" + fishes[1][i1] + "\n";
}
str += "--------------------------------------------------\n";
i = 0;
str += "哺乳類:\n\n";
for(int i1 = 0;i1 <= burun - 1;i1++)
{str += Integer.toString(++i) + burus[0][i1] + "\n" + burus[1][i1] + "\n";
}
str += "--------------------------------------------------\n";
i = 0;
str += "昆蟲類:\n\n";
for(int i1 = 0;i1 <=insectn - 1;i1++)
{str += Integer.toString(++i) + insects[0][i1] + "\n" + insects[1][i1] +"\n";
}
str += "--------------------------------------------------\n";
//str +=
return str;
}
}
public class UserJFrame extends JFrame
{
//添加按鈕
public UserJFrame()
{
super("動物園成員清單");
//this.setLayout(new FlowLayout(FlowLayout.CENTER));
this.setSize(360,200);
this.setLocation(300,240);
this.setResizable(false);
//this.setDefaultCloseOperation(EXIT_ON_CLOSE); //單擊窗口關閉按鈕時,結束程序運行
// this.setLayout(new GridLayout(3,1));
MyShowAnimalPanel myshow = new MyShowAnimalPanel();
this.getContentPane().add(myshow,BorderLayout.CENTER);
myshow.setVisible(true);
}
}
class MyShowAnimalPanel extends JPanel implements ActionListener,ItemListener
{ static alterframe myalterframe = new alterframe();
static addAndmodify addandmodify = new addAndmodify();
static JTextArea text_user; //文本區
private JComboBox alternative;
private JScrollPane scrollPane;
JButton b_refresh =new JButton("刷新列表");
public MyShowAnimalPanel(){
new zoo(); //完成輸入初始成員
//this.setLocation(600,240);
text_user = new JTextArea("",7,25);
text_user.setEditable(false);
scrollPane=new JScrollPane(text_user,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
this.add(scrollPane);
// this.add(text_user);
Object alter_content[] = {"選擇操作","添加","刪除","修改"};
alternative = new JComboBox(alter_content);
alternative.addItemListener(this);
this.add(alternative);
b_refresh.addActionListener(this);
this.add(b_refresh);
text_user.setText(zoo.showmessage());
//this.setVisible(true);
}
public void actionPerformed(ActionEvent e){
if (e.getSource()== b_refresh) {
JOptionPane.showMessageDialog(this,"無需刷新,此列表已經在您修改時自動更新過!呵呵");
}
}
public void itemStateChanged(ItemEvent e) //在組合框的下拉列表中選擇數據項時觸發執行
{ //實現ItemListener接口中的方法
switch (alternative.getSelectedIndex()) {
case 0:myalterframe.setVisible(false);
addandmodify.setVisible(false);
break;
case 1:myalterframe.setVisible(false);
addandmodify.setVisible(true);
break;
case 2:addandmodify.setVisible(false);
myalterframe.setVisible(true);
break;
case 3:myalterframe.setVisible(false);
addandmodify.setVisible(true);
break;
}
}
}
class alterframe extends JFrame implements ActionListener {
private JComboBox xuanzelei;
private JComboBox xuanzehao;
private JButton submit;
public alterframe(){
super("刪除成員");
this.setSize(360,100);
this.setLocation(300,440);
this.setLayout(new FlowLayout());
Object alter_content1[] = {"選擇類名","鳥類","魚類","哺乳類","昆蟲類"};
xuanzelei = new JComboBox(alter_content1);
Object alter_content2[] = {"選擇編號","1","2","3","4","5","6","7","8","9","10"};
xuanzehao = new JComboBox(alter_content2);
submit = new JButton("確定");
submit.addActionListener(this);
this.add(xuanzelei);
this.add(xuanzehao);
this.add(submit);
}
public void actionPerformed(ActionEvent e){
int i;//循環時用!
if (e.getSource()== submit) {
switch (xuanzelei.getSelectedIndex())
{
case 0:
break;
case 1:if(xuanzehao.getSelectedIndex() == 0)
break;
if((zoo.birdn == 0 )|| xuanzehao.getSelectedIndex() > zoo.birdn ){
JOptionPane.showMessageDialog(this,"無此編號的動物成員!");
break;}
zoo.birdn = zoo.birdn - 1;
for( i = xuanzehao.getSelectedIndex() - 1;i < zoo.birdn; i++){
zoo.birds[0][i] = zoo.birds[0][i + 1];
zoo.birds[1][i] = zoo.birds[1][i + 1];}
MyShowAnimalPanel.text_user.setText(zoo.showmessage());
break;
case 2:if(xuanzehao.getSelectedIndex() == 0)
break;
if((zoo.fishn == 0 )|| xuanzehao.getSelectedIndex() > zoo.fishn ){
JOptionPane.showMessageDialog(this,"無此編號的動物成員!");
break;}
zoo.fishn = zoo.fishn - 1;
for( i = xuanzehao.getSelectedIndex() - 1;i < zoo.fishn; i++){
zoo.fishes[0][i] = zoo.fishes[0][i + 1];
zoo.fishes[1][i] = zoo.fishes[1][i + 1];}
MyShowAnimalPanel.text_user.setText(zoo.showmessage());
break;
case 3:if(xuanzehao.getSelectedIndex() == 0)
break;
if((zoo.burun == 0 )|| xuanzehao.getSelectedIndex() > zoo.burun ){
JOptionPane.showMessageDialog(this,"無此編號的動物成員!");
break;}
zoo.burun = zoo.burun - 1;
for( i = xuanzehao.getSelectedIndex() - 1;i < zoo.burun; i++){
zoo.burus[0][i] = zoo.burus[0][i + 1];
zoo.burus[1][i] = zoo.burus[1][i + 1];}
MyShowAnimalPanel.text_user.setText(zoo.showmessage());
break;
case 4:if(xuanzehao.getSelectedIndex() == 0)
break;
if((zoo.insectn == 0 )|| xuanzehao.getSelectedIndex() > zoo.insectn ){
JOptionPane.showMessageDialog(this,"無此編號的動物成員!");
break;}
zoo.insectn = zoo.insectn - 1;
for( i = xuanzehao.getSelectedIndex() - 1;i < zoo.insectn; i++){
zoo.insects[0][i] = zoo.insects[0][i + 1];
zoo.insects[1][i] = zoo.insects[1][i + 1];}
MyShowAnimalPanel.text_user.setText(zoo.showmessage());
break;
}
}
}
}
class addAndmodify extends JFrame implements ActionListener, ItemListener{
private JComboBox xuanzelei;
private JTextArea shurudongwu;
private JTextArea shuruzhushi;
private JButton submit;
private int flag = 0; //用于下面的FOR循環
public addAndmodify(){
super("添加或者修改成員");
this.setSize(360,100);
//this.setVisible(true);
this.setLocation(300,440);
this.setLayout(new FlowLayout());
Object alter_content1[] = {"選擇類名","鳥類","魚類","哺乳類","昆蟲類"};
xuanzelei = new JComboBox(alter_content1);
this.add(xuanzelei);
shurudongwu = new JTextArea("輸入動物名稱",1,8);
this.add(shurudongwu);
submit = new JButton("提交");
submit.addActionListener(this);
this.add(submit);
shuruzhushi = new JTextArea("在此添加注釋",1,30);
this.add(shuruzhushi);
}
public void actionPerformed(ActionEvent e){
if (e.getSource()== submit) {
switch (xuanzelei.getSelectedIndex())
{
case 0:
break;
case 1:
if(zoo.birdn < 10){
for(int i=0;i<zoo.birdn;i++){
if(zoo.birds[0][i] == shurudongwu.getText()){
zoo.birds[1][i] = shuruzhushi.getText();
flag = 1;
break;}
}
if(flag ==0){
zoo.birdn++;
zoo.birds[0][zoo.birdn - 1] = shurudongwu.getText();
zoo.birds[1][zoo.birdn - 1] = shuruzhushi.getText();}
// System.out.println((String));
}
else
JOptionPane.showMessageDialog(this,"額定超員:每類動物數額要小于10!");
break;
case 2:
if(zoo.fishn < 10){
zoo.fishn++;
zoo.fishes[0][zoo.fishn - 1] = shurudongwu.getText();
zoo.fishes[1][zoo.fishn - 1] = shuruzhushi.getText();}
else
JOptionPane.showMessageDialog(this,"額定超員:每類動物數額要小于10!");
break;
case 3:
if(zoo.burun < 10){
zoo.burun++;
zoo.burus[0][zoo.burun - 1] = shurudongwu.getText();
zoo.burus[1][zoo.burun - 1] = shuruzhushi.getText();}
else
JOptionPane.showMessageDialog(this,"額定超員:每類動物數額要小于10!");
break;
case 4:
if(zoo.insectn < 10){
zoo.insectn++;
zoo.insects[0][zoo.insectn - 1] = shurudongwu.getText();
zoo.insects[1][zoo.insectn - 1] = shuruzhushi.getText();}
else
JOptionPane.showMessageDialog(this,"額定超員:每類動物數額要小于10!");
break;
default: System.out.println("Data Error!");
}
}
System.out.println(zoo.insectn);
MyShowAnimalPanel.text_user.setText(zoo.showmessage());
}
public void itemStateChanged(ItemEvent e){
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -