jTextField = new JTextField(\输入\); // 窗体加入各组件 this.add(label1); this.add(jTextField); this.add(label2); this.add(label3); this.add(buttonOfSure); this.add(buttonOfExit); // 显示窗体
this.setVisible(true);
System.out.println(numberOfRandom);// 偷偷看看随机数,哈哈 }
// 事件处理
public void eventHandle() {
//捕获非整型输入异常 try {
numberOfUser = Integer.parseInt(jTextField.getText());//获得
用户输入数 if (numberOfUser < 1 || numberOfUser > 100) {
JOptionPane.showMessageDialog(this, \请输入1到100之间的整
数\);
}
} else { }
JOptionPane.showMessageDialog(this, \请输入整数\, \输入错误\, JOptionPane.WARNING_MESSAGE); jTextField.requestFocus();
if (numberOfUser > numberOfRandom) {
label3.setText(\偏大\);
jTextField.requestFocus();//清空文本框并使重新获得焦点
}
if (numberOfUser < numberOfRandom) { }
if (numberOfUser == numberOfRandom) { }
label3.setText(\恭喜你,答对了。\); jTextField.requestFocus(); label3.setText(\偏小\);
jTextField.requestFocus();
} catch (NumberFormatException e) {
}
public static void main(String[] args) { }
new GuessNumber().init(); }
//按钮触发选择
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand() == \确定\) { this.eventHandle(); }
if (e.getActionCommand() == \退出\) { System.exit(0); } }
运行结果:
7.练习使用JscrollPane。使用BorderLayout将JFrame布局分为左右两块;左边又使用
GridLayout,包含三个按钮,右边在JLabel里显示一幅图画,按钮控制JLabel是否显示滚动条。 //test9_7
import java.awt.BorderLayout; import java.awt.Button; import java.awt.Container; import java.awt.GridLayout;
import java.awt.event.ActionEvent; import java.awt.event.ActionListener;
import javax.swing.ImageIcon; import javax.swing.JFrame;
import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
public class test9_7 extends JFrame implements ActionListener {
// 将左右两边的中间容器放弃JFrame中
// 读取图片作为图标
picture = new ImageIcon(\); // 将图标赋给标签label
label = new JLabel(picture);
// 定义滚动框,总是显示滚动条
jscrollPane = new JScrollPane(label,
ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
// 定义左边镶板JPanel框和三个按钮
jPanel = new JPanel(new GridLayout(3, 1, 0, 30)); button1 = new Button(\滚动\);
button1.addActionListener(this); button2 = new Button(\试试\);
button2.addActionListener(this); button3 = new Button(\退出\);
button3.addActionListener(this); jPanel.add(button1); jPanel.add(button2); jPanel.add(button3);
private JPanel jPanel;//左边panel框 private Button button1; private Button button2; private Button button3;
private JScrollPane jscrollPane;//右边滚动框 private JLabel label;//滚动框中的标签
public ImageIcon picture = null;//标签中的图标
public boolean whetherCroll = true;//用于切换滚动条的显示 public void init() {
JFrame jFrame = new JFrame(\练习使用JscrollPane\); Container pane = jFrame.getContentPane();
this.setDefaultCloseOperation((JFrame.EXIT_ON_CLOSE));
}
pane.add(jPanel, BorderLayout.WEST); pane.add(jscrollPane, BorderLayout.EAST); //排版显示
jFrame.pack();
jFrame.setVisible(true);
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub if (e.getActionCommand() == \滚动\) { if (whetherCroll) {
jscrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROL
LBAR_NEVER);//垂直不显示
jscrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);//水平不显示 whetherCroll = false;
} else {
jscrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROL
LBAR_ALWAYS);//垂直显示
jscrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);//水平显示 whetherCroll = true;
public static void main(String[] args) { }
new test9_7().init(); }
}
if (e.getActionCommand() == \试试\) { JOptionPane.showMessageDialog(this, }
if (e.getActionCommand() == \退出\) { System.exit(0); }
\);
}
}
运行结果: 显示滚动条
点击“滚动”按钮后,没显示滚动条
8.练习使用JList。建立两个JList,双击其中任何一个中的某一项,此项就会跑到另外一个JList