我想创建以下GUI:
https://www.dropbox.com/s/ebm057jfevwe532/result.png?dl=0
这是我的代码:
public class TypingTutor extends JFrame{
private JPanel mainPanel;
private JPanel[] keyboardPanel;
private JLabel instructionsLabel;
private JLabel noteLabel;
private JTextArea typingTextArea;
private JButton[] buttonKeysFirstRow;
private JButton[] buttonKeysSecondRow;
private JButton[] buttonKeysThirdRow;
private JButton[] buttonKeysFourthRow;
private JButton[] buttonKeysFifthRow;
//TypingTutor no-argument constructor
public TypingTutor(){
super("Typing Application");
setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
instructionsLabel = new JLabel("Type some text using your keyboard. The keys you press " +
"will be highlighted and the text will be displayed.");
noteLabel = new JLabel("Note: Clicking the buttons with your mouse will not perform any " +
"action.");
add(instructionsLabel);
add(noteLabel);
typingTextArea = new JTextArea();
add(typingTextArea);
//each row represents each row in the keyboard
buttonKeysFirstRow = new JButton[14];
buttonKeysSecondRow = new JButton[13];
buttonKeysThirdRow = new JButton[14];
buttonKeysFourthRow = new JButton[12];
buttonKeysFifthRow = new JButton[4];
initializeKeys(1);
initializeKeys(2);
initializeKeys(3);
initializeKeys(4);
initializeKeys(5);
Box[] keyboard = new Box[5];
for(int i = 0; i < keyboard.length; i++){
keyboard[i] = Box.createHorizontalBox();
switch(i){
case 0:
for(int j = 0; j < buttonKeysFirstRow.length; j++){
keyboard[i].add(buttonKeysFirstRow[j]);
} //end for
break;
case 1:
for(int j = 0; j < buttonKeysSecondRow.length; j++){
keyboard[i].add(buttonKeysSecondRow[j]);
} //end for
break;
case 2:
for(int j = 0; j < buttonKeysThirdRow.length; j++){
keyboard[i].add(buttonKeysThirdRow[j]);
} //end for
break;
case 3:
for(int j = 0; j < buttonKeysFourthRow.length; j++){
keyboard[i].add(buttonKeysFourthRow[j]);
} //end for
break;
case 4:
for(int j = 0; j < buttonKeysFifthRow.length; j++){
keyboard[i].add(buttonKeysFifthRow[j]);
} //end for
break;
} //end switch
add(keyboard[i]);
} //end for
} //end TypingTutor no-argument constructor
//initializes button keys with their values
private void initializeKeys(int keys){
switch(keys){
case 1:
buttonKeysFirstRow[0] = new JButton("|\n°");
buttonKeysFirstRow[1] = new JButton("1");
buttonKeysFirstRow[2] = new JButton("2");
buttonKeysFirstRow[3] = new JButton("3");
buttonKeysFirstRow[4] = new JButton("4");
buttonKeysFirstRow[5] = new JButton("5");
buttonKeysFirstRow[6] = new JButton("6");
buttonKeysFirstRow[7] = new JButton("7");
buttonKeysFirstRow[8] = new JButton("8");
buttonKeysFirstRow[9] = new JButton("9");
buttonKeysFirstRow[10] = new JButton("0");
buttonKeysFirstRow[11] = new JButton("'\n?");
buttonKeysFirstRow[12] = new JButton("¿\n¡");
buttonKeysFirstRow[13] = new JButton("Backspace");
break;
case 2:
buttonKeysSecondRow[0] = new JButton(" Tab ");
buttonKeysSecondRow[1] = new JButton("Q");
buttonKeysSecondRow[2] = new JButton("W");
buttonKeysSecondRow[3] = new JButton("E");
buttonKeysSecondRow[4] = new JButton("R");
buttonKeysSecondRow[5] = new JButton("T");
buttonKeysSecondRow[6] = new JButton("Y");
buttonKeysSecondRow[7] = new JButton("U");
buttonKeysSecondRow[8] = new JButton("I");
buttonKeysSecondRow[9] = new JButton("O");
buttonKeysSecondRow[10] = new JButton("P");
buttonKeysSecondRow[11] = new JButton("´\n¨");
buttonKeysSecondRow[12] = new JButton("+\n*");
break;
case 3:
buttonKeysThirdRow[0] = new JButton(" Caps ");
buttonKeysThirdRow[1] = new JButton("A");
buttonKeysThirdRow[2] = new JButton("S");
buttonKeysThirdRow[3] = new JButton("D");
buttonKeysThirdRow[4] = new JButton("F");
buttonKeysThirdRow[5] = new JButton("G");
buttonKeysThirdRow[6] = new JButton("H");
buttonKeysThirdRow[7] = new JButton("J");
buttonKeysThirdRow[8] = new JButton("K");
buttonKeysThirdRow[9] = new JButton("L");
buttonKeysThirdRow[10] = new JButton("Ñ");
buttonKeysThirdRow[11] = new JButton("{\n[");
buttonKeysThirdRow[12] = new JButton("}\n]");
buttonKeysThirdRow[13] = new JButton(" Enter ");
break;
case 4:
buttonKeysFourthRow[0] = new JButton(" Shift ");
buttonKeysFourthRow[1] = new JButton("<\n>");
buttonKeysFourthRow[2] = new JButton("Z");
buttonKeysFourthRow[3] = new JButton("X");
buttonKeysFourthRow[4] = new JButton("C");
buttonKeysFourthRow[5] = new JButton("V");
buttonKeysFourthRow[6] = new JButton("B");
buttonKeysFourthRow[7] = new JButton("N");
buttonKeysFourthRow[8] = new JButton("M");
buttonKeysFourthRow[9] = new JButton(",\n;");
buttonKeysFourthRow[10] = new JButton(".\n:");
buttonKeysFourthRow[11] = new JButton("^");
break;
case 5:
buttonKeysFifthRow[0] = new JButton("");
buttonKeysFifthRow[1] = new JButton("<");
buttonKeysFifthRow[2] = new JButton("v");
buttonKeysFifthRow[3] = new JButton(">");
buttonKeysFifthRow[0].setMinimumSize(new Dimension(50, 10));
break;
} //end switch
} //end method initializeKeys
} //end class TypingTutor
这是我的结果:
https://www.dropbox.com/s/dx99npirnl0v1wd/sample.png?dl=0
我尝试使用类javax.swing.Box并将一个带Box.createVerticalBox()的主Box添加到JFrame BorderLayout的中心,然后使用Box.createHorizontalBox()添加其他框,我得到相同的结果,一切都好,直到我在JTextArea之后添加JButton,之后,JButton缩小,JLabel向右对齐。
答案 0 :(得分:1)
我对您的代码进行了一些修改,以使其可以运行。这是现在打字导师GUI的图像。
我将指令文本居中,使打字区域合理,并将打字区域放入滚动窗格。我稍微修了一下空格键。
这是可运行的代码。
package com.ggl.testing;
import java.awt.Dimension;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
public class TypingTutor extends JFrame {
private static final long serialVersionUID = -7372491882460254385L;
private JLabel instructionsLabel;
private JLabel noteLabel;
private JTextArea typingTextArea;
private JButton[] buttonKeysFirstRow;
private JButton[] buttonKeysSecondRow;
private JButton[] buttonKeysThirdRow;
private JButton[] buttonKeysFourthRow;
private JButton[] buttonKeysFifthRow;
// TypingTutor no-argument constructor
public TypingTutor() {
super("Typing Application");
setLocationByPlatform(true);
setLayout(new BoxLayout(getContentPane(), BoxLayout.PAGE_AXIS));
JPanel instructionPanel = new JPanel();
instructionPanel.setLayout(
new BoxLayout(instructionPanel, BoxLayout.PAGE_AXIS));
instructionsLabel = new JLabel(
"Type some text using your keyboard. The keys you press "
+ "will be highlighted and the text will be displayed.");
instructionsLabel.setAlignmentX(JLabel.CENTER_ALIGNMENT);
instructionPanel.add(instructionsLabel);
noteLabel = new JLabel(
"Note: Clicking the buttons with your mouse will not perform any "
+ "action.");
noteLabel.setAlignmentX(JLabel.CENTER_ALIGNMENT);
instructionPanel.add(noteLabel);
add(instructionPanel);
typingTextArea = new JTextArea(10, 60);
typingTextArea.setWrapStyleWord(true);
JScrollPane scrollPane = new JScrollPane(typingTextArea);
add(scrollPane);
// each row represents each row in the keyboard
buttonKeysFirstRow = new JButton[14];
buttonKeysSecondRow = new JButton[13];
buttonKeysThirdRow = new JButton[14];
buttonKeysFourthRow = new JButton[12];
buttonKeysFifthRow = new JButton[4];
initializeKeys(1);
initializeKeys(2);
initializeKeys(3);
initializeKeys(4);
initializeKeys(5);
Box[] keyboard = new Box[5];
for (int i = 0; i < keyboard.length; i++) {
keyboard[i] = Box.createHorizontalBox();
switch (i) {
case 0:
for (int j = 0; j < buttonKeysFirstRow.length; j++) {
keyboard[i].add(buttonKeysFirstRow[j]);
} // end for
break;
case 1:
for (int j = 0; j < buttonKeysSecondRow.length; j++) {
keyboard[i].add(buttonKeysSecondRow[j]);
} // end for
break;
case 2:
for (int j = 0; j < buttonKeysThirdRow.length; j++) {
keyboard[i].add(buttonKeysThirdRow[j]);
} // end for
break;
case 3:
for (int j = 0; j < buttonKeysFourthRow.length; j++) {
keyboard[i].add(buttonKeysFourthRow[j]);
} // end for
break;
case 4:
for (int j = 0; j < buttonKeysFifthRow.length; j++) {
keyboard[i].add(buttonKeysFifthRow[j]);
} // end for
break;
} // end switch
add(keyboard[i]);
} // end for
this.pack();
this.setVisible(true);
} // end TypingTutor no-argument constructor
// initializes button keys with their values
private void initializeKeys(int keys) {
switch (keys) {
case 1:
buttonKeysFirstRow[0] = new JButton("|\n°");
buttonKeysFirstRow[1] = new JButton("1");
buttonKeysFirstRow[2] = new JButton("2");
buttonKeysFirstRow[3] = new JButton("3");
buttonKeysFirstRow[4] = new JButton("4");
buttonKeysFirstRow[5] = new JButton("5");
buttonKeysFirstRow[6] = new JButton("6");
buttonKeysFirstRow[7] = new JButton("7");
buttonKeysFirstRow[8] = new JButton("8");
buttonKeysFirstRow[9] = new JButton("9");
buttonKeysFirstRow[10] = new JButton("0");
buttonKeysFirstRow[11] = new JButton("'\n?");
buttonKeysFirstRow[12] = new JButton("¿\n¡");
buttonKeysFirstRow[13] = new JButton("Backspace");
break;
case 2:
buttonKeysSecondRow[0] = new JButton(" Tab ");
buttonKeysSecondRow[1] = new JButton("Q");
buttonKeysSecondRow[2] = new JButton("W");
buttonKeysSecondRow[3] = new JButton("E");
buttonKeysSecondRow[4] = new JButton("R");
buttonKeysSecondRow[5] = new JButton("T");
buttonKeysSecondRow[6] = new JButton("Y");
buttonKeysSecondRow[7] = new JButton("U");
buttonKeysSecondRow[8] = new JButton("I");
buttonKeysSecondRow[9] = new JButton("O");
buttonKeysSecondRow[10] = new JButton("P");
buttonKeysSecondRow[11] = new JButton("´\n¨");
buttonKeysSecondRow[12] = new JButton("+\n*");
break;
case 3:
buttonKeysThirdRow[0] = new JButton(" Caps ");
buttonKeysThirdRow[1] = new JButton("A");
buttonKeysThirdRow[2] = new JButton("S");
buttonKeysThirdRow[3] = new JButton("D");
buttonKeysThirdRow[4] = new JButton("F");
buttonKeysThirdRow[5] = new JButton("G");
buttonKeysThirdRow[6] = new JButton("H");
buttonKeysThirdRow[7] = new JButton("J");
buttonKeysThirdRow[8] = new JButton("K");
buttonKeysThirdRow[9] = new JButton("L");
buttonKeysThirdRow[10] = new JButton("Ñ");
buttonKeysThirdRow[11] = new JButton("{\n[");
buttonKeysThirdRow[12] = new JButton("}\n]");
buttonKeysThirdRow[13] = new JButton(" Enter ");
break;
case 4:
buttonKeysFourthRow[0] = new JButton(" Shift ");
buttonKeysFourthRow[1] = new JButton("<\n>");
buttonKeysFourthRow[2] = new JButton("Z");
buttonKeysFourthRow[3] = new JButton("X");
buttonKeysFourthRow[4] = new JButton("C");
buttonKeysFourthRow[5] = new JButton("V");
buttonKeysFourthRow[6] = new JButton("B");
buttonKeysFourthRow[7] = new JButton("N");
buttonKeysFourthRow[8] = new JButton("M");
buttonKeysFourthRow[9] = new JButton(",\n;");
buttonKeysFourthRow[10] = new JButton(".\n:");
buttonKeysFourthRow[11] = new JButton("^");
break;
case 5:
buttonKeysFifthRow[0] = new JButton(" ");
buttonKeysFifthRow[1] = new JButton("<");
buttonKeysFifthRow[2] = new JButton("v");
buttonKeysFifthRow[3] = new JButton(">");
break;
} // end switch
} // end method initializeKeys
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new TypingTutor();
}
});
}
} // end class TypingTutor
答案 1 :(得分:0)
非直观地 - 您遇到了默认标签对齐问题。当您将标签与BoxLayout中的某些其他组件结合使用时会出现这种情况 - 请参阅alignment in BoxLayout上的Java教程
试试这个JLabel(请注意两个新行):
instructionsLabel = new JLabel("Type some text using your keyboard. The keys you press " +
"will be highlighted and the text will be displayed.");
noteLabel = new JLabel("Note: Clicking the buttons with your mouse will not perform any " +
"action.");
instructionsLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
noteLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
add(instructionsLabel);
add(noteLabel);
您可能需要玩一下才能正确显示空格键的大小 - 计算机上的50 x 10显示屏非常小......
<强> 编辑: 强>
实际上 - 显示小的空格键也是JButtons如何绘制的问题 - see this question。总之,您必须设置最小和首选大小,然后设置最大大小,以确保它检查最小大小并正确绘制。因此,请使用以下内容替换您的.setMinimumSize(...
(尺寸250 x 26在我的显示器上运行良好)
Dimension spaceDimension = new Dimension(250, 26);
buttonKeysFifthRow[0].setMinimumSize(spaceDimension);
buttonKeysFifthRow[0].setPreferredSize(spaceDimension);
buttonKeysFifthRow[0].setMaximumSize(spaceDimension);