?? tabbedpanedemo.java
字號:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;
public class TabbedPaneDemo extends JPanel {
//存放學號arrayno和成績arraycj的數組;
private String[] arrayno=new String[100];
private String[] arraycj=new String[100];
private static int count=0;//計算錄入個數;
//建立四個用于輸入的區域;
JTextField tf1=new JTextField(16);
JTextField tf2=new JTextField(16);
JTextField tf3=new JTextField(16);
JTextField tf4=new JTextField(16);
//用于排序后輸出區域;
JTextArea ta=new JTextArea();
//插入按鈕事件;
private class buttonhandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(tf1.getText().length()!=0)//輸入不為空;
{
arrayno[count]=tf1.getText();
arraycj[count]=tf2.getText();
count++;
}
//清空;
tf1.setText("");
tf2.setText("");
}
}
//查詢按鈕事件;
private class buttonhandler2 implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String aa=new String();
aa=tf3.getText();
int i=0;
while(i<count)
{
if(arrayno[i].equals(aa)) {tf4.setText(arraycj[i]);break;}
else if(i==count-1){tf4.setText("no data!");} //判斷查找是否結束;
i++;
}
}
}
//排序按鈕事件;
private class buttonhandler3 implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
//冒泡排序
for (int i = 0; i <count; i++)
{
for (int j = i; j <count; j++)
{
if (Integer.parseInt(arraycj[i]) > Integer.parseInt(arraycj[j]))
{
String temp=arraycj[i];
arraycj[i]=arraycj[j];
arraycj[j]=temp;
temp=arrayno[i];
arrayno[i]=arrayno[j];
arrayno[j]=temp;
}
}
}
ta.setText("");//先清空顯示區間;
ta.append("No.\t\tMark\n");
for(int k=0;k<count;k++)
{
ta.append(arrayno[k]+"\t\t"+arraycj[k]+"\n");
}
}
}
public TabbedPaneDemo()
{
super(new GridLayout(1, 1));
JTabbedPane tabbedPane = new JTabbedPane();
//建立3個圖標;
ImageIcon icon1 = new ImageIcon("1.gif");
ImageIcon icon2 = new ImageIcon("2.gif");
ImageIcon icon3 = new ImageIcon("3.gif");
//成績插入面板;
JComponent panel1 = makeTextPanel("");
tabbedPane.addTab("成績輸入", icon1, panel1,"write");
Box baseBox,box1,box2,box3;
box1=Box.createVerticalBox();
box1.add(new JLabel("學生學號:"));
box1.add(Box.createVerticalStrut(8));
box1.add(new JLabel("學生成績:"));
box2=Box.createVerticalBox();
box2.add(tf1);
box2.add(Box.createVerticalStrut(10));
box2.add(tf2);
box3=Box.createHorizontalBox();
JButton button1=new JButton("確定");
button1.addActionListener(new buttonhandler());
box3.add(button1);
baseBox=Box.createHorizontalBox();
box2.add(Box.createVerticalStrut(8));
baseBox.add(box1);
baseBox.add(Box.createHorizontalStrut(10));
baseBox.add(box2);
baseBox.add(Box.createHorizontalStrut(10));
baseBox.add(box3);
baseBox.add(Box.createVerticalStrut(10));
panel1.setLayout(new FlowLayout());
panel1.add(baseBox);
tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);
//成績查詢面板;
JComponent panel2 = makeTextPanel(" ");
tabbedPane.addTab("成績查詢", icon2, panel2,"read");
panel2.setLayout(new FlowLayout());
Box baseBox2,box4,box6;
box4=Box.createHorizontalBox();
box4.add(new JLabel("學生學號:"));
box4.add(tf3);
box6=Box.createHorizontalBox();
box6.add(new JLabel("學生成績:"));
box6.add(tf4);
JButton button2=new JButton("查詢");
button2.addActionListener(new buttonhandler2());
box4.add(button2);
baseBox2=Box.createVerticalBox();
baseBox2.add(box4);
baseBox2.add(box6);
panel2.add(baseBox2);
tabbedPane.setMnemonicAt(1, KeyEvent.VK_2);
//排序面板;
JComponent panel3 = makeTextPanel("");
tabbedPane.addTab("成績排序", icon3, panel3,"sort");
panel3.setLayout(new BorderLayout());
JButton button3=new JButton("排序");
button3.addActionListener(new buttonhandler3());
panel3.add(button3,"North");
panel3.add(new JScrollPane(ta),BorderLayout.CENTER);
tabbedPane.setMnemonicAt(2, KeyEvent.VK_3);
panel1.setPreferredSize(new Dimension(410, 150));
add(tabbedPane);
tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
}
protected JComponent makeTextPanel(String text) {
JPanel panel = new JPanel(false);
JLabel filler = new JLabel(text);
filler.setHorizontalAlignment(JLabel.CENTER);
panel.setLayout(new GridLayout(1, 1));
panel.add(filler);
return panel;
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("學生成績系統");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TabbedPaneDemo(), BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
UIManager.put("swing.boldMetal", Boolean.FALSE);
createAndShowGUI();
}
});
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -