问题描述
我如何不同的ActionListeners添加到P1的对象。我希望程序能够在textbar设置为适当的数量时$ P $与相应的按钮pssed。因为它们不是不同的变量,我不能简单地用code以下(在我的actionPerformed功能),
如果(e.getSource()==按钮1){
txtField.setText(1);
}
进口java.awt中的*。
导入的java.applet。*;
java.awt.event中导入*。公共类电话扩展小程序实现的ActionListener
{
文本字段txtField; 公共无效的init(){
的setLayout(新的BorderLayout()); txtField =新的TextField();
加(txtField,BorderLayout.NORTH); 面板P1 =新面板();
p1.setLayout(新的GridLayout(4,3));
p1.add(新按钮(1));
p1.add(新按钮(2));
p1.add(新按钮(3));
p1.add(新按钮(4));
p1.add(新按钮(5));
p1.add(新按钮(6));
p1.add(新按钮(7));
p1.add(新按钮(8));
p1.add(新按钮(9));
p1.add(新按钮(*));
p1.add(新键(0));
p1.add(新按钮(#));
加(P1,BorderLayout.CENTER); } 公共无效的actionPerformed(ActionEvent的五){
}
}
没有你的GUI类也是监听器类作为的要求类的事太多了。相反,考虑使用匿名内部监听类或私有内部类。顺便说一句,我没有看到你添加任何听众的按钮。此外,对于我的钱,我想创建一个Swing GUI的,而不是一个AWT GUI的Swing是一个更强大和灵活的。
另外请注意,上面的例子中,我会在实际上给我所有的按钮对象相同的动作侦听器。如果使用摇摆,我可以简单地让这将是数息字符串ActionEvent对象的actionCommand。无需10 if块或交换机模块。
例如,这表明在显示一个JTextField的一些非常简单的逻辑,但没有计算逻辑:
进口java.awt.BorderLayout中;
进口java.awt.GridLayout中;
进口java.awt.event.ActionEvent中;进口的javax.swing *。公共类CalcEg {
私有静态最终浮动BTN_FONT_SIZE = 18F;
私有静态最后的String [] [] = BTN_LABELS {
{7,8,9, - },
{4,5,6,+},
{1,2,3,/},
{0,。,=,*}
};
私人的JPanel的mainPanel =新JPanel();
私人JTextField的文本框=新的JTextField(10); 公共CalcEg(){
诠释行= BTN_LABELS.length;
INT COLS = BTN_LABELS [0]。长度;
INT间隙= 4; JPanel的buttonPanel =新JPanel(新网格布局(行,COLS,缺口,缺口));
对于(字符串[] btnLabelRow:BTN_LABELS){
对于(字符串btnLabel:btnLabelRow){
JButton的BTN = createButton(btnLabel);
如果(0123456789。含有(btnLabel)){
btn.setAction(新NumberListener(btnLabel));
}
buttonPanel.add(BTN);
}
} textField.setFont(textField.getFont()的deriveFont(BTN_FONT_SIZE)); mainPanel.setLayout(新的BorderLayout(间隙,间隙));
mainPanel.setBorder(BorderFactory.createEmptyBorder(缺口,缺口,缺口,缺口));
mainPanel.add(文本框,BorderLayout.PAGE_START);
mainPanel.add(buttonPanel,BorderLayout.CENTER);
} 私人的JButton createButton(字符串btnLabel){
JButton的按钮=的新的JButton(btnLabel);
button.setFont(button.getFont()的deriveFont(BTN_FONT_SIZE));
返回按钮;
} 公共JComponent的getMainComponent(){
返回mainPanel中;
} 私有类NumberListener扩展AbstractAction {
NumberListener(字符串actionCommand){
超(actionCommand);
} @覆盖
公共无效的actionPerformed(ActionEvent的五){
串actionCommand = e.getActionCommand();
textField.setText(textField.getText()+ actionCommand);
}
} 私有静态无效createAndShowGui(){
CalcEg的mainPanel =新CalcEg(); JFrame的帧=新的JFrame(CalcEg);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
。frame.getContentPane()加(mainPanel.getMainComponent());
frame.pack();
frame.setLocationByPlatform(真);
frame.setVisible(真);
} 公共静态无效的主要(字串[] args){
SwingUtilities.invokeLater(Runnable的新(){
公共无效的run(){
createAndShowGui();
}
});
}
}
How do I add different actionlisteners to the p1 objects. I want the program to be able to set the textbar to the appropriate number when pressed with the appropriate button. Since they are not different variables I cannot simply use the code below(in my actionPerformed function),
if (e.getSource() == button1){ txtField.setText("1"); }
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class Telephone extends Applet implements ActionListener
{
TextField txtField;
public void init() {
setLayout(new BorderLayout());
txtField = new TextField("");
add(txtField, BorderLayout.NORTH);
Panel p1 = new Panel();
p1.setLayout(new GridLayout(4, 3));
p1.add(new Button("1"));
p1.add(new Button("2"));
p1.add(new Button("3"));
p1.add(new Button("4"));
p1.add(new Button("5"));
p1.add(new Button("6"));
p1.add(new Button("7"));
p1.add(new Button("8"));
p1.add(new Button("9"));
p1.add(new Button("*"));
p1.add(new Button("0"));
p1.add(new Button("#"));
add(p1, BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent e) {
}
}
Don't have your GUI class also be your listener class as that's asking the class to do too much. Instead consider using anonymous inner listener classes or private inner classes. Incidentally, I don't see where you're adding any listeners to your buttons. Also, for my money, I'd create a Swing GUI, not an AWT GUI as Swing is much more robust and flexible.
Also note, for the example above, I would in fact give all my button objects the same action listener. If using Swing, I could simply get the ActionEvent object's actionCommand which would be the number String of interest. No need for 10 if blocks or a switch block.
For example, this demonstrates very simple logic of displaying the number in a JTextField, but has no calculation logic:
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import javax.swing.*;
public class CalcEg {
private static final float BTN_FONT_SIZE = 18f;
private static final String[][] BTN_LABELS = {
{"7", "8", "9", "-"},
{"4", "5", "6", "+"},
{"1", "2", "3", "/"},
{"0", ".", "=", "*"}
};
private JPanel mainPanel = new JPanel();
private JTextField textField = new JTextField(10);
public CalcEg() {
int rows = BTN_LABELS.length;
int cols = BTN_LABELS[0].length;
int gap = 4;
JPanel buttonPanel = new JPanel(new GridLayout(rows, cols, gap, gap));
for (String[] btnLabelRow : BTN_LABELS) {
for (String btnLabel : btnLabelRow) {
JButton btn = createButton(btnLabel);
if ("0123456789.".contains(btnLabel)) {
btn.setAction(new NumberListener(btnLabel));
}
buttonPanel.add(btn);
}
}
textField.setFont(textField.getFont().deriveFont(BTN_FONT_SIZE));
mainPanel.setLayout(new BorderLayout(gap, gap));
mainPanel.setBorder(BorderFactory.createEmptyBorder(gap, gap, gap, gap));
mainPanel.add(textField, BorderLayout.PAGE_START);
mainPanel.add(buttonPanel, BorderLayout.CENTER);
}
private JButton createButton(String btnLabel) {
JButton button = new JButton(btnLabel);
button.setFont(button.getFont().deriveFont(BTN_FONT_SIZE));
return button;
}
public JComponent getMainComponent() {
return mainPanel;
}
private class NumberListener extends AbstractAction {
NumberListener(String actionCommand) {
super(actionCommand);
}
@Override
public void actionPerformed(ActionEvent e) {
String actionCommand = e.getActionCommand();
textField.setText(textField.getText() + actionCommand);
}
}
private static void createAndShowGui() {
CalcEg mainPanel = new CalcEg();
JFrame frame = new JFrame("CalcEg");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel.getMainComponent());
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
这篇关于如何不同的ActionListener添加到相同的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!