0

我正在尝试创建一个屏幕上的 Telepad,人们可以在其中按下键盘按钮,它会出现在文本框中我还没有为键盘制作 ActionListner,但我希望它显示在视图中......这是键盘面板和视图的代码还有一个持续时间计时器,我已经设法开始工作,但无法将它们放入一个视图中

这是键盘面板

import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JPanel;

public class KeypadPanel extends JPanel {

private JButton noStar;
private JButton noHash;
private JButton[] buttons;
private JButton C;
private JButton add;
private JPanel keypadPanel;

public KeypadPanel(TelepadController controller)  {

    buttons = new JButton[10];
    for (int i = 0; i < buttons.length; i++) {
        buttons[i] = new JButton("" + i);
      //  buttons[i].addActionListener(controller.new NumberButtonListener());
    }

    //noStar.addActionListener(controller.new noStarActionListener);
    //noHash.addActionListener(controller.new noHashActionListener);
    //C.addActionListener(controller.new CActionListener);
    //add.addActionListener(controller.new addActionListener);


    noStar = new JButton("*");
    noHash = new JButton("#");
    C = new JButton("C");
    add = new JButton("+");


    JPanel keypadPanel = new JPanel();
    keypadPanel.setLayout(new GridLayout(4, 3));
    for (int i = 1; i <= 9; i++) {
         keypadPanel.add(buttons[i]);
            add(noStar);
    add(noHash);
  add(C);
 add(add);


    }
}

}

这是主视图的代码

package londontelepad2;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.util.Observable;
import java.util.Observer;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.plaf.ColorUIResource;
import javax.swing.plaf.FontUIResource;
import org.apache.commons.beanutils.BeanUtils;


public class TelepadView implements Observer {

private StopwatchPanel stopwatchPanel;
private KeypadPanel keypadPanel;
private JFrame frame;

/**
 *
 * @param controller
 */
public TelepadView(TelepadController controller) {

    super();
    this.setResources();

    frame = new JFrame("London Telepad");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    stopwatchPanel = new StopwatchPanel(controller);
    //stopwatchPanel = new StopwatchPanel2(controller);
    keypadPanel = new KeypadPanel(controller);

    frame.getContentPane().add(stopwatchPanel);
    frame.getContentPane().add(keypadPanel);
    frame.pack();
}

public void show() {
    frame.setVisible(true);
}

@Override
public void update(Observable observable, Object arg) {
    if (arg.equals(Properties.TIME)) {
        try {
            stopwatchPanel.setTime(BeanUtils.getProperty(observable,
                    Properties.TIME));
        } catch (Exception e) {
            System.out.println(e);
        }

    }
}

public void setResetState() {
    stopwatchPanel.setButtons(true, false, false);
}

public void setStoppedState() {
    stopwatchPanel.setButtons(false, false, true);

}

public void setRunningState() {
    stopwatchPanel.setButtons(false, true, false);
}

private void setResources() {

    ColorUIResource defaultBackground = new ColorUIResource(Color.white);
    ColorUIResource defaultForeground = new ColorUIResource(Color.black);
    ColorUIResource disabledColor = new ColorUIResource(Color.lightGray);

    FontUIResource smallFont = new FontUIResource(
            new Font("Dialog", Font.BOLD, 12));
    FontUIResource bigFont = new FontUIResource(
            new Font("Dialog", Font.BOLD, 14));

    UIManager.put("Button.background",
            defaultBackground);
    UIManager.put("Button.foreground",
            defaultForeground);
    UIManager.put("Button.disabledText",
            disabledColor);
    UIManager.put("Button.font", smallFont);


    UIManager.put("Label.background",
            defaultBackground);
    UIManager.put("Label.foreground",
            defaultForeground);
    UIManager.put("Label.font", bigFont);

    UIManager.put("Panel.background",
            defaultBackground);
    UIManager.put("Panel.foreground",
            defaultForeground);
}
}

如果我单独执行 .add,我会收到一个错误(实际参数列表和形式参数列表的长度不同)

如果我一起做,我会得到 java.lang.IllegalArgumentException: cannot add to layout: constraint must be a string (or null)

而且我找不到我做错了什么!

看到我是 java 的优步菜鸟,世界上所有的帮助将不胜感激!

谢谢

比拉尔

更新

这是我将它们放在同一个 .add 字段中时收到的错误日志

Exception in thread "main" java.lang.NullPointerException
at londontelepad2.KeypadPanel.<init>(KeypadPanel.java:48)
at londontelepad2.TelepadView.<init>(TelepadView.java:46)
at londontelepad2.TelepadController.<init>(TelepadController.java:33)
at londontelepad2.LondonTelepad2.main(LondonTelepad2.java:19)
Java Result: 1
4

1 回答 1

3

等等,KeypadPanel 只是一个普通的对象。为什么它不扩展JPanel?

您正在调用add()JFrame 的 -method 将组件添加到框架中吗?你需要打电话

frame.getContentPane().add(comp);
于 2013-05-13T11:36:34.197 回答