我到目前为止的代码如下:
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JPanel;
import java.awt.GridLayout;
import java.awt.FlowLayout;
public class GUI extends JFrame
{
private JButton button;
private JPanel panel;
private static final String[][] key = { { "Esc", "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", "F11", "F12", "PrtSc", "Insert", "Delete", "Home", "End", "PgUp", "PgDn"}, { "3\n2", "&", "é", "\"", "'", "(", "§", "è", "!", "ç", "à", ")", "-", "BkSpc", "NumLock", "/", "*", "-" }, { "Tab", "A", "Z", "E", "R", "T", "Y", "U", "I", "O", "P", "^", "$", "Enter", "7", "8", "9", "+" }, { "ShiftLock", "Q", "S", "D", "F", "G", "H", "J", "K", "L", "M", "ù", "µ", "4", "5", "6" }, { "Shift", "<", "W", "X", "C", "V", "B", "N", ",", ";", ":", "=", "Shift", "Up", "1", "2", "3", "Enter" }, { "Ctrl", "Fn", "Win", "Alt", "Space", "AltGr", "Context", "Ctrl", "Left", "Down", "Right", "0", "." } };
public GUI()
{
super( "Typing Tutor" );
setLayout( new GridLayout( key.length, 0 ) );
for ( int row = 0; row < key.length; row++)
{
panel = new JPanel();
panel.setLayout(new FlowLayout());
for ( int column = 0; column < key[ row ].length; column++ )
{
button = new JButton( key[ row ][ column ] );
panel.add( button );
}
add( panel );
}
}
}
==================================================== =================
import javax.swing.JFrame;
public class GUITest extends JFrame
{
public static void main(String[] args)
{
GUI gui = new GUI();
gui.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
gui.setSize( 1600, 250 );
gui.setVisible( true );
}
}
它在正确的行中显示键,但不完全按原样显示。例如,输入按钮不是矩形的并且是一排的。有什么改进的建议吗?
PS:我想做的是创建一个打字导师,它充当普通的文本编辑器,或者更好的是一个在其他程序之间传递击键的程序,同时我仍然可以在屏幕上看到可视键盘以供参考(和反馈)。只是为了确保我不必看真正的键盘,并且可以随时练习触摸打字。如果您有更好的建议/替代方案或知道是否可能,请告诉我。