58,454
社区成员
发帖
与我相关
我的任务
分享
public void actionPerformed(ActionEvent ae){
switch(ae.getActionCommand().charAt(0)){
case '1':
tablenameUI getTablename =tablenameUI.getTablenameUI();
Thread getTablenameThread = new Thread(getTablename);
getTablenameThread.start();
System.out.println("get table的UI线程启动了。");
synchronized (getTablename){
try {
System.out.println("我要wait了哦!");
getTablename.wait();
System.out.println("Oops,被唤醒了");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
btnOk = new JButton("ok");
btnOk.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tablename = textField.getText();
if (tablename == "")
JOptionPane.showMessageDialog(getContentPane(),"Please enter the name!");
else{
commandFrame.tablename = tablename;
System.out.println("There's something written in the textfield");
synchronized (this){
if (tablename != "**"){
notifyAll();
System.out.println("I have unlocked the lock.");
}
}
}
}
});
public void run() {
System.out.println("启动run method");
setVisible(true);
}
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.Font;
import java.awt.Toolkit;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class tablenameUI extends JFrame implements Runnable{
commandUI commandFrame = commandUI.getCommandUI();
static tablenameUI tablenameFrame= null;
public String tablename = "**";
private JLabel lblNewLabel;
private JLabel lblTheTable;
private JTextField textField;
private JButton btnOk;
private JButton btnCancle;
public static tablenameUI getTablenameUI(){
if (tablenameFrame == null)
tablenameFrame = new tablenameUI();
return tablenameFrame;
}
private tablenameUI() {
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setTitle("Table Name");
setLocation((int)Toolkit.getDefaultToolkit().getScreenSize().getWidth()/3,
(int)Toolkit.getDefaultToolkit().getScreenSize().getHeight()/4);
setSize(410 , 300);
getContentPane().setLayout(null);
lblNewLabel = new JLabel("Please enter the name of");
lblNewLabel.setFont(new Font("Comic Sans MS", Font.PLAIN, 25));
lblNewLabel.setBounds(42, 28, 310, 61);
getContentPane().add(lblNewLabel);
System.out.println("加载了一个“姓名”标签");
lblTheTable = new JLabel("the table:");
lblTheTable.setFont(new Font("Comic Sans MS", Font.PLAIN, 25));
lblTheTable.setBounds(42, 92, 130, 61);
getContentPane().add(lblTheTable);
System.out.println("加载了一个“the table”标签");
textField = new JTextField();
textField.setFont(new Font("Comic Sans MS", Font.PLAIN, 30));
textField.setBounds(191, 102, 130, 40);
getContentPane().add(textField);
textField.setColumns(10);
System.out.println("加载了一个“textField”");
btnOk = new JButton("ok");
btnOk.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tablename = textField.getText();
if (tablename == "")
JOptionPane.showMessageDialog(getContentPane(),"Please enter the name!");
else{
commandFrame.tablename = tablename;
System.out.println("There's something written in the textfield");
synchronized (this){
if (tablename != "**"){
notifyAll();
System.out.println("I have unlocked the lock.");
}
}
}
}
});
btnOk.setFont(new Font("Comic Sans MS", Font.PLAIN, 18));
btnOk.setBounds(49, 178, 112, 44);
getContentPane().add(btnOk);
System.out.println("加载了一个“ok”buttuon");
btnCancle = new JButton("cancel");
btnCancle.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
textField.setText("");
}
});
btnCancle.setFont(new Font("Comic Sans MS", Font.PLAIN, 18));
btnCancle.setBounds(229, 178, 112, 44);
getContentPane().add(btnCancle);
System.out.println("加载了一个“cancel”button");
//while closing this frame, set the main frame into visible
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
super.windowClosing(e);
commandUI.getCommandUI().setVisible(true);
}
});
System.out.println("全部加载完毕!");
}
@Override
public void run() {
System.out.println("启动run method");
setVisible(true);
}
}
package modelWindow;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class MainFrame extends JFrame {
private String strTableName;
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new MainFrame().setVisible(true);
}
public MainFrame(){
this.setSize(400,300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(null);
JButton btn = new JButton("输入表名");
btn.setSize(100, 30);
btn.setLocation(10, 10);
this.add(btn);
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
MainFrame.this.setEnabled(false);
new InputFrame(MainFrame.this).setVisible(true);
}
});
}
public void setTableName(String tablename){
this.setEnabled(true);
strTableName = tablename;
System.out.println("用户输入的表名是:"+strTableName);
}
}
class InputFrame extends JFrame implements WindowListener{
//父窗口的引用
private MainFrame main;
private TextField tf = new TextField();
public InputFrame(MainFrame main){
//输入窗口显示状态设置
this.main = main;
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(215, 120);
this.setResizable(false);
this.setLayout(null);
//输入框
tf.setEditable(true);
tf.setLocation(10, 10);
tf.setSize(186, 35);
this.add(tf);
//确定按钮
JButton btn = new JButton("确认输入");
btn.setSize(100, 30);
btn.setLocation(50, 55);
this.add(btn);
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(!tf.getText().equals("")){
InputFrame.this.dispose();
}
}
});
//监听窗口事件
this.addWindowListener(this);
}
@Override
public void windowClosed(WindowEvent e) {
//将输入传回父窗口
main.setTableName(tf.getText());
}
@Override
public void windowClosing(WindowEvent e) {
}
@Override
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub
}
}