[英]Adding text to JTextArea box when a button is clicked?
好的,所以我試圖弄清楚如何在GUI中單擊某個按鈕時將文本添加到TextArea中。 對於此當前問題,當我單擊“重置”按鈕時,我希望它顯示幾行我已經預先寫好的文本。 我不確定如何將其實現到我的applet中。
這是按鈕的代碼
public class DisplayButtonPanel2 extends JPanel{
//create buttons for reseting back to original data, displaying data and sorting data
JButton resebutton = new JButton("RESET");
JButton displaybutton = new JButton("DISPLAY");
JButton sortbutton = new JButton("SORT");
//constructor
public DisplayButtonPanel2()
{
//create action listener object
BListen listener = new BListen();
//add action listen functionalty to buttons
resebutton.addActionListener(listener);
displaybutton.addActionListener(listener);
sortbutton.addActionListener(listener);
//add buttons to panel
add(resebutton);
add(displaybutton);
add(sortbutton);
}
public class BListen implements ActionListener
{
public void actionPerformed(ActionEvent event){
if (event.getSource() == resebutton){
JOptionPane.showMessageDialog(null,"reset Button Clicked");
DisplayEmployeePanel displayemply = new DisplayEmployeePanel();
displayemply.resetText();
}
else if(event.getSource() == displaybutton){
JOptionPane.showMessageDialog(null,"display Button Clicked");
// DisplayEmployeePanel displayemply = new DisplayEmployeePanel();
}
else if(event.getSource() == sortbutton){
JOptionPane.showMessageDialog(null,"sort Button Clicked");
}
}
}
textArea的代碼
public class DisplayEmployeePanel extends JPanel{
//create a text area that will display employees
JTextArea employeetextarea = new JTextArea("~Company Employee's~", 20 ,30); //textinarea, hieght, width
//constructor
public DisplayEmployeePanel()
{
employeetextarea.setEditable(false); //will not be able to edit text in area
String ns = "\nDisplay Employee Info BELOW";
employeetextarea.append(ns);
//add text area to panel
add(employeetextarea);
}
public void resetText()
{
String sn = "\nreset to original emmployee list";
employeetextarea.append(sn);
add(employeetextarea);
System.out.println("reset text");///testing purposes
}
}//end class
EDIT *添加更多源代碼(如果有幫助的話),此代碼可用於創建框架
public class CreateCompanyGUI extends JFrame{
//constructor
public CreateCompanyGUI()
{
super("Company Applet!"); //tittle
setSize(800,800); //set size *careful when using pack*
setLocationRelativeTo(null); //places frame in middle of screen by default
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //how to close
setLayout(new BorderLayout()); //set BorderLayout manager for this frame
//create panels
DisplayEmployeePanel dep = new DisplayEmployeePanel();
// DisplayEmployeePanel2 dep2 = new DisplayEmployeePanel2();
DisplayButtonPanel dbp = new DisplayButtonPanel();
DisplayButtonPanel2 dbp2 = new DisplayButtonPanel2();
// DisplayListPanel dlp = new DisplayListPanel();
//add panels to frame
add(dep, BorderLayout.NORTH);
add(dbp2, BorderLayout.CENTER);
add(dbp, BorderLayout.SOUTH);
// add(dlp, BorderLayout.EAST);
// add(dep2, BorderLayout.CENTER);
//pack (packs the window and creates appporiate size
pack();
//visible = TRUE (so we can see the frame)
setVisible(true);
}
}//end class
用於創建GUI的代碼
public class CompanyApplet extends JApplet{
public static void main(String[] args)
{
//create GUI for company applet
new CreateCompanyGUI();
}
}
DisplayEmployeePanel displayemply = new DisplayEmployeePanel();
displayemply.resetText();
您無法繼續創建新的DisplayEmployeePanel。 您要更新在GUI上可見的現有面板的文本區域。 這意味着DisplayButtonPanel2需要對DisplayEmployeePanel的引用。
因此,在創建兩個面板並將其添加到框架的代碼中,需要將DisplayEmployeedPanel作為參數傳遞給DisplayButtonPanel2。
或更簡單的解決方案是只使用一個類來創建兩個面板。 然后,該類中的所有方法都可以訪問該類中定義的所有組件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.