0

我到目前为止的代码如下:

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:我想做的是创建一个打字导师,它充当普通的文本编辑器,或者更好的是一个在其他程序之间传递击键的程序,同时我仍然可以在屏幕上看到可视键盘以供参考(和反馈)。只是为了确保我不必看真正的键盘,并且可以随时练习触摸打字。如果您有更好的建议/替代方案或知道是否可能,请告诉我。

4

1 回答 1

4
  • private JPanel[] panel;private JButton[][] button;未初始化的数组

  • KeyBoard 的指定不好,必须放一些不可见但占用或拆分布局到三个单独的 JPanel 的 JButton(再加上一个用于 ESC - F12 - Pause/Break)

  • removeprivate JPanel[] panel;是没用的,把 JButtons 直接放到 JPanel 上,这让我对 Borders 有意义,但是你需要创建和分离具有与 for 相同数量的成员的数组private JButton[][] button;,在那里你可以将 Border 作为具有四个数字的子数组(i, i, i, i) 一个成员

快速修复,只需要我在最后两点中描述的微小更改

在此处输入图像描述

import java.awt.EventQueue;
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.JPanel;

public class GUI extends JFrame {

    private JFrame frame = new JFrame("Typing Tutor");
    private JPanel parent = new JPanel(new GridLayout(0, 1));
    private JPanel[] panel;
    private JButton[][] button;
    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");
        panel = new JPanel[6];
        for (int row = 0; row < key.length; row++) {
            panel[row] = new JPanel();
            button = new JButton[20][20];
            for (int column = 0; column < key[row].length; column++) {
                button[row][column] = new JButton(key[row][column]);
                button[row][column].putClientProperty("column", column);
                button[row][column].putClientProperty("row", row);
                button[row][column].putClientProperty("key", key[row][column]);
                button[row][column].addActionListener(new MyActionListener());
                panel[row].add(button[row][column]);
            }
            parent.add(panel[row]);
        }
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        add(parent);
        pack();
        setVisible(true);
    }

    public class MyActionListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            JButton btn = (JButton) e.getSource();
            System.out.println("clicked column --> " + btn.getClientProperty("column")
                    + ", row --> " + btn.getClientProperty("row")
                    + ", Key Typed --> " + btn.getClientProperty("key"));
        }
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                GUI guI = new GUI();
            }
        });
    }
}
于 2013-10-09T06:48:49.840 回答