guiuse.java
来自「一个关于宠物统计的小程序。java编写。可供初学者学习。」· Java 代码 · 共 188 行
JAVA
188 行
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class GUIUse extends JFrame {
private boolean DEBUG = true;
private String str2=new String();
public GUIUse() {
//首先调用父类JFrame的构造方法生成一个窗口
super("label");
setLayout(new BorderLayout());
String labelStr="You like all these animals....";
final JLabel labelQue=new JLabel(labelStr);
int numButtons=5;
final String str1="so, you like: ";
final JLabel label=new JLabel(str1);
JCheckBox[] checkBoxs=new JCheckBox[numButtons];
//对各个复选框选项添加事件监听器
checkBoxs[0]=new JCheckBox("<html><font color=red>dog</font></html>");
checkBoxs[0].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
boolean foundIt=false;
int i=0;
int lenStr2=str2.length();
int len="dog".length();
while((!str2.regionMatches(i,"dog",0,len))&&i<lenStr2){
i++;
foundIt=false;
}
if(i<lenStr2){
foundIt=true;
}
if(!foundIt){
str2=str2+"dog ";
}
else{
StringBuffer temp=new StringBuffer(str2);
temp.delete(i,i+len);
str2=new String(temp);
}
label.setText(str1+str2);
}
});
checkBoxs[1]=new JCheckBox("<html><font color=yello>cat</font></html>");
checkBoxs[1].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
boolean foundIt=false;
int i=0;
int lenStr2=str2.length();
int len="cat ".length();
while((!str2.regionMatches(i,"cat ",0,len))
&&i<lenStr2){
i++;
foundIt=false;
}
if(i<lenStr2){
foundIt=true;
}
if(!foundIt){
str2=str2+"cat ";
}
else{
StringBuffer temp=new StringBuffer(str2);
temp.delete(i,i+len);
str2=new String(temp);
}
label.setText(str1+str2);
}
});
checkBoxs[2]=new JCheckBox("<html><font color=green>rabbit</font></html>");
checkBoxs[2].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
boolean foundIt=false;
int i=0;
int lenStr2=str2.length();
int len="rabbit ".length();
while((!str2.regionMatches(i,"rabbit ",0,len))&&i<lenStr2){
i++;
foundIt=false;
}
if(i<lenStr2){
foundIt=true;
}
if(!foundIt){
str2=str2+"rabbit ";
}
else{
StringBuffer temp=new StringBuffer(str2);
temp.delete(i,i+len);
str2=new String(temp);
}
label.setText(str1+str2);
}
});
checkBoxs[3]=new JCheckBox("<html><font color=blue>pig</font></html>");
checkBoxs[3].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
boolean foundIt=false;
int i=0;
int lenStr2=str2.length();
int len="pig ".length();
while((!str2.regionMatches(i,"pig ",0,len))&&i<lenStr2){
i++;
foundIt=false;
}
if(i<lenStr2){
foundIt=true;
}
if(!foundIt){
str2=str2+"pig ";
}
else{
StringBuffer temp=new StringBuffer(str2);
temp.delete(i,i+len);
str2=new String(temp);
}
label.setText(str1+str2);
}
});
checkBoxs[4]=new JCheckBox("<html><font color=black>bird</font></html>");
checkBoxs[4].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
boolean foundIt=false;
int i=0;
int lenStr2=str2.length();
int len="bird ".length();
while((!str2.regionMatches(i,"bird ",0,len))&&i<lenStr2){
i++;
foundIt=false;
}
if(i<lenStr2){
foundIt=true;
}
if(!foundIt){
str2=str2+"bird ";
}
else{
StringBuffer temp=new StringBuffer(str2);
temp.delete(i,i+len);
str2=new String(temp);
}
label.setText(str1+str2);
}
});
//生成面板
final JPanel pane1 = new JPanel();
pane1.add(labelQue);
//将面板添加入窗口中
getContentPane().add(pane1,BorderLayout.NORTH);
final JPanel pane2 = new JPanel();
for(int i=0;i<numButtons;i++){
pane2.add(checkBoxs[i]);
}
getContentPane().add(pane2,BorderLayout.CENTER);
JPanel pane3=new JPanel();
pane3.add(label);
getContentPane().add(pane3,BorderLayout.SOUTH);
//生成事件监听器
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public static void main(String[] args) {
GUIUse frame = new GUIUse();
frame.pack();
frame.setVisible(true);
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?