Re: [J2SE] GUI ActionEvent 設定問題
※ 引述《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
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 3 之 3 篇):