Java GUI键盘模拟器
我正在开发这个键盘模拟器,但是我对GUI比较陌生,我一直在尝试添加ActionListener来执行按钮的功能,这意味着每当按下a按钮时,我希望字母出现在输入区域。 提前谢谢Java GUI键盘模拟器,java,swing,user-interface,keyboard,simulator,Java,Swing,User Interface,Keyboard,Simulator,我正在开发这个键盘模拟器,但是我对GUI比较陌生,我一直在尝试添加ActionListener来执行按钮的功能,这意味着每当按下a按钮时,我希望字母出现在输入区域。 提前谢谢 import java.awt.BorderLayout; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; im
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class StockTicker extends JFrame implements ActionListener
{
String firstRow[] = {"1","2","3","4","5","6","7","8","9","0"};
String secondRow[] = {"Q","W","E","R","T","Y","U","I","O","P","Del"};
String thirdRow[] = {"A","S","D","F","G","H","J","K","L","Return"};
String fourthRow[] = {"Z","X","C","V","B","N","M","."};
JButton first[];
JButton second[];
JButton third[];
JButton fourth[];
public StockTicker()
{
super ("A Simple Stock Market GUI");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
initWidgets();
}
private void initWidgets()
{
JLabel jlOutput = new JLabel("Output: ");
JLabel jlInput = new JLabel("Intput: ");
final JLabel jtfOutput = new JLabel("");
final JLabel jtfInput = new JLabel();
JPanel bottomPanel = new JPanel();
bottomPanel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 1;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 1.0;
gbc.weighty = 0.1;
gbc.anchor = GridBagConstraints.PAGE_START;
JLabel bottomLabel = new JLabel(" Input : ", JLabel.LEFT);
setLayout(new BorderLayout());
JPanel jpNorth = new JPanel();
JPanel jpCenter = new JPanel();
JPanel jpKeyboard = new JPanel();
add( jpNorth, BorderLayout.NORTH);
add( jpCenter, BorderLayout.CENTER);
add(jpKeyboard, BorderLayout.SOUTH);
jpNorth.setLayout(new BorderLayout());
jpNorth.add(jlOutput, BorderLayout.WEST);
jpNorth.add(jtfOutput, BorderLayout.SOUTH);
jpCenter.setLayout( new BorderLayout());
jpCenter.add(jlInput, BorderLayout.WEST);
jpCenter.add(jtfInput, BorderLayout.CENTER);
jpKeyboard.setLayout(new GridLayout(4,1));
pack();
first = new JButton[firstRow.length];
JPanel p = new JPanel(new GridLayout(1, firstRow.length));
for(int i = 0; i < firstRow.length; ++i)
{
first[i] = new JButton(firstRow[i]);
p.add(first[i]);
}
jpKeyboard.add(p);
second = new JButton[secondRow.length];
p = new JPanel(new GridLayout(1, secondRow.length));
for(int i = 0; i < secondRow.length; ++i)
{
second[i] = new JButton(secondRow[i]);
p.add(second[i]);
}
jpKeyboard.add(p);
third = new JButton[thirdRow.length];
p = new JPanel(new GridLayout(1, thirdRow.length));
for(int i = 0; i < thirdRow.length; ++i)
{
third[i] = new JButton(thirdRow[i]);
p.add(third[i]);
}
jpKeyboard.add(p);
fourth = new JButton[fourthRow.length];
p = new JPanel(new GridLayout(1, fourthRow.length));
for(int i = 0; i < fourthRow.length; ++i)
{
fourth[i] = new JButton(fourthRow[i]);
p.add(fourth[i]);
}
jpKeyboard.add(p);
}
导入java.awt.BorderLayout;
导入java.awt.GridBagConstraints;
导入java.awt.GridBagLayout;
导入java.awt.GridLayout;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入javax.swing.JButton;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
导入javax.swing.JTextArea;
公共类StockTicker扩展JFrame实现ActionListener
{
字符串第一行[]={“1”、“2”、“3”、“4”、“5”、“6”、“7”、“8”、“9”、“0”};
字符串第二行[]={“Q”、“W”、“E”、“R”、“T”、“Y”、“U”、“I”、“O”、“P”、“Del”};
字符串thirdRow[]={“A”、“S”、“D”、“F”、“G”、“H”、“J”、“K”、“L”、“Return”};
字符串四掷[]={“Z”、“X”、“C”、“V”、“B”、“N”、“M”、“M”};
JButton first[];
JButton第二[];
JButton第三[];
杰布顿第四[];
股票行情
{
超级(“一个简单的股票市场图形用户界面”);
此.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
此参数为.setresizeable(false);
initWidgets();
}
私有void initWidgets()
{
JLabel jlOutput=新的JLabel(“输出:”);
JLabel jlInput=新的JLabel(“输入:”);
最终JLabel jtfOutput=新JLabel(“”);
最终JLabel jtfInput=新JLabel();
JPanel bottomPanel=新的JPanel();
setLayout(新的GridBagLayout());
GridBagConstraints gbc=新的GridBagConstraints();
gbc.gridx=1;
gbc.gridy=0;
gbc.fill=GridBagConstraints.HORIZONTAL;
gbc.weightx=1.0;
gbc.weighty=0.1;
gbc.anchor=gridbagsconstraints.PAGE_START;
JLabel bottomLabel=新的JLabel(“输入:”,JLabel.LEFT);
setLayout(新的BorderLayout());
JPanel jpNorth=新的JPanel();
JPanel jpCenter=新的JPanel();
JPanel jpKeyboard=新的JPanel();
添加(jpNorth,BorderLayout.NORTH);
添加(jpCenter,BorderLayout.CENTER);
添加(jpKeyboard,BorderLayout.SOUTH);
jpNorth.setLayout(新的BorderLayout());
add(jlOutput,BorderLayout.WEST);
jpNorth.add(jtfOutput,BorderLayout.SOUTH);
jpCenter.setLayout(新的BorderLayout());
jpCenter.add(jlInput,BorderLayout.WEST);
添加(jtfInput,BorderLayout.CENTER);
设置布局(新的网格布局(4,1));
包装();
first=新的JButton[firstRow.length];
JPanel p=newjpanel(新的GridLayout(1,firstRow.length));
对于(int i=0;i
JButton.addActionListener(listener);
所以你会这样做
MyActionListener listener = new MyActionListener();
.
.
.
first[i] = new JButton(firstRow[i]);
first[i].setActionCommand(firstRow[i]);
first[i].addActionListener(listener);
.
.
.
public class MyActionListener implements ActionListener {
public void actionPerformed(ActionEvent evt) {
String cmd = evt.getActionCommand();
// append the cmd value to your out put...
}
}
你有什么问题吗?我不知道如何将actionlistener添加到按钮并使其实际工作。