p1.add(password = new JTextField(""));
contentPane.add("Center",p1);------------------------------------
p1.add(password=new JPasswordField("应该是密码框组件"))
contentPane.add("Center",p1);------------------------------------
p1.add(password=new JPasswordField("应该是密码框组件"))
解决方案 »
- 关于java当中string用法的一个小疑问
- 怎么监听组合按键
- 读文件的错误。求救
- Calendar是从哪里来的啊?
- 小程序在浏览器运行时的问题???
- 编程显示一棵树时,如何让一开始树的所有子节点都展开呢?
- 高手大虾们,在"swing"中如何给菜单项加如快捷键如ctrl v ---先谢了
- 一个初学者的问题。
- 请问大家有没有把数字转成英文的class,如¥23.45 译成twenty three and fourty five cents.谢谢!
- 请问java内部类有何用?很重要吗?thinking in java 看的我如入云雾,实在头大~
- 请教:在JB6中怎么样连接数据库!!
- JTextPane的问题 ?
DataExchangeTest.java:51: '}' expected.
}
^
DataExchangeTest.java:53: Statement expected.
public static void main(String [] args){
^
2 errors
错误很多:现为如下,可运行!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class DataExchangeTest extends JFrame implements ActionListener{
private ConnectDialog dialog = null;
private JMenuItem connectItem;
private JMenuItem exitItem;
public static void main(String[] args){
JFrame f=new DataExchangeTest();
f.show();
}
public DataExchangeTest(){
setTitle("DataExchangeTest");
setSize(200,200);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
JMenuBar mbar = new JMenuBar();
setJMenuBar(mbar);
JMenu fileMenu = new JMenu("File");
mbar.add(fileMenu);
connectItem = new JMenuItem("Connect");
connectItem.addActionListener(this);//
fileMenu.add(connectItem);
exitItem = new JMenuItem("Exit");
exitItem.addActionListener(this);
fileMenu.add(exitItem);
}
public void actionPerformed(ActionEvent evt){
Object source = evt.getSource();
if(source == connectItem){
ConnectInfo transfer = new ConnectInfo("yourname","pw");
if(dialog == null){
dialog = new ConnectDialog(this);
if(dialog.showDialog(transfer)){
String uname = transfer.username;
String pwd = transfer.password;
Container contentPane = getContentPane();
contentPane.add(new JLabel("username="+uname+",password="+pwd),"South");
validate();
}
}else if(source == exitItem)
System.exit(0);
}}class ConnectInfo{
public String username;
public String password;
public ConnectInfo(String u,String p){
username = u;
password = p;
}
}class ConnectDialog extends JDialog implements ActionListener{
private JTextField username;
private JPasswordField password;
private boolean ok;
private JButton okButton;
private JButton cancelButton;
public ConnectDialog(JFrame parent){
super(parent,"Connect",true);
Container contentPane = getContentPane();
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(2,2));
p1.add(new JLabel("User name:"));
p1.add(username = new JTextField(""));
p1.add(new JLabel("Password:"));
p1.add(password = new JPasswordField(""));
contentPane.add("Center",p1);
Panel p2 = new Panel();
okButton = addButton(p2,"OK");
cancelButton = addButton(p2,"Cancel");
contentPane.add("South",p2);
setSize(240,120);
}
private JButton addButton(Container c, String name){
JButton button = new JButton(name);
button.addActionListener(this);
c.add(button);
return button;
}
public void actionPerformed(ActionEvent evt){
Object source = evt.getSource();
if(source == okButton){
ok = true;
setVisible(false);
}else if(source==cancelButton){
setVisible(false);
}
}
public boolean showDialog(ConnectInfo transfer){
username.setText(transfer.username);
password.setText(transfer.password);
ok = false;
show();
if(ok){
transfer.username = username.getText();
transfer.password = new String(password.getPassword());
}
return ok;
}
}
}