📄 frame1.java~17~
字号:
package hi_lo;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Frame1 extends JFrame {
private JPanel contentPane;
private JTextField jTextField1 = new JTextField();
private JButton jButton1 = new JButton();
private JTextField jTextField2 = new JTextField();
private JLabel jLabel1 = new JLabel();
int times = 6;
int rand;
int count = 0;
int input;
//Construct the frame
public Frame1() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
//setIconImage(Toolkit.getDefaultToolkit().createImage(Frame1.class.getResource("[Your Icon]")));
contentPane = (JPanel) this.getContentPane();
jTextField1.setBounds(new Rectangle(62, 33, 251, 47));
jTextField1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jTextField1_actionPerformed(e);
}
});
contentPane.setLayout(null);
this.setSize(new Dimension(400, 300));
this.setTitle("Frame Title");
jButton1.setBounds(new Rectangle(80, 139, 116, 38));
jButton1.setText("start game");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton1_actionPerformed(e);
}
});
jTextField2.setBounds(new Rectangle(162, 203, 151, 41));
jTextField2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jTextField2_actionPerformed(e);
}
});
jLabel1.setText("set times");
jLabel1.setBounds(new Rectangle(60, 203, 80, 40));
contentPane.add(jTextField1, null);
contentPane.add(jButton1, null);
contentPane.add(jTextField2, null);
contentPane.add(jLabel1, null);
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
void jButton1_actionPerformed(ActionEvent e) {
rand = (int)(Math.random() * 100);
count = 0;
System.out.println(rand);
}
void jTextField2_actionPerformed(ActionEvent e) {
times = Integer.parseInt(jTextField2.getText());
}
void jTextField1_actionPerformed(ActionEvent e) {
if(count >= times)
{
System.out.println("times out!");
return;
}
input = Integer.parseInt(jTextField1.getText());
if(rand == input)
{
System.out.println("matched!");
}
else
{
if(input > rand) System.out.println("high");
else System.out.println("low");
jTextField1.setText("");
count++;
}
}
public void selectionSort(int[] number)
{
int startIndex,minIndex,length,temp;
length = number.length;
for(startIndex = 0; startIndex <= length - 2; startIndex++)
{
//each iteration of the for loop is one pass
minIndex = startIndex;
//find the smallest in this pass at position minIndex
for(int i = startIndex + 1; i <= length - 1; i++)
if(number[i] < number[minIndex]) minIndex = i;
//exchange number[startIndex] and number[minIndex]
temp = number[startIndex];
number[startIndex] = number[minIndex];
number[minIndex] = temp;
}
}
public int linearSearch(int[] number,int searchValue)
{
int loc = 0;
while(loc < number.length && number[loc] != searchValue) loc++;
if(loc == number.length) return -1; //not found
else return loc; //found,return the position
}
public int binarySearch(int[] number,int searchValue)
{
int low = 0;
int high = number.length - 1;
int mid = (low + high)/2;
while(low <= high && number[mid] != searchValue)
{
if(number[mid] < searchValue) low = mid + 1;
else high = mid - 1;
mid = (low + high)/2;
}
if(low > high) mid = -1;
return mid;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -