gpt4 book ai didi

java - 如何在虚拟键盘上使字母彼此间隔

转载 作者:行者123 更新时间:2023-12-02 11:35:13 25 4
gpt4 key购买 nike

我最近才学会如何使用 JButton、JFrame 等,并正在尝试制作键盘。到目前为止,当我单击字母时,会出现 JLabel,但我很难让它们彼此相邻,因为它们都出现在彼此之上。我想知道是否有人可以帮助我解决这个问题。我知道我的代码并不理想,但虽然我仍然缺乏经验,但这是目前我知道如何做到这一点的唯一方法。这是到目前为止我的代码(不包括用于运行整个代码的“Run”方法):

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class Frame extends JFrame {

JButton a;
JButton b;
JButton c;
JButton d;
JButton e;
JButton f;
JButton g;
JButton h;
JButton i;
JButton j;
JButton k;
JButton l;
JButton m;
JButton n;
JButton o;
JButton p;
JButton q;
JButton r;
JButton s;
JButton t;
JButton u;
JButton v;
JButton w;
JButton x;
JButton y;
JButton z;
JButton space;
JPanel panel;

public Frame() {

panel = new JPanel();
panel.setBackground(Color.WHITE);
panel.setLayout(null);
add(panel);

a = new JButton("A");
a.setBounds(100, 0, 50, 37);
b = new JButton("B");
b.setBounds(200, 0, 50, 37);
c = new JButton("C");
c.setBounds(300, 0, 50, 37);
d = new JButton("D");
d.setBounds(400, 0, 50, 37);
e = new JButton("E");
e.setBounds(500, 0, 50, 37);
f = new JButton("F");
f.setBounds(600, 0, 50, 37);
g = new JButton("G");
g.setBounds(100, 50, 50, 37);
h = new JButton("H");
h.setBounds(200, 50, 50, 37);
i = new JButton("I");
i.setBounds(300, 50, 50, 37);
j = new JButton("J");
j.setBounds(400, 50, 50, 37);
k = new JButton("K");
k.setBounds(500, 50, 50, 37);
l = new JButton("L");
l.setBounds(600, 50, 50, 37);
m = new JButton("M");
m.setBounds(100, 100, 50, 37);
n = new JButton("N");
n.setBounds(200, 100, 50, 37);
o = new JButton("O");
o.setBounds(300, 100, 50, 37);
p = new JButton("P");
p.setBounds(400, 100, 50, 37);
q = new JButton("Q");
q.setBounds(500, 100, 50, 37);
r = new JButton("R");
r.setBounds(600, 100, 50, 37);
s = new JButton("S");
s.setBounds(100, 150, 50, 37);
t = new JButton("T");
t.setBounds(200, 150, 50, 37);
u = new JButton("U");
u.setBounds(300, 150, 50, 37);
v = new JButton("V");
v.setBounds(400, 150, 50, 37);
w = new JButton("W");
w.setBounds(500, 150, 50, 37);
x = new JButton("X");
x.setBounds(600, 150, 50, 37);
y = new JButton("Y");
y.setBounds(100, 200, 50, 37);
z = new JButton("Z");
z.setBounds(200, 200, 50, 37);

space = new JButton("SPACE");
space.setBounds(300, 200, 100, 37);

panel.add(a);
panel.add(b);
panel.add(c);
panel.add(d);
panel.add(e);
panel.add(f);
panel.add(g);
panel.add(h);
panel.add(i);
panel.add(j);
panel.add(k);
panel.add(l);
panel.add(m);
panel.add(n);
panel.add(o);
panel.add(p);
panel.add(q);
panel.add(r);
panel.add(s);
panel.add(t);
panel.add(u);
panel.add(v);
panel.add(w);
panel.add(x);
panel.add(y);
panel.add(z);
panel.add(space);

Handler handler = new Handler();
a.addActionListener(handler);
b.addActionListener(handler);
c.addActionListener(handler);
d.addActionListener(handler);
e.addActionListener(handler);
f.addActionListener(handler);
g.addActionListener(handler);
h.addActionListener(handler);
i.addActionListener(handler);
j.addActionListener(handler);
k.addActionListener(handler);
l.addActionListener(handler);
m.addActionListener(handler);
n.addActionListener(handler);
o.addActionListener(handler);
p.addActionListener(handler);
q.addActionListener(handler);
r.addActionListener(handler);
s.addActionListener(handler);
t.addActionListener(handler);
u.addActionListener(handler);
v.addActionListener(handler);
w.addActionListener(handler);
x.addActionListener(handler);
y.addActionListener(handler);
z.addActionListener(handler);

space.addActionListener(handler);
}

class Handler implements ActionListener {
public void actionPerformed(ActionEvent event) {
if (event.getSource() == a) {
JLabel text = new JLabel("A");
text.setBounds(100, 250, 100, 100);
text.setFont(new Font("Serif", Font.BOLD, 50));
panel.add(text);
panel.repaint();
panel.revalidate();
}
if (event.getSource() == b) {
JLabel text2 = new JLabel("B");
text2.setBounds(100, 250, 100, 100);
text2.setFont(new Font("Serif", Font.BOLD, 50));
panel.add(text2);
panel.repaint();
panel.revalidate();
}
if (event.getSource() == c) {
JLabel text3 = new JLabel("C");
text3.setBounds(100, 250, 100, 100);
text3.setFont(new Font("Serif", Font.BOLD, 50));
panel.add(text3);
panel.repaint();
panel.revalidate();
}
if (event.getSource() == d) {
JLabel text4 = new JLabel("D");
text4.setBounds(100, 250, 100, 100);
text4.setFont(new Font("Serif", Font.BOLD, 50));
panel.add(text4);
panel.repaint();
panel.revalidate();
}
if (event.getSource() == e) {
JLabel text5 = new JLabel("E");
text5.setBounds(100, 250, 100, 100);
text5.setFont(new Font("Serif", Font.BOLD, 50));
panel.add(text5);
panel.repaint();
panel.revalidate();
}
if (event.getSource() == f) {
JLabel text6 = new JLabel("F");
text6.setBounds(100, 250, 100, 100);
text6.setFont(new Font("Serif", Font.BOLD, 50));
panel.add(text6);
panel.repaint();
panel.revalidate();
}
if (event.getSource() == g) {
JLabel text7 = new JLabel("G");
text7.setBounds(100, 250, 100, 100);
text7.setFont(new Font("Serif", Font.BOLD, 50));
panel.add(text7);
panel.repaint();
panel.revalidate();
}
if (event.getSource() == h) {
JLabel text8 = new JLabel("H");
text8.setBounds(100, 250, 100, 100);
text8.setFont(new Font("Serif", Font.BOLD, 50));
panel.add(text8);
panel.repaint();
panel.revalidate();
}
if (event.getSource() == i) {
JLabel text9 = new JLabel("I");
text9.setBounds(100, 250, 100, 100);
text9.setFont(new Font("Serif", Font.BOLD, 50));
panel.add(text9);
panel.repaint();
panel.revalidate();
}
if (event.getSource() == j) {
JLabel text10 = new JLabel("J");
text10.setBounds(100, 250, 100, 100);
text10.setFont(new Font("Serif", Font.BOLD, 50));
panel.add(text10);
panel.repaint();
panel.revalidate();
}
if (event.getSource() == k) {
JLabel text11 = new JLabel("K");
text11.setBounds(100, 250, 100, 100);
text11.setFont(new Font("Serif", Font.BOLD, 50));
panel.add(text11);
panel.repaint();
panel.revalidate();
}
if (event.getSource() == l) {
JLabel text12 = new JLabel("L");
text12.setBounds(100, 250, 100, 100);
text12.setFont(new Font("Serif", Font.BOLD, 50));
panel.add(text12);
panel.repaint();
panel.revalidate();
}
if (event.getSource() == m) {
JLabel text13 = new JLabel("M");
text13.setBounds(100, 250, 100, 100);
text13.setFont(new Font("Serif", Font.BOLD, 50));
panel.add(text13);
panel.repaint();
panel.revalidate();
}
if (event.getSource() == n) {
JLabel text14 = new JLabel("N");
text14.setBounds(100, 250, 100, 100);
text14.setFont(new Font("Serif", Font.BOLD, 50));
panel.add(text14);
panel.repaint();
panel.revalidate();
}
if (event.getSource() == o) {
JLabel text15 = new JLabel("O");
text15.setBounds(100, 250, 100, 100);
text15.setFont(new Font("Serif", Font.BOLD, 50));
panel.add(text15);
panel.repaint();
panel.revalidate();
}
if (event.getSource() == p) {
JLabel text16 = new JLabel("P");
text16.setBounds(100, 250, 100, 100);
text16.setFont(new Font("Serif", Font.BOLD, 50));
panel.add(text16);
panel.repaint();
panel.revalidate();
}
if (event.getSource() == q) {
JLabel text17 = new JLabel("Q");
text17.setBounds(100, 250, 100, 100);
text17.setFont(new Font("Serif", Font.BOLD, 50));
panel.add(text17);
panel.repaint();
panel.revalidate();
}
if (event.getSource() == r) {
JLabel text18 = new JLabel("R");
text18.setBounds(100, 250, 100, 100);
text18.setFont(new Font("Serif", Font.BOLD, 50));
panel.add(text18);
panel.repaint();
panel.revalidate();
}
if (event.getSource() == s) {
JLabel text19 = new JLabel("S");
text19.setBounds(100, 250, 100, 100);
text19.setFont(new Font("Serif", Font.BOLD, 50));
panel.add(text19);
panel.repaint();
panel.revalidate();
}
if (event.getSource() == t) {
JLabel text20 = new JLabel("T");
text20.setBounds(100, 250, 100, 100);
text20.setFont(new Font("Serif", Font.BOLD, 50));
panel.add(text20);
panel.repaint();
panel.revalidate();
}
if (event.getSource() == u) {
JLabel text21 = new JLabel("U");
text21.setBounds(100, 250, 100, 100);
text21.setFont(new Font("Serif", Font.BOLD, 50));
panel.add(text21);
panel.repaint();
panel.revalidate();
}
if (event.getSource() == v) {
JLabel text22 = new JLabel("V");
text22.setBounds(100, 250, 100, 100);
text22.setFont(new Font("Serif", Font.BOLD, 50));
panel.add(text22);
panel.repaint();
panel.revalidate();
}
if (event.getSource() == w) {
JLabel text23 = new JLabel("W");
text23.setBounds(100, 250, 100, 100);
text23.setFont(new Font("Serif", Font.BOLD, 50));
panel.add(text23);
panel.repaint();
panel.revalidate();
}
if (event.getSource() == x) {
JLabel text24 = new JLabel("X");
text24.setBounds(100, 250, 100, 100);
text24.setFont(new Font("Serif", Font.BOLD, 50));
panel.add(text24);
panel.repaint();
panel.revalidate();
}
if (event.getSource() == y) {
JLabel text25 = new JLabel("Y");
text25.setBounds(100, 250, 100, 100);
text25.setFont(new Font("Serif", Font.BOLD, 50));
panel.add(text25);
panel.repaint();
panel.revalidate();
}
if (event.getSource() == z) {
JLabel text26 = new JLabel("Z");
text26.setBounds(100, 250, 100, 100);
text26.setFont(new Font("Serif", Font.BOLD, 50));
panel.add(text26);
panel.repaint();
panel.revalidate();
}
if (event.getSource() == space) {
JLabel text27 = new JLabel(" ");
text27.setBounds(100, 250, 100, 100);
text27.setFont(new Font("Serif", Font.BOLD, 50));
panel.add(text27);
panel.repaint();
panel.revalidate();
}
}
}
}

