JPanels内的JButton,填满整个面板 [英] JButtons inside JPanels, fill up the whole panel

查看:133
本文介绍了JPanels内的JButton,填满整个面板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力为插入带有GridLayout的JPanel中的按钮设置特定的大小.

I've been struggling to set a specific size to a button inserted into a JPanel with a GridLayout.

该按钮始终会填满整个面板,而如果删除网格布局,该按钮将不会具有相同的行为.

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

有什么提示吗?

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显示代码:

Your question is about GridLayout but you show code using 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));

?

该按钮始终会填满整个面板,而如果我删除 gridlayout,该按钮将不会具有相同的行为.

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

这是GridLayout的默认行为,它的空间被平均分配,并且每个组件都占满了整个空间(BorderLayout会使用相同的空间).

This is GridLayout default behavior, it space is divided equally and each component takes up the full space (same would apply for BorderLayout).

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

There are many other LayoutManagers which will will meet your needs:

您可能希望查看 GridBagLayout 更灵活:

You may want to look at GridBagLayout which is more flexible:

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);

,甚至是默认的JPanel FlowLayout:

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

    buttonPanel.add(button);

或第三方LayoutManger,例如 MigLayout .

Or a 3rd party LayoutManger like MigLayout.

其他建议

  • 不要调用setPreferredSize(..)而不是覆盖getPreferredSize(),甚至不仅仅在绘制到Graphic s对象或想要使组件更大/更小时才这样做,不要出于布局目的而这么做,那就是

  • Dont call setPreferredSize(..) rather override getPreferredSize() and even than only do this when painting to the Graphics object or wanting to make a component bigger/smaller dont do this for Layout purposes thats a LayoutManagers job.

还要始终记住在通过SwingUtilities.invokeLater(Runnable r)

这篇关于JPanels内的JButton,填满整个面板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