作者showbizz (永保安康)
看板java
标题Re: [J2SE] GUI ActionEvent 设定问题
时间Tue Jun 27 15:06:49 2006
※ 引述《thirddawn (白夜)》之铭言:
: 因为是新手上路,所以问的问题说不定很白痴请大家多多包含
: 目前在写一段GUI的东西
: 在一个Frame里面要有两个Label(左边一个小的,右边一个大的)
: 是希望能够在作出像是网页的那种
: 左边点了按钮之後右边可以跑出对应的Label页
: 可是我在左边按钮的event Listener里写了要整个container去add右边的Label
: 执行起来时却没有反应Orz...
: 请问一下是不能这样写吗?还是说要怎麽样才可以多次更新右边那块Label
: 而左边都可以保持不动呢?
: 谢谢
你试试看这样可不可以!?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class AdministratorDisplay extends JFrame{
private JPanel rightpanel;
private JPanel leftpanel;
private JButton addaccount, deleteaccount, changepassword;
public Container pane = getContentPane();
public AdministratorDisplay(){
super ( "AdministrastorDisplay" );
rightpanel = new JPanel();
deleteaccount = new JButton("Delete Account");
addaccount = new JButton("Add account");
changepassword = new JButton("Change Password");
leftpanel = new JPanel();
leftpanel.setLayout(new GridLayout(3,1));
leftpanel.add(addaccount);
leftpanel.add(deleteaccount);
leftpanel.add(changepassword);
JSplitPane jsplit = new
JSplitPane(JSplitPane.HORIZONTAL_SPLIT,leftpanel,rightpanel);
pane.add(jsplit, BorderLayout.CENTER);
addaccount.addActionListener(
new ActionListener(){
public void actionPerformed( ActionEvent e ){
if(e.getSource() == addaccount)
{
rightpanel.removeAll();
rightpanel.updateUI();
String names[] = {"Administrator", "Professor", "Student"};
JTextField account = new JTextField(20);
JTextField password = new JTextField(20);
JButton ok = new JButton("OK");
GridLayout show = new GridLayout(7,1);
JLabel first = new JLabel("Please enter your new account name");
JLabel second = new JLabel("Please enter your new password");
JLabel third = new JLabel("Please Choose your type");
JComboBox type = new JComboBox(names);
rightpanel.setLayout(show);
rightpanel.add(first);
rightpanel.add(account);
rightpanel.add(second);
rightpanel.add(password);
rightpanel.add(third);
rightpanel.add(type);
rightpanel.add(ok);
}
}
}
);
deleteaccount.addActionListener(
new ActionListener(){
public void actionPerformed( ActionEvent Event ){
rightpanel.removeAll();
rightpanel.updateUI();
JTextField account = new JTextField(20);
JButton ok = new JButton("OK");
JLabel first = new JLabel("Please enter the account name which you want
to delete");
rightpanel.setLayout(new FlowLayout());
rightpanel.add(first);
rightpanel.add(account);
rightpanel.add(ok);
}
}
);
changepassword.addActionListener(
new ActionListener(){
public void actionPerformed( ActionEvent Event ){
rightpanel.removeAll();
rightpanel.updateUI();
JTextField account = new JTextField(20);
JTextField password = new JTextField(20);
JButton ok = new JButton("OK");
JLabel first = new JLabel("Please enter the account name which you want
to change password");
JLabel second = new JLabel("Please enter your new password");
rightpanel.setLayout(new FlowLayout());
rightpanel.add(first);
rightpanel.add(account);
rightpanel.add(second);
rightpanel.add(password);
rightpanel.add(ok);
}
}
);
setSize(640,480);
setVisible( true );
}
public static void main(String args[]){
AdministratorDisplay hi = new AdministratorDisplay();
hi.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.117.156.148