中。
//test9_8
import java.awt.Container; import java.awt.GridLayout; import java.awt.event.MouseEvent; import java.awt.event.MouseListener;
import javax.swing.DefaultListModel; import javax.swing.JFrame; import javax.swing.JList;
public class test9_8 extends JFrame implements MouseListener {
listModel1 = new DefaultListModel(); listModel1.addElement(\); listModel1.addElement(\); listModel1.addElement(\);
listModel2=new DefaultListModel(); listModel2.addElement(\); listModel2.addElement(\); listModel2.addElement(\); jList1 = new JList(listModel1);
jList2=new JList(listModel2); jList1.addMouseListener(this); jList2.addMouseListener(this); pane.add(jList1); pane.add(jList2);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); public void init() {
JFrame jFrame = new JFrame(\练习使用List\); Container pane = jFrame.getContentPane(); pane.setLayout(new GridLayout(1,2)); public JFrame jFrame; public JList jList1; public JList jList2;
public DefaultListModel listModel1; public DefaultListModel listModel2;
}
jFrame.pack();
jFrame.setVisible(true);
@Override
public void mouseClicked(MouseEvent e) { }
@Override
public void mousePressed(MouseEvent e) { }
@Override
public void mouseReleased(MouseEvent e) { }
@Override
public void mouseEntered(MouseEvent e) { }
@Override
public void mouseExited(MouseEvent e) { }
// TODO Auto-generated method stub
// TODO Auto-generated method stub
// TODO Auto-generated method stub
// TODO Auto-generated method stub
// TODO Auto-generated method stub if(e.getSource() == jList1){ }
if(e.getSource()==jList2){
int index = jList2.locationToIndex(e.getPoint()); Object o = jList2.getModel().getElementAt(index); listModel1.addElement(o);
int index = jList1.locationToIndex(e.getPoint()); Object o = jList1.getModel().getElementAt(index); listModel2.addElement(o);
}
}
public static void main(String[] args) { }
new test9_8().init();
运行结果:
9.练习使用JComboBox。包括一个JLable、一个JComboBox,可以通过输入或者选择JComboBox中的某一项来控制JLable中文字的大小。 //test9_9
import java.awt.Container; import java.awt.Font; import java.awt.GridLayout; import java.awt.event.ItemEvent; import java.awt.event.ItemListener;
import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane;
public class test9_9 extends JFrame implements ItemListener {
public JFrame jFrame;
}
private JLabel jLabel; private JComboBox jComboBox;
private String[] fontSize = { \, \, \, \, \, \ }; public void init() { }
public void itemStateChanged(ItemEvent e) { }
public static void main(String[] args) { }
new test9_9().init();
//使用这个判断的目的的是为了避免触发事件总是执行两次的问题 if (e.getStateChange() == ItemEvent.SELECTED) { }
try {
String s = jComboBox.getSelectedItem().toString(); int intFontSize = Integer.parseInt(s);
jLabel.setFont(new Font(\, 0, intFontSize)); System.out.println(intFontSize);
jFrame = new JFrame(\练习ComboBox\);
Container container = jFrame.getContentPane(); container.setLayout(new GridLayout(2, 1));
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jLabel = new JLabel(\你好吗?\);
jLabel.setFont(new Font(\, 0, 10));// 设置字体大小 jComboBox = new JComboBox(fontSize);
jComboBox.setEditable(true);// 设置复选框可以输入
jComboBox.addItemListener(this);// 添加ItemListener监听 // 添加两个组件
container.add(jLabel); container.add(jComboBox); jFrame.pack();// 排版
jFrame.setVisible(true);// 显示
//
} catch (NumberFormatException r) {
JOptionPane.showMessageDialog(this, \请输入整数\); }
运行结果:
10.练习使用JTable。包括姓名、学号、语文成绩、数学成绩、总分五项,单击总分会自动将语文数学成绩相加。
import java.awt.event.*; import javax.swing.*; import javax.swing.table.*;
public class Test9_10 extends JFrame implements MouseListener {
private JTable jTable;
private Object[][] cellData = { { \姓名\, new String(\林楚金\) },
{ \学号\, new String(\) }, { \语文成绩\, new String(\) },
{ \数学成绩\, new String(\) }, { \总分\, new String(\点击出
总分\) } }; private String[] columnNames = { \, \ };
public void mouseClicked(MouseEvent e) { }
this.setTitle(\表格演示\); this.setSize(300, 350); this.setLocation(200, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true);
public Test9_10() {
DefaultTableModel model = new DefaultTableModel(cellData, };
jTable = new JTable(model); jTable.addMouseListener(this); this.add(jTable);
public boolean isCellEditable(int row, int column) { }
return false;
columnNames) {