JLabel 不会在单击按钮时显示
JLabel wont show up on button click
这是我的第一个 GUI,所以这里的任何建议都是很好的建议,但主要是我只是想弄清楚为什么我的 errorLabel
不会出现。我试过 setVisible
、setOpaque
,手动设置位置,最后(如下所示)只是让它成为自己的面板,按下按钮时它仍然不会显示.
有什么想法吗?
package GUI;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import javax.swing.*;
/**
*
* @author User
*/
public class AddChair extends JFrame {
private final int FRAME_HEIGHT = 600;
private final int FRAME_WIDTH = 400;
private final String ADD_CHAIR_TITLE = "Chair";
private final int TEXT_BOX_WIDTH = FRAME_WIDTH / 4;
private final int TEXT_BOX_HEIGHT = 25;
private JPanel basePanel;
private JPanel leftPanel;
private JPanel rightPanel;
private JPanel lowerPanel;
private JPanel itemIDPanel;
private JPanel woodTypePanel;
private JPanel armrestPanel;
private JPanel quantityPanel;
private JPanel itemIDInputPanel;
private JPanel woodTypeInputPanel;
private JPanel quantityInputPanel;
private JPanel armrestInputPanel;
private JPanel confirmCancelPanel;
private JPanel errorPanel;
private JTextField itemIDTextField;
private JTextField quantityTextField;
private JRadioButton walnut;
private JRadioButton oak;
private JRadioButton yes;
private JRadioButton no;
private JButton confirmButton;
private JButton cancelButton;
public AddChair() {
frameSetup();
add(basePanel());
}
private void frameSetup() { // sets up base frame
setSize(FRAME_WIDTH, FRAME_HEIGHT);
setVisible(true);
setAlwaysOnTop(true);
setTitle(ADD_CHAIR_TITLE);
setLocationRelativeTo(null);
}
private JPanel basePanel() { //sets up a base panel to arrange other panels on
basePanel = new JPanel(new BorderLayout());
basePanel.setPreferredSize(new Dimension(FRAME_WIDTH, FRAME_HEIGHT));
basePanel.add(leftPanel(), BorderLayout.WEST);
basePanel.add(rightPanel(), BorderLayout.EAST);
basePanel.add(lowerPanel(), BorderLayout.SOUTH);
return basePanel;
}
private JPanel leftPanel() { //sets up the left panel which will contain labels
leftPanel = new JPanel();
leftPanel.setPreferredSize(new Dimension(FRAME_WIDTH / 2, FRAME_HEIGHT * 3 / 4));
leftPanel.setVisible(true);
labelPanels();
labels();
return leftPanel;
}
private void labelPanels() {//sets up the panels for the labels
itemIDPanel = new JPanel();
woodTypePanel = new JPanel();
armrestPanel = new JPanel();
quantityPanel = new JPanel();
itemIDPanel.setPreferredSize(new Dimension(FRAME_WIDTH / 2, FRAME_HEIGHT * 3 / 4 / 5));
woodTypePanel.setPreferredSize(new Dimension(FRAME_WIDTH / 2, FRAME_HEIGHT * 3 / 4 / 5));
armrestPanel.setPreferredSize(new Dimension(FRAME_WIDTH / 2, FRAME_HEIGHT * 3 / 4 / 5));
quantityPanel.setPreferredSize(new Dimension(FRAME_WIDTH / 2, FRAME_HEIGHT * 3 / 4 / 5));
itemIDPanel.setVisible(true);
woodTypePanel.setVisible(true);
armrestPanel.setVisible(true);
quantityPanel.setVisible(true);
leftPanel.add(itemIDPanel);
leftPanel.add(woodTypePanel);
leftPanel.add(armrestPanel);
leftPanel.add(quantityPanel);
}
private void labels() { //sets up the and adds the labels for the user
JLabel itemIDLabel = new JLabel("Item ID:");
JLabel woodTypeLabel = new JLabel("Wood Type:");
JLabel armrestLabel = new JLabel("Armrest:");
JLabel quantityLabel = new JLabel("Quantity:");
itemIDLabel.setVisible(true);
woodTypeLabel.setVisible(true);
armrestLabel.setVisible(true);
quantityLabel.setVisible(true);
itemIDPanel.add(itemIDLabel);
woodTypePanel.add(woodTypeLabel);
armrestPanel.add(armrestLabel);
quantityPanel.add(quantityLabel);
}
private JPanel rightPanel() { // sets up the right panel which will contain the user input objects
rightPanel = new JPanel();
rightPanel.setPreferredSize(new Dimension(FRAME_WIDTH / 2, FRAME_HEIGHT * 3 / 4));
rightPanel.setVisible(true);
userInputPanels();
return rightPanel;
}
private void userInputPanels() {//sets up the panels for the labels
itemIDInputPanel = new JPanel();
woodTypeInputPanel = new JPanel();
armrestInputPanel = new JPanel();
quantityInputPanel = new JPanel();
itemIDInputPanel.setPreferredSize(new Dimension(FRAME_WIDTH / 2, FRAME_HEIGHT * 3 / 4 / 5));
woodTypeInputPanel.setPreferredSize(new Dimension(FRAME_WIDTH / 2, FRAME_HEIGHT * 3 / 4 / 5));
armrestInputPanel.setPreferredSize(new Dimension(FRAME_WIDTH / 2, FRAME_HEIGHT * 3 / 4 / 5));
quantityInputPanel.setPreferredSize(new Dimension(FRAME_WIDTH / 2, FRAME_HEIGHT * 3 / 4 / 5));
itemIDInputPanel.setVisible(true);
woodTypeInputPanel.setVisible(true);
armrestInputPanel.setVisible(true);
quantityInputPanel.setVisible(true);
userInputs();
rightPanel.add(itemIDInputPanel);
rightPanel.add(woodTypeInputPanel);
rightPanel.add(armrestInputPanel);
rightPanel.add(quantityInputPanel);
}
private void userInputs() { // sets up and adds to the right panel the user input objects
itemIDTextField = new JTextField("");
walnut = new JRadioButton("Walnut");
oak = new JRadioButton("Oak");
yes = new JRadioButton("Yes");
no = new JRadioButton("No");
quantityTextField = new JTextField("");
itemIDTextField.setPreferredSize(new Dimension(TEXT_BOX_WIDTH, TEXT_BOX_HEIGHT));
itemIDInputPanel.add(itemIDTextField);
ButtonGroup woodTypeGroup = new ButtonGroup();
woodTypeGroup.add(oak);
woodTypeGroup.add(walnut);
woodTypeInputPanel.add(walnut);
woodTypeInputPanel.add(oak);
ButtonGroup armrestGroup = new ButtonGroup();
armrestGroup.add(yes);
armrestGroup.add(no);
armrestInputPanel.add(yes);
armrestInputPanel.add(no);
quantityTextField.setPreferredSize(new Dimension(TEXT_BOX_WIDTH, TEXT_BOX_HEIGHT));
quantityInputPanel.add(quantityTextField);
}
private JPanel lowerPanel() { // sets up the lower panel for confirm and cancel buttons
lowerPanel = new JPanel(new BorderLayout());
lowerPanel.setPreferredSize(new Dimension(FRAME_WIDTH, FRAME_HEIGHT / 4));
lowerPanel.setVisible(true);
confirmCancelPanel();
errorPanel();
confirmCancelButtons();
return lowerPanel;
}
private void confirmCancelPanel(){
confirmCancelPanel = new JPanel();
confirmCancelPanel.setPreferredSize(new Dimension(FRAME_WIDTH, FRAME_HEIGHT / 8));
confirmCancelPanel.setBackground(Color.blue);
confirmCancelPanel.setVisible(true);
lowerPanel.add(confirmCancelPanel, BorderLayout.NORTH);
}
private void errorPanel(){
errorPanel = new JPanel();
errorPanel.setPreferredSize(new Dimension(FRAME_WIDTH, FRAME_HEIGHT / 8));
errorPanel.setBackground(Color.yellow);
errorPanel.setVisible(true);
lowerPanel.add(errorPanel, BorderLayout.SOUTH);
}
private void confirmCancelButtons(){
confirmButton = new JButton("Confirm");
cancelButton = new JButton("Cancel");
confirmButton.setVisible(true);
cancelButton.setVisible(true);
confirmCancelPanel.add(confirmButton);
confirmCancelPanel.add(cancelButton);
cancelButtonLogic();
confirmButtonLogic();
}
private void cancelButtonLogic(){
cancelButton.addActionListener((ActionEvent ae) -> {
this.dispose();
});
}
private void confirmButtonLogic(){
confirmButton.addActionListener((ActionEvent ae) -> {
errorMessage();
});
}
private void errorMessage(){
JLabel errorLabel = new JLabel("Error");
errorLabel.setOpaque(true);
errorLabel.setVisible(true);
errorLabel.setForeground(Color.BLACK);
errorPanel.add(errorLabel);
}
}
我添加了一个静态 main 方法,这样我就可以 运行 GUI。我修改了你的 errorPanel
和 errorMessage
方法。
这是启动时的 GUI。
这是左键单击确认后的 GUI JButton
。
我没有详细查看您的代码,但您似乎发布了很多表单 JPanel
、按钮 JPanel
和错误 JPanel
的代码。
这是完整的运行可用代码。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
/**
*
* @author User
*/
public class AddChair extends JFrame {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new AddChair();
}
});
}
private final int FRAME_HEIGHT = 600;
private final int FRAME_WIDTH = 400;
private final String ADD_CHAIR_TITLE = "Chair";
private final int TEXT_BOX_WIDTH = FRAME_WIDTH / 4;
private final int TEXT_BOX_HEIGHT = 25;
private JLabel errorLabel;
private JPanel basePanel;
private JPanel leftPanel;
private JPanel rightPanel;
private JPanel lowerPanel;
private JPanel itemIDPanel;
private JPanel woodTypePanel;
private JPanel armrestPanel;
private JPanel quantityPanel;
private JPanel itemIDInputPanel;
private JPanel woodTypeInputPanel;
private JPanel quantityInputPanel;
private JPanel armrestInputPanel;
private JPanel confirmCancelPanel;
private JPanel errorPanel;
private JTextField itemIDTextField;
private JTextField quantityTextField;
private JRadioButton walnut;
private JRadioButton oak;
private JRadioButton yes;
private JRadioButton no;
private JButton confirmButton;
private JButton cancelButton;
public AddChair() {
frameSetup();
add(basePanel());
}
private void frameSetup() { // sets up base frame
setSize(FRAME_WIDTH, FRAME_HEIGHT);
setVisible(true);
setAlwaysOnTop(true);
setTitle(ADD_CHAIR_TITLE);
setLocationRelativeTo(null);
}
private JPanel basePanel() { //sets up a base panel to arrange other panels on
basePanel = new JPanel(new BorderLayout());
basePanel.setPreferredSize(new Dimension(FRAME_WIDTH, FRAME_HEIGHT));
basePanel.add(leftPanel(), BorderLayout.WEST);
basePanel.add(rightPanel(), BorderLayout.EAST);
basePanel.add(lowerPanel(), BorderLayout.SOUTH);
return basePanel;
}
private JPanel leftPanel() { //sets up the left panel which will contain labels
leftPanel = new JPanel();
leftPanel.setPreferredSize(new Dimension(FRAME_WIDTH / 2, FRAME_HEIGHT * 3 / 4));
leftPanel.setVisible(true);
labelPanels();
labels();
return leftPanel;
}
private void labelPanels() {//sets up the panels for the labels
itemIDPanel = new JPanel();
woodTypePanel = new JPanel();
armrestPanel = new JPanel();
quantityPanel = new JPanel();
itemIDPanel.setPreferredSize(new Dimension(FRAME_WIDTH / 2, FRAME_HEIGHT * 3 / 4 / 5));
woodTypePanel.setPreferredSize(new Dimension(FRAME_WIDTH / 2, FRAME_HEIGHT * 3 / 4 / 5));
armrestPanel.setPreferredSize(new Dimension(FRAME_WIDTH / 2, FRAME_HEIGHT * 3 / 4 / 5));
quantityPanel.setPreferredSize(new Dimension(FRAME_WIDTH / 2, FRAME_HEIGHT * 3 / 4 / 5));
itemIDPanel.setVisible(true);
woodTypePanel.setVisible(true);
armrestPanel.setVisible(true);
quantityPanel.setVisible(true);
leftPanel.add(itemIDPanel);
leftPanel.add(woodTypePanel);
leftPanel.add(armrestPanel);
leftPanel.add(quantityPanel);
}
private void labels() { //sets up the and adds the labels for the user
JLabel itemIDLabel = new JLabel("Item ID:");
JLabel woodTypeLabel = new JLabel("Wood Type:");
JLabel armrestLabel = new JLabel("Armrest:");
JLabel quantityLabel = new JLabel("Quantity:");
itemIDLabel.setVisible(true);
woodTypeLabel.setVisible(true);
armrestLabel.setVisible(true);
quantityLabel.setVisible(true);
itemIDPanel.add(itemIDLabel);
woodTypePanel.add(woodTypeLabel);
armrestPanel.add(armrestLabel);
quantityPanel.add(quantityLabel);
}
private JPanel rightPanel() { // sets up the right panel which will contain the user input objects
rightPanel = new JPanel();
rightPanel.setPreferredSize(new Dimension(FRAME_WIDTH / 2, FRAME_HEIGHT * 3 / 4));
rightPanel.setVisible(true);
userInputPanels();
return rightPanel;
}
private void userInputPanels() {//sets up the panels for the labels
itemIDInputPanel = new JPanel();
woodTypeInputPanel = new JPanel();
armrestInputPanel = new JPanel();
quantityInputPanel = new JPanel();
itemIDInputPanel.setPreferredSize(new Dimension(FRAME_WIDTH / 2, FRAME_HEIGHT * 3 / 4 / 5));
woodTypeInputPanel.setPreferredSize(new Dimension(FRAME_WIDTH / 2, FRAME_HEIGHT * 3 / 4 / 5));
armrestInputPanel.setPreferredSize(new Dimension(FRAME_WIDTH / 2, FRAME_HEIGHT * 3 / 4 / 5));
quantityInputPanel.setPreferredSize(new Dimension(FRAME_WIDTH / 2, FRAME_HEIGHT * 3 / 4 / 5));
itemIDInputPanel.setVisible(true);
woodTypeInputPanel.setVisible(true);
armrestInputPanel.setVisible(true);
quantityInputPanel.setVisible(true);
userInputs();
rightPanel.add(itemIDInputPanel);
rightPanel.add(woodTypeInputPanel);
rightPanel.add(armrestInputPanel);
rightPanel.add(quantityInputPanel);
}
private void userInputs() { // sets up and adds to the right panel the user input objects
itemIDTextField = new JTextField("");
walnut = new JRadioButton("Walnut");
oak = new JRadioButton("Oak");
yes = new JRadioButton("Yes");
no = new JRadioButton("No");
quantityTextField = new JTextField("");
itemIDTextField.setPreferredSize(new Dimension(TEXT_BOX_WIDTH, TEXT_BOX_HEIGHT));
itemIDInputPanel.add(itemIDTextField);
ButtonGroup woodTypeGroup = new ButtonGroup();
woodTypeGroup.add(oak);
woodTypeGroup.add(walnut);
woodTypeInputPanel.add(walnut);
woodTypeInputPanel.add(oak);
ButtonGroup armrestGroup = new ButtonGroup();
armrestGroup.add(yes);
armrestGroup.add(no);
armrestInputPanel.add(yes);
armrestInputPanel.add(no);
quantityTextField.setPreferredSize(new Dimension(TEXT_BOX_WIDTH, TEXT_BOX_HEIGHT));
quantityInputPanel.add(quantityTextField);
}
private JPanel lowerPanel() { // sets up the lower panel for confirm and cancel buttons
lowerPanel = new JPanel(new BorderLayout());
lowerPanel.setPreferredSize(new Dimension(FRAME_WIDTH, FRAME_HEIGHT / 4));
lowerPanel.setVisible(true);
confirmCancelPanel();
errorPanel();
confirmCancelButtons();
return lowerPanel;
}
private void confirmCancelPanel(){
confirmCancelPanel = new JPanel();
confirmCancelPanel.setPreferredSize(new Dimension(FRAME_WIDTH, FRAME_HEIGHT / 8));
confirmCancelPanel.setBackground(Color.blue);
confirmCancelPanel.setVisible(true);
lowerPanel.add(confirmCancelPanel, BorderLayout.NORTH);
}
private void errorPanel(){
errorPanel = new JPanel();
errorPanel.setPreferredSize(new Dimension(FRAME_WIDTH, FRAME_HEIGHT / 8));
errorPanel.setBackground(Color.yellow);
errorPanel.setVisible(true);
errorLabel = new JLabel(" ");
errorLabel.setOpaque(true);
errorLabel.setVisible(true);
errorLabel.setForeground(Color.BLACK);
errorPanel.add(errorLabel);
lowerPanel.add(errorPanel, BorderLayout.SOUTH);
}
private void confirmCancelButtons(){
confirmButton = new JButton("Confirm");
cancelButton = new JButton("Cancel");
confirmButton.setVisible(true);
cancelButton.setVisible(true);
confirmCancelPanel.add(confirmButton);
confirmCancelPanel.add(cancelButton);
cancelButtonLogic();
confirmButtonLogic();
}
private void cancelButtonLogic(){
cancelButton.addActionListener((ActionEvent ae) -> {
this.dispose();
});
}
private void confirmButtonLogic(){
confirmButton.addActionListener((ActionEvent ae) -> {
errorMessage();
});
}
private void errorMessage(){
errorLabel.setText("Error message");
}
}
I've tried setVisible,
Swing 组件默认可见。无需在组件上调用 setVisible(true)
my errorLabel wont show up.
将组件添加到可见 GUI 时,基本代码为:
errorPanel.add(errorLabel);
errorPanel.revalidate();
errorPanel.repaint();
这将确保调用布局管理器,以便为标签提供 size/location。
但是,另一种解决方案是在创建 GUI 时创建“errorLabel”并将其添加到“errorPanel”。您可以将集合设置为“”。所以标签占用了一些space。然后,您只需调用 error.Label.setText(...)
即可使用各种错误消息更新标签。当然这意味着需要将标签定义为实例变量。
这是我的第一个 GUI,所以这里的任何建议都是很好的建议,但主要是我只是想弄清楚为什么我的 errorLabel
不会出现。我试过 setVisible
、setOpaque
,手动设置位置,最后(如下所示)只是让它成为自己的面板,按下按钮时它仍然不会显示.
有什么想法吗?
package GUI;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import javax.swing.*;
/**
*
* @author User
*/
public class AddChair extends JFrame {
private final int FRAME_HEIGHT = 600;
private final int FRAME_WIDTH = 400;
private final String ADD_CHAIR_TITLE = "Chair";
private final int TEXT_BOX_WIDTH = FRAME_WIDTH / 4;
private final int TEXT_BOX_HEIGHT = 25;
private JPanel basePanel;
private JPanel leftPanel;
private JPanel rightPanel;
private JPanel lowerPanel;
private JPanel itemIDPanel;
private JPanel woodTypePanel;
private JPanel armrestPanel;
private JPanel quantityPanel;
private JPanel itemIDInputPanel;
private JPanel woodTypeInputPanel;
private JPanel quantityInputPanel;
private JPanel armrestInputPanel;
private JPanel confirmCancelPanel;
private JPanel errorPanel;
private JTextField itemIDTextField;
private JTextField quantityTextField;
private JRadioButton walnut;
private JRadioButton oak;
private JRadioButton yes;
private JRadioButton no;
private JButton confirmButton;
private JButton cancelButton;
public AddChair() {
frameSetup();
add(basePanel());
}
private void frameSetup() { // sets up base frame
setSize(FRAME_WIDTH, FRAME_HEIGHT);
setVisible(true);
setAlwaysOnTop(true);
setTitle(ADD_CHAIR_TITLE);
setLocationRelativeTo(null);
}
private JPanel basePanel() { //sets up a base panel to arrange other panels on
basePanel = new JPanel(new BorderLayout());
basePanel.setPreferredSize(new Dimension(FRAME_WIDTH, FRAME_HEIGHT));
basePanel.add(leftPanel(), BorderLayout.WEST);
basePanel.add(rightPanel(), BorderLayout.EAST);
basePanel.add(lowerPanel(), BorderLayout.SOUTH);
return basePanel;
}
private JPanel leftPanel() { //sets up the left panel which will contain labels
leftPanel = new JPanel();
leftPanel.setPreferredSize(new Dimension(FRAME_WIDTH / 2, FRAME_HEIGHT * 3 / 4));
leftPanel.setVisible(true);
labelPanels();
labels();
return leftPanel;
}
private void labelPanels() {//sets up the panels for the labels
itemIDPanel = new JPanel();
woodTypePanel = new JPanel();
armrestPanel = new JPanel();
quantityPanel = new JPanel();
itemIDPanel.setPreferredSize(new Dimension(FRAME_WIDTH / 2, FRAME_HEIGHT * 3 / 4 / 5));
woodTypePanel.setPreferredSize(new Dimension(FRAME_WIDTH / 2, FRAME_HEIGHT * 3 / 4 / 5));
armrestPanel.setPreferredSize(new Dimension(FRAME_WIDTH / 2, FRAME_HEIGHT * 3 / 4 / 5));
quantityPanel.setPreferredSize(new Dimension(FRAME_WIDTH / 2, FRAME_HEIGHT * 3 / 4 / 5));
itemIDPanel.setVisible(true);
woodTypePanel.setVisible(true);
armrestPanel.setVisible(true);
quantityPanel.setVisible(true);
leftPanel.add(itemIDPanel);
leftPanel.add(woodTypePanel);
leftPanel.add(armrestPanel);
leftPanel.add(quantityPanel);
}
private void labels() { //sets up the and adds the labels for the user
JLabel itemIDLabel = new JLabel("Item ID:");
JLabel woodTypeLabel = new JLabel("Wood Type:");
JLabel armrestLabel = new JLabel("Armrest:");
JLabel quantityLabel = new JLabel("Quantity:");
itemIDLabel.setVisible(true);
woodTypeLabel.setVisible(true);
armrestLabel.setVisible(true);
quantityLabel.setVisible(true);
itemIDPanel.add(itemIDLabel);
woodTypePanel.add(woodTypeLabel);
armrestPanel.add(armrestLabel);
quantityPanel.add(quantityLabel);
}
private JPanel rightPanel() { // sets up the right panel which will contain the user input objects
rightPanel = new JPanel();
rightPanel.setPreferredSize(new Dimension(FRAME_WIDTH / 2, FRAME_HEIGHT * 3 / 4));
rightPanel.setVisible(true);
userInputPanels();
return rightPanel;
}
private void userInputPanels() {//sets up the panels for the labels
itemIDInputPanel = new JPanel();
woodTypeInputPanel = new JPanel();
armrestInputPanel = new JPanel();
quantityInputPanel = new JPanel();
itemIDInputPanel.setPreferredSize(new Dimension(FRAME_WIDTH / 2, FRAME_HEIGHT * 3 / 4 / 5));
woodTypeInputPanel.setPreferredSize(new Dimension(FRAME_WIDTH / 2, FRAME_HEIGHT * 3 / 4 / 5));
armrestInputPanel.setPreferredSize(new Dimension(FRAME_WIDTH / 2, FRAME_HEIGHT * 3 / 4 / 5));
quantityInputPanel.setPreferredSize(new Dimension(FRAME_WIDTH / 2, FRAME_HEIGHT * 3 / 4 / 5));
itemIDInputPanel.setVisible(true);
woodTypeInputPanel.setVisible(true);
armrestInputPanel.setVisible(true);
quantityInputPanel.setVisible(true);
userInputs();
rightPanel.add(itemIDInputPanel);
rightPanel.add(woodTypeInputPanel);
rightPanel.add(armrestInputPanel);
rightPanel.add(quantityInputPanel);
}
private void userInputs() { // sets up and adds to the right panel the user input objects
itemIDTextField = new JTextField("");
walnut = new JRadioButton("Walnut");
oak = new JRadioButton("Oak");
yes = new JRadioButton("Yes");
no = new JRadioButton("No");
quantityTextField = new JTextField("");
itemIDTextField.setPreferredSize(new Dimension(TEXT_BOX_WIDTH, TEXT_BOX_HEIGHT));
itemIDInputPanel.add(itemIDTextField);
ButtonGroup woodTypeGroup = new ButtonGroup();
woodTypeGroup.add(oak);
woodTypeGroup.add(walnut);
woodTypeInputPanel.add(walnut);
woodTypeInputPanel.add(oak);
ButtonGroup armrestGroup = new ButtonGroup();
armrestGroup.add(yes);
armrestGroup.add(no);
armrestInputPanel.add(yes);
armrestInputPanel.add(no);
quantityTextField.setPreferredSize(new Dimension(TEXT_BOX_WIDTH, TEXT_BOX_HEIGHT));
quantityInputPanel.add(quantityTextField);
}
private JPanel lowerPanel() { // sets up the lower panel for confirm and cancel buttons
lowerPanel = new JPanel(new BorderLayout());
lowerPanel.setPreferredSize(new Dimension(FRAME_WIDTH, FRAME_HEIGHT / 4));
lowerPanel.setVisible(true);
confirmCancelPanel();
errorPanel();
confirmCancelButtons();
return lowerPanel;
}
private void confirmCancelPanel(){
confirmCancelPanel = new JPanel();
confirmCancelPanel.setPreferredSize(new Dimension(FRAME_WIDTH, FRAME_HEIGHT / 8));
confirmCancelPanel.setBackground(Color.blue);
confirmCancelPanel.setVisible(true);
lowerPanel.add(confirmCancelPanel, BorderLayout.NORTH);
}
private void errorPanel(){
errorPanel = new JPanel();
errorPanel.setPreferredSize(new Dimension(FRAME_WIDTH, FRAME_HEIGHT / 8));
errorPanel.setBackground(Color.yellow);
errorPanel.setVisible(true);
lowerPanel.add(errorPanel, BorderLayout.SOUTH);
}
private void confirmCancelButtons(){
confirmButton = new JButton("Confirm");
cancelButton = new JButton("Cancel");
confirmButton.setVisible(true);
cancelButton.setVisible(true);
confirmCancelPanel.add(confirmButton);
confirmCancelPanel.add(cancelButton);
cancelButtonLogic();
confirmButtonLogic();
}
private void cancelButtonLogic(){
cancelButton.addActionListener((ActionEvent ae) -> {
this.dispose();
});
}
private void confirmButtonLogic(){
confirmButton.addActionListener((ActionEvent ae) -> {
errorMessage();
});
}
private void errorMessage(){
JLabel errorLabel = new JLabel("Error");
errorLabel.setOpaque(true);
errorLabel.setVisible(true);
errorLabel.setForeground(Color.BLACK);
errorPanel.add(errorLabel);
}
}
我添加了一个静态 main 方法,这样我就可以 运行 GUI。我修改了你的 errorPanel
和 errorMessage
方法。
这是启动时的 GUI。
这是左键单击确认后的 GUI JButton
。
我没有详细查看您的代码,但您似乎发布了很多表单 JPanel
、按钮 JPanel
和错误 JPanel
的代码。
这是完整的运行可用代码。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
/**
*
* @author User
*/
public class AddChair extends JFrame {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new AddChair();
}
});
}
private final int FRAME_HEIGHT = 600;
private final int FRAME_WIDTH = 400;
private final String ADD_CHAIR_TITLE = "Chair";
private final int TEXT_BOX_WIDTH = FRAME_WIDTH / 4;
private final int TEXT_BOX_HEIGHT = 25;
private JLabel errorLabel;
private JPanel basePanel;
private JPanel leftPanel;
private JPanel rightPanel;
private JPanel lowerPanel;
private JPanel itemIDPanel;
private JPanel woodTypePanel;
private JPanel armrestPanel;
private JPanel quantityPanel;
private JPanel itemIDInputPanel;
private JPanel woodTypeInputPanel;
private JPanel quantityInputPanel;
private JPanel armrestInputPanel;
private JPanel confirmCancelPanel;
private JPanel errorPanel;
private JTextField itemIDTextField;
private JTextField quantityTextField;
private JRadioButton walnut;
private JRadioButton oak;
private JRadioButton yes;
private JRadioButton no;
private JButton confirmButton;
private JButton cancelButton;
public AddChair() {
frameSetup();
add(basePanel());
}
private void frameSetup() { // sets up base frame
setSize(FRAME_WIDTH, FRAME_HEIGHT);
setVisible(true);
setAlwaysOnTop(true);
setTitle(ADD_CHAIR_TITLE);
setLocationRelativeTo(null);
}
private JPanel basePanel() { //sets up a base panel to arrange other panels on
basePanel = new JPanel(new BorderLayout());
basePanel.setPreferredSize(new Dimension(FRAME_WIDTH, FRAME_HEIGHT));
basePanel.add(leftPanel(), BorderLayout.WEST);
basePanel.add(rightPanel(), BorderLayout.EAST);
basePanel.add(lowerPanel(), BorderLayout.SOUTH);
return basePanel;
}
private JPanel leftPanel() { //sets up the left panel which will contain labels
leftPanel = new JPanel();
leftPanel.setPreferredSize(new Dimension(FRAME_WIDTH / 2, FRAME_HEIGHT * 3 / 4));
leftPanel.setVisible(true);
labelPanels();
labels();
return leftPanel;
}
private void labelPanels() {//sets up the panels for the labels
itemIDPanel = new JPanel();
woodTypePanel = new JPanel();
armrestPanel = new JPanel();
quantityPanel = new JPanel();
itemIDPanel.setPreferredSize(new Dimension(FRAME_WIDTH / 2, FRAME_HEIGHT * 3 / 4 / 5));
woodTypePanel.setPreferredSize(new Dimension(FRAME_WIDTH / 2, FRAME_HEIGHT * 3 / 4 / 5));
armrestPanel.setPreferredSize(new Dimension(FRAME_WIDTH / 2, FRAME_HEIGHT * 3 / 4 / 5));
quantityPanel.setPreferredSize(new Dimension(FRAME_WIDTH / 2, FRAME_HEIGHT * 3 / 4 / 5));
itemIDPanel.setVisible(true);
woodTypePanel.setVisible(true);
armrestPanel.setVisible(true);
quantityPanel.setVisible(true);
leftPanel.add(itemIDPanel);
leftPanel.add(woodTypePanel);
leftPanel.add(armrestPanel);
leftPanel.add(quantityPanel);
}
private void labels() { //sets up the and adds the labels for the user
JLabel itemIDLabel = new JLabel("Item ID:");
JLabel woodTypeLabel = new JLabel("Wood Type:");
JLabel armrestLabel = new JLabel("Armrest:");
JLabel quantityLabel = new JLabel("Quantity:");
itemIDLabel.setVisible(true);
woodTypeLabel.setVisible(true);
armrestLabel.setVisible(true);
quantityLabel.setVisible(true);
itemIDPanel.add(itemIDLabel);
woodTypePanel.add(woodTypeLabel);
armrestPanel.add(armrestLabel);
quantityPanel.add(quantityLabel);
}
private JPanel rightPanel() { // sets up the right panel which will contain the user input objects
rightPanel = new JPanel();
rightPanel.setPreferredSize(new Dimension(FRAME_WIDTH / 2, FRAME_HEIGHT * 3 / 4));
rightPanel.setVisible(true);
userInputPanels();
return rightPanel;
}
private void userInputPanels() {//sets up the panels for the labels
itemIDInputPanel = new JPanel();
woodTypeInputPanel = new JPanel();
armrestInputPanel = new JPanel();
quantityInputPanel = new JPanel();
itemIDInputPanel.setPreferredSize(new Dimension(FRAME_WIDTH / 2, FRAME_HEIGHT * 3 / 4 / 5));
woodTypeInputPanel.setPreferredSize(new Dimension(FRAME_WIDTH / 2, FRAME_HEIGHT * 3 / 4 / 5));
armrestInputPanel.setPreferredSize(new Dimension(FRAME_WIDTH / 2, FRAME_HEIGHT * 3 / 4 / 5));
quantityInputPanel.setPreferredSize(new Dimension(FRAME_WIDTH / 2, FRAME_HEIGHT * 3 / 4 / 5));
itemIDInputPanel.setVisible(true);
woodTypeInputPanel.setVisible(true);
armrestInputPanel.setVisible(true);
quantityInputPanel.setVisible(true);
userInputs();
rightPanel.add(itemIDInputPanel);
rightPanel.add(woodTypeInputPanel);
rightPanel.add(armrestInputPanel);
rightPanel.add(quantityInputPanel);
}
private void userInputs() { // sets up and adds to the right panel the user input objects
itemIDTextField = new JTextField("");
walnut = new JRadioButton("Walnut");
oak = new JRadioButton("Oak");
yes = new JRadioButton("Yes");
no = new JRadioButton("No");
quantityTextField = new JTextField("");
itemIDTextField.setPreferredSize(new Dimension(TEXT_BOX_WIDTH, TEXT_BOX_HEIGHT));
itemIDInputPanel.add(itemIDTextField);
ButtonGroup woodTypeGroup = new ButtonGroup();
woodTypeGroup.add(oak);
woodTypeGroup.add(walnut);
woodTypeInputPanel.add(walnut);
woodTypeInputPanel.add(oak);
ButtonGroup armrestGroup = new ButtonGroup();
armrestGroup.add(yes);
armrestGroup.add(no);
armrestInputPanel.add(yes);
armrestInputPanel.add(no);
quantityTextField.setPreferredSize(new Dimension(TEXT_BOX_WIDTH, TEXT_BOX_HEIGHT));
quantityInputPanel.add(quantityTextField);
}
private JPanel lowerPanel() { // sets up the lower panel for confirm and cancel buttons
lowerPanel = new JPanel(new BorderLayout());
lowerPanel.setPreferredSize(new Dimension(FRAME_WIDTH, FRAME_HEIGHT / 4));
lowerPanel.setVisible(true);
confirmCancelPanel();
errorPanel();
confirmCancelButtons();
return lowerPanel;
}
private void confirmCancelPanel(){
confirmCancelPanel = new JPanel();
confirmCancelPanel.setPreferredSize(new Dimension(FRAME_WIDTH, FRAME_HEIGHT / 8));
confirmCancelPanel.setBackground(Color.blue);
confirmCancelPanel.setVisible(true);
lowerPanel.add(confirmCancelPanel, BorderLayout.NORTH);
}
private void errorPanel(){
errorPanel = new JPanel();
errorPanel.setPreferredSize(new Dimension(FRAME_WIDTH, FRAME_HEIGHT / 8));
errorPanel.setBackground(Color.yellow);
errorPanel.setVisible(true);
errorLabel = new JLabel(" ");
errorLabel.setOpaque(true);
errorLabel.setVisible(true);
errorLabel.setForeground(Color.BLACK);
errorPanel.add(errorLabel);
lowerPanel.add(errorPanel, BorderLayout.SOUTH);
}
private void confirmCancelButtons(){
confirmButton = new JButton("Confirm");
cancelButton = new JButton("Cancel");
confirmButton.setVisible(true);
cancelButton.setVisible(true);
confirmCancelPanel.add(confirmButton);
confirmCancelPanel.add(cancelButton);
cancelButtonLogic();
confirmButtonLogic();
}
private void cancelButtonLogic(){
cancelButton.addActionListener((ActionEvent ae) -> {
this.dispose();
});
}
private void confirmButtonLogic(){
confirmButton.addActionListener((ActionEvent ae) -> {
errorMessage();
});
}
private void errorMessage(){
errorLabel.setText("Error message");
}
}
I've tried setVisible,
Swing 组件默认可见。无需在组件上调用 setVisible(true)
my errorLabel wont show up.
将组件添加到可见 GUI 时,基本代码为:
errorPanel.add(errorLabel);
errorPanel.revalidate();
errorPanel.repaint();
这将确保调用布局管理器,以便为标签提供 size/location。
但是,另一种解决方案是在创建 GUI 时创建“errorLabel”并将其添加到“errorPanel”。您可以将集合设置为“”。所以标签占用了一些space。然后,您只需调用 error.Label.setText(...)
即可使用各种错误消息更新标签。当然这意味着需要将标签定义为实例变量。