gpt4 book ai didi

java - Box Layout 移动组件、对齐

转载 作者:行者123 更新时间:2023-12-01 12:03:24 25 4
gpt4 key购买 nike

我想创建以下 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() 添加一个主框到 JFrame BorderLayout 的中心,然后使用 Box.createHorizo​​ntalBox() 添加其他框,我得到了相同的结果,一切正常,直到我在 JTextArea 之后添加 JButtons,之后,JButtons 缩小并且 JLabel 向右对齐。

最佳答案

我对您的代码进行了一些修改以使其可以运行。现在这是打字导师 GUI 的图像。

Typing Tutor

我将说明文本居中,将打字区域设置为合理的大小,并将打字区域放入滚动 Pane 中。我稍微修复了空格键。

这是可运行的代码。

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

关于java - Box Layout 移动组件、对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27848384/

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