enter image description here

最佳答案

您的代码中有几个错误:

  1. 您正在调用 Frame给您JFrame ,由于 AWT Frame ,这可能会令人困惑。类。

  2. 您正在扩展 JFrame ,但你不会在任何地方改变它的行为,所以没有必要。基于JPanel构建您的GUI未开启 JFrame s。请参阅:Extends JFrame vs. creating it inside the program了解更多相关信息。

  3. 您正在为每个字母创建一个新变量。您可以将其更改为数组并使用 for循环并迭代它们,这样您的代码就更容易阅读、维护和理解。

  4. 也许您的代码中最重要和最危险的错误:您正在使用 null-layout并手动定位每个组件,这可能会导致您在不同的操作系统/PLAF/屏幕尺寸和分辨率中出现奇怪的输出!,请参阅this example快速检查为什么不鼓励使用 null-layout构建您的 GUI。请参阅Null layout is evilWhy is it frowned upon to use a null layout in Swing?有关此的更多信息。要纠正它,请正确使用各种Layout managers并与其他人一起使用它们并省略对 .setBounds(...) 的调用.

  5. 不要将对象与 == 进行比较:if (event.getSource() == a) {应该是if (event.getSource().equals(a)) {相反或如下面的代码所示 ife.getActionCommand().equals("TextInsideYourJButton") { ... }

  6. 最后但同样重要的是,为变量使用有意义的名称:a如果有人第一次阅读您的代码,他/她将很难确定aJButton !关注 Java naming conventions .

现在,我编写的代码是在您发布图像链接之前编写的,因此 GUI 可能会有所不同:

import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;

public class VirtualKeyBoard {
private JFrame frame;
private JPanel mainPane;
private GridBagConstraints c;
private JLabel label;

private static final String[] NUMBERS = new String[] {"1", "2", "3", "4", "5", "6", "7", "8", "9", "0"};
private static final String[] FIRST_ROW_LETTERS = new String[] {"Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P"};
private static final String[] SECOND_ROW_LETTERS = new String[] {"A", "S", "D", "F", "G", "H", "J", "K", "L"};
private static final String[] THIRD_ROW_LETTERS = new String[] {"Z", "X", "C", "V", "B", "N", "M"};

public static void main(String[] args) {
SwingUtilities.invokeLater(new VirtualKeyBoard()::createAndShowGui);
}

private void createAndShowGui() {
frame = new JFrame();
mainPane = new JPanel();
mainPane.setLayout(new GridLayout(6, 1, 5, 5));

label = new JLabel("");
label.setHorizontalAlignment(SwingConstants.CENTER);

c = new GridBagConstraints();
c.insets = new Insets(5, 5, 5, 5);
c.fill = GridBagConstraints.BOTH;

mainPane.add(addRow(NUMBERS));
mainPane.add(addRow(FIRST_ROW_LETTERS));
mainPane.add(addRow(SECOND_ROW_LETTERS));
mainPane.add(addRow(THIRD_ROW_LETTERS));

JButton spaceButton = new JButton("SPACE");
spaceButton.addActionListener(listener);

mainPane.add(spaceButton);
mainPane.add(label);

frame.add(mainPane);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

private ActionListener listener = e -> {
if (e.getActionCommand().equals("SPACE")) {
label.setText("");
} else {
label.setText(e.getActionCommand());
}
};

private JPanel addRow(String[] values) {
JPanel pane = new JPanel();
pane.setLayout(new GridBagLayout());

for (String s : values) {
c.gridx++;
JButton button = new JButton(s);
button.addActionListener(listener);
pane.add(button, c);
}

return pane;
}
}

上面的代码产生这个输出。正如你所看到的,我从未使用过 .setBounds() ,但我组合了 2 个布局管理器,例如 GridLayoutGridBagLayout我使用了一个 ActionListener对于我们所有人JButton刚刚设置了 ActionCommandJLabel单击时。

enter image description here

要生成与您在图像中发布的输出类似的输出:

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;

public class SimpleVirtualKeyBoard {
private JFrame frame;
private JPanel pane;
private JButton[] buttons;
private JButton spaceButton;
private JLabel label;

public static void main(String[] args) {
SwingUtilities.invokeLater(new SimpleVirtualKeyBoard()::createAndShowGui);
}

private void createAndShowGui() {
frame = new JFrame(getClass().getSimpleName());

pane = new JPanel();
pane.setLayout(new GridLayout(0, 6, 10, 10));

String[] letters = new String[26];

spaceButton = new JButton("SPACE");
spaceButton.addActionListener(listener);

label = new JLabel(" ");
label.setHorizontalAlignment(SwingConstants.CENTER);

for (char i = 'A'; i <= 'Z'; i++) {
letters[i - 'A'] = String.valueOf(i); //We fill our alphabet array
System.out.println(letters[i - 'A']);
}

buttons = new JButton[26];

for (int i = 0; i < buttons.length; i++) {
buttons[i] = new JButton(letters[i]);
buttons[i].addActionListener(listener);

pane.add(buttons[i]);
}

pane.add(spaceButton);

frame.add(pane);
frame.add(label, BorderLayout.SOUTH);

frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

private ActionListener listener = e -> {
label.setText(e.getActionCommand());
};
}

产生以下输出:

enter image description here

你可以在上面的代码中看到我再次没有使用 .setBounds()在这种情况下,我使用了一个 LayoutManger这是GridLayout

关于java - 如何在虚拟键盘上使字母彼此间隔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48993725/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com