gpt4 book ai didi

java - JPanels 内的 JButtons,填满整个面板

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

我一直在努力为使用 GridLayout 插入 JPanel 的按钮设置特定大小。

按钮总是填满整个面板,而如果我删除网格布局,按钮将不会有相同的行为。

有什么提示吗?

package panels;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
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.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class ColorDisplay {

private final int X = 100;
private final int Y = 100;
private final Dimension PANEL_SIZE = new Dimension(500,500);
private JTextField textRed;
private JTextField textGreen;
private JTextField textBlue;
private JLabel labelText, labelRed, labelGreen, labelBlue;
private JPanel displayPanel;
private JPanel textPanel;
private JPanel buttonPanel;
private JButton button;
private final Font font = new Font("Arial", Font.PLAIN, 22);

public static void main(String[] args) {
// TODO Auto-generated method stub

new ColorDisplay();


}
public ColorDisplay(){
JFrame mainFrame = new JFrame();

// make sure the program exits when the frame close
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setTitle("Color Display");
mainFrame.setLocation(X,Y);
mainFrame.setPreferredSize(PANEL_SIZE);

// ensure an elastic layout
mainFrame.setLayout(new GridLayout(3, 1));

mainFrame.setLocationByPlatform(true);

mainFrame.add(getColorPanel());
mainFrame.add(getTextPanel());
mainFrame.add(getButtonPanel());

mainFrame.pack();
mainFrame.setVisible(true);

}

public JPanel getColorPanel(){
displayPanel = new JPanel(new BorderLayout());
labelText = new JLabel("Color Display", JLabel.CENTER);
Font fontColorDisplay = new Font("Arial", Font.PLAIN, 42);
labelText.setFont(fontColorDisplay);

displayPanel.add(labelText);

return displayPanel;
}

public JPanel getTextPanel(){
textPanel = new JPanel(new GridLayout(2,3));
labelRed = new JLabel("Red", JLabel.CENTER);
labelGreen = new JLabel("Green", JLabel.CENTER);
labelBlue = new JLabel("Blue", JLabel.CENTER);
textRed = new JTextField();
textGreen = new JTextField();
textBlue = new JTextField();

labelRed.setFont(font);
labelGreen.setFont(font);
labelBlue.setFont(font);
textRed.setFont(font);
textGreen.setFont(font);
textBlue.setFont(font);

textPanel.add(labelRed);
textPanel.add(labelGreen);
textPanel.add(labelBlue);
textPanel.add(textRed);
textPanel.add(textGreen);
textPanel.add(textBlue);

return textPanel;
}

public JPanel getButtonPanel(){

buttonPanel = new JPanel(new BorderLayout());
button = new JButton("Display Color");
button.addActionListener(new ButtonListener ()); // Add event handler
button.setFont(font);
button.setPreferredSize(new Dimension(100, 100));

buttonPanel.add(button);
return buttonPanel;

}

private int getColor(){

String colorCode = textRed.getText() + textGreen.getText() + textBlue.getText();
return Integer.parseInt(colorCode);
}

private boolean validateColor(String textValue){
boolean isValid = false;
try {
int num1 = Integer.parseInt(textValue);
if (num1 >= 0 && num1 <= 255)
isValid = true;
else
{
isValid = false;
JOptionPane.showConfirmDialog(null, "Please enter numbers between 0 and 255", "Error", JOptionPane.PLAIN_MESSAGE);
}
} catch (NumberFormatException e) {
JOptionPane.showConfirmDialog(null, "Please enter numerical values", "Error", JOptionPane.PLAIN_MESSAGE);
}
return isValid;


}
private class ButtonListener implements ActionListener { // Inner class
public void actionPerformed(ActionEvent event) {

if (validateColor(textRed.getText()) && validateColor(textGreen.getText()) && validateColor(textBlue.getText()))
{
Color bgColor = new Color(getColor());
displayPanel.setBackground(bgColor);
}


}
}
}

最佳答案

您的问题是关于 GridLayout 但您使用 BorderLayout 显示代码:

buttonPanel = new JPanel(new BorderLayout());
button = new JButton("Display Color");
button.addActionListener(new ButtonListener ()); // Add event handler
button.setFont(font);
button.setPreferredSize(new Dimension(100, 100));

The button always fills up the whole panel, whereas if I remove the gridlayout, the button won't have the same behavior.

这是GridLayout默认行为,它的空间被平均划分,每个组件占据完整的空间(同样适用于BorderLayout)。

还有许多其他 LayoutManager 可以满足您的需求:

您可能想查看GridBagLayout哪个更灵活:

buttonPanel = new JPanel(new GridBagLayout());
button = new JButton("Display Color");
button.addActionListener(new ButtonListener()); // Add event handler
button.setFont(font);


GridBagConstraints gc=new GridBagConstraints();
gc.fill=GridBagConstraints.HORIZONTAL;
gc.gridx=0;
gc.gridy=0;
        
buttonPanel.add(button,gc);

enter image description here

甚至是默认的JPanel FlowLayout:

    buttonPanel = new JPanel();
button = new JButton("Display Color");
button.addActionListener(new ButtonListener()); // Add event handler
button.setFont(font);

buttonPanel.add(button);

enter image description here

或者像 MigLayout 这样的第三方 LayoutManger .

其他建议:

  • 不要调用setPreferredSize(..),而是重写getPreferredSize(),甚至不只在绘制到Graphic时执行此操作s 对象或想要使组件更大/更小,不要出于布局目的这样做,这是 LayoutManager 的工作。

  • 还请始终记住在 Event Dispatch Thread 上创建和操作 Swing 组件。通过 SwingUtilities.invokeLater(Runnable r) block

关于java - JPanels 内的 JButtons,填满整个面板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14550255/

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