一、Java GUI图形用户界面可以进行简单的窗口界面设计,那么现在设计一个简单的表白窗口
1.标签内容有表白对象,说一句你想表白的话,是否愿意余生与ta一起过的单选框
2.连接数据库的按钮有重新表白,继续表白,和退出,那么我们将使用监听器来实现其三个按钮的功能
表白窗口最后实现的样式如下图:
二、实现的过程及相关组件
1.相关组件
JTextField 标签文本框
JRadioButton[ ] 单选按钮
JButton 按钮
ButtonGroup 按钮组
JLabel 标签
2.实现过程
(1)首先设置好窗口为绝对布局,并设置窗口大小
(2)定义标签对象和标签文本框,并进行界面的实现
(3)是否愿意设置为单选的数组按钮
(4)使用监听器来实现动作按钮的监听实现
3.按钮功能
点击继续表白,表白的数据进入数据库object对象内
点击重新表白,表白的数据清空,需重新输入
点击退出,退出程序
点击继续表白按钮实现的效果如下图
点击重新表白的效果如下图
三、提供以下代码仅供参考
package layouttest;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class biaobaiFrame extends JFrame {
private JTextField biaobaiobject;
private JTextField words;
private JRadioButton[] willings;
private JButton btn1,btn2,btn3;
private ButtonGroup bg1;
public biaobaiFrame() {
this.setTitle("表白窗口");//设置标题
this.setSize(340, 220);//设置宽和高
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置关闭时退出
this.setLayout(null);//设置为绝对布局
JLabel lb1=new JLabel("表 白 对 象:");//定义标签对象
lb1.setBounds(30, 10, 120, 30);//设置标签距离布局
this.add(lb1);//实现标签作用
biaobaiobject=new JTextField();//标签文本框
biaobaiobject.setBounds(160, 10, 140, 25);//标签文本框长度
this.add(biaobaiobject);
JLabel lb2=new JLabel("说一句你想表白的话:");
lb2.setBounds(20, 55, 140, 30);
this.add(lb2);
words=new JTextField();
words.setBounds(160, 55, 140, 25);
this.add(words);
JLabel lb3=new JLabel("是否愿意余生与ta一起过:");
lb3.setBounds(3, 100, 150, 30);
this.add(lb3);
String[] yi= {"愿意","不愿意"};
willings=new JRadioButton[yi.length];//设置单选框组件
bg1=new ButtonGroup();//设置按钮组
for(int i=0;i<yi.length;i++) {
willings[i]=new JRadioButton(yi[i]);
willings[i].setBounds(160+i*70, 100, 70, 30);
bg1.add(willings[i]);
this.add(willings[i]);
}
btn1=new JButton("重新表白");//设置按钮
btn1.setBounds(30, 145, 100, 36);//设置按钮位置
this.add(btn1);//实现按钮作用
btn2=new JButton("继续表白");
btn2.setBounds(135, 145, 100, 36);
this.add(btn2);
btn3=new JButton("退出");
btn3.setBounds(240, 145, 60, 36);
this.add(btn3);
ClickListener listener=new ClickListener();//配置监听器实现按钮动作
btn2.addActionListener(listener);
btn3.addActionListener(listener);
btn1.addActionListener(listener);
this.setResizable(false);//设置窗口大小不可调
this.setLocationRelativeTo(null);//居中显示
this.setVisible(true);//显示窗口
}
class ClickListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent event) {
String ob=biaobaiobject.getText();
String word=words.getText();
if(event.getActionCommand().equals("继续表白")) {
String will=null;
for(int i=0;i<willings.length;i++) {
if(willings[i].isSelected()) {
will=willings[i].getActionCommand();
break;
}
}
try {
insert(ob,word,will);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(event.getActionCommand().equals("重新表白")) {
if(!(ob.trim().equals(word.trim()))) {
biaobaiobject.setText("");
words.setText("");
return;
}
}
if(event.getActionCommand().equals("退出")) {
System.exit(0);
}
}
public void insert(Object...params) throws Exception{
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/biaobai?useUnicode=true&characterEncoding=UTF-8";
String user="root";
String password="123456";
Connection conn=DriverManager.getConnection(url, user, password);
String sql="insert into object values(?,?,?)";
PreparedStatement stmt=conn.prepareStatement(sql);
for(int i=0;i<params.length;i++){
stmt.setObject(i+1,params[i]);
}
int i=stmt.executeUpdate();
stmt.close();
conn.close();
}
}
public static void main(String[] args) {
new biaobaiFrame();
}
}